拦截器类
package cn.jzy.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyIntercepteor implements Interceptor {
private static final long serialVersionUID = 1L;
private String userName; //struts.xml中配置的属性
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("myIntercept");
System.out.println("struts中的属性: " + userName);
return arg0.invoke();
}
}
配置文件
<?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" />
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.custom.i18n.resources" value="mystruts"></constant>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="test" class="cn.jzy.interceptor.MyIntercepteor">
<param name="userName">你好</param>
</interceptor>
</interceptors>
<action name="index">
<result>/index.jsp</result>
</action>
<action name="login_*" class="cn.jzy.action.LoginAction" method="{1}">
<result name="show">/show.jsp</result>
<result name="invalid.token">/show.jsp</result>
<result name="input">/login.jsp</result> <!-- input为验证跳转 -->
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="test"></interceptor-ref>
<interceptor-ref name="fileUpload"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>