1.interceptor的实例,重要的是如何获取请求的参数
package com.sid.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.sid.domain.User;
public class LoginInterceptor extends MethodFilterInterceptor {
private Map<String,Object> request;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//此方法下可以调用service层访从而访问数据库
request = (Map<String, Object>) ActionContext.getContext().get("request");
User user = (User) request.get("user");
if(user.getUsername().equals("pxc")){
return "errorName"; //action中相应方法的返回结果
}
else{
return invocation.invoke();
}
}
}
<interceptors>
<interceptor name="secondinter" class="com.sid.interceptor.SecondInterceptor"></interceptor>
<interceptor name="firstinter" class="com.sid.interceptor.FirstInterceptor"></interceptor>
<interceptor name="thridinter" class="com.sid.interceptor.ThridInterceptor">
<!-- <param name="excludeMethods">add,update</param> --> <!--不包括这两个方法,这两个方法不拦截,其他的拦截 -->
<param name="includeMethods">add,update</param> <!--包括这两个方法,对这两个方法拦截 -->
</interceptor>
<interceptor name="checkLogin" class="com.sid.interceptor.LoginInterceptor">
<param name="includeMethods">checkLogin</param>
</interceptor>
<interceptor-stack name="my">
<!--系统默认的拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="secondinter"></interceptor-ref>
<interceptor-ref name="firstinter"></interceptor-ref>
<interceptor-ref name="thridinter"></interceptor-ref>
<interceptor-ref name="checkLogin"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="checklogin" class="com.sid.web.action.LoginAction" method="checkLogin">
<!--此处哪个拦截器配在前面,先执行哪个.若引用了自定义的拦截器,必须引用系统默认的拦截器-->
<!-- <interceptor-ref name="firstinter"></interceptor-ref>
<interceptor-ref name="secondinter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref> -->
<interceptor-ref name="my"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="errorName">/error.jsp</result>
</action>