1.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<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>
</package>
</struts>
2.action
package com.sid.web.action;
import com.opensymphony.xwork2.ActionSupport;
import com.sid.domain.User;
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("in LoginAction method execute()");
System.out.println(user.getUsername());
return "success";
}
public String add() throws Exception {
// TODO Auto-generated method stub
System.out.println("in LoginAction method add()");
System.out.println(user.getUsername());
return "success";
}
public String del() throws Exception {
// TODO Auto-generated method stub
System.out.println("in LoginAction method del()");
System.out.println(user.getUsername());
return "success";
}
public String update() throws Exception {
// TODO Auto-generated method stub
System.out.println("in LoginAction method update()");
System.out.println(user.getUsername());
return "success";
}
public String checkLogin() throws Exception{
System.out.println("in LoginAction method checkLogin()");
return "success";
}
}
3.如果对action中不同的方法进行拦截,则需自定义的拦截器继承MethodFilterInterceptor(这个经常用)
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";
}
else{
return invocation.invoke();
}
}
}