关于Struts2的自定义的验证截器
- package ch06.struts2.Interceptor;
-
- import java.util.Map;
-
- import com.opensymphony.xwork2.Action;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.Interceptor;
-
-
- public class AuthenticationInterceptor implements Interceptor {
-
- private static final long serialVersionUID = 1L;
-
- public static final String USER_SESSION_KEY = "UserSessionKey";
-
- public void destroy() {}
-
- public void init() {}
-
-
- public String intercept(ActionInvocation actionInvocation) throws Exception {
-
- Map session = actionInvocation.getInvocationContext().getSession();
-
- String user = (String) session.get(USER_SESSION_KEY);
-
- boolean isAuthenticated = (null!=user);
- if (!isAuthenticated) {
-
-
- return Action.LOGIN;
-
- }else{
-
- return actionInvocation.invoke();
- }
- }
- }
struts.xml配置自定义的拦截器
-
- <interceptors>
- <interceptor name="Authentication" class="ch06.struts2.Interceptor.AuthenticationInterceptor" />
- </interceptors>
-
- <action name="Welcome">
- <interceptor-ref name ="Authentication"/>
- <result name="success">welcome.jsp</result>
- </action>
actionInvocation.invoke() 就是通知struts2接着干下面的事情
比如 调用下一个拦截器 或 执行下一个Action
就等于退出了你自己编写的这个interceptor了