struts2拦截器的原理是AOP的,的运用
下面我的配置struts2的拦截器
首先在struts2.xml里面配置
<interceptors>
<interceptor name="fileDownload" class="com.wen.interceptor.FileDownloadInserceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="fileDownload"/>
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/index.jsp页面 -->
<result name="login">/index.jsp</result>
</global-results>
可以把相应的拦截器名称放入需要拦截的配置的struts2中配置的action中
<interceptor-ref name="fileDownload"/>
建立拦截器
package com.wen.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.wen.bean.person.User;
/** 拦截器
* Created by Administrator on 2016-09-09.
*/
public class FileDownloadInserceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
String result="";
User user = (User) ActionContext.getContext().getSession().get("currentUser");
if(user == null){
System.out.println("进入这个方法");
return "login";
}else {
result =actionInvocation.invoke();
return result;
}
}
}