常用的struts2的Interceptor易做图
我们可以自定义Interceptor继承com.opensymphony.xwork2.interceptor.AbstractInterceptor,即可定义易做图:易做图可以与Filter过滤器集合起来使用,Interceptor的配置在struts.xml中配置,可以拦截指定的struts2请求,一般是指.action后缀的请求。
[java]
package edu.press.utils;
import javax.servlet.http.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
/**
* Interceptors for security
* @author
* @version 1.0
*
*/
public class AdminInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
private HttpSession session;
public String intercept(ActionInvocation invocation) throws Exception {
session = ServletActionContext.getRequest().getSession(true);
// 简单的验证会话session是否有指定属性
if (session.getAttribute("XXXXXXXX") != null) {
return invocation.invoke(); //权限符合,请求顺利执行
}
return Action.LOGIN; //请求未通过,转向LOGIN的result
}
}
在struts.xml文件中配置:
[html]
<package name="press" extends="struts-default">
<interceptors>
<interceptor name="adminInterceptor"
/>
<interceptor-stack name="adminStack">
<interceptor-ref name="basicStack" />
<interceptor-ref name="adminInterceptor" />
</interceptor-stack>
</interceptors>
<global-results>
<result name="login" type="redirect">/login.jsp?err=1</result>
</global-results>
下边这是我们项目中的定义,拦截了多有继承struts-defult的package里的Action
[html]
<package name="struts-estone" extends="struts-default">
<interceptors>
<interceptor name="authority" class="com.estone.www.kepuoa.admin.action.interceptor.AuthorityInterceptor"/>
<interceptor-stack name="auctionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="auctionStack"/>
<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="login" type="redirect">/forword.jsp</result>
<result name="input">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="input" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
对于需要拦截的action,可按照如下例子:
[html]
<action name="uploadResource" method="upload">
<interceptor-ref name="adminStack"/>
<result name="success" type="redirect">XXXXXXXXXXX</result>
<result name="input">XXXXXXXXXXXXXXXX</result>
</action>
摘自 那年那月那天
补充:软件开发 , Java ,