Struts2 拦截器—拦截action
对于拦截器的基本使用这里我就懒得打字了,我这里就讲下如何用 Struts2 拦截器 拦截action。这是我个人的想法,如果有什么不对的,或者你们有什么更好的方法。请多多留言!
拦截器的默认拦截的方法参数是:includeMethods(要拦截的方法) 和 excludeMethods(不需要拦截的方法),多个的时候,用逗号分开;
但,现实中,有些时候。我们还是需要设置有3 个类的所有方法都不需要拦截和4个方法不需要拦截,那么如果使用excludeMethods 的话,就得把 3 个类中的所有方法都列出来。当然如果少就列列就算了,如果多呢。不就麻烦了。所以。下面我们将讨论如何拦截 类 这个级别的请求。
一、在配置文件中修改下:
- <interceptors>
- <interceptor name="mainSession" class="net.zy.interceptor.MainSessionInterceptor"></interceptor>
- <interceptor-stack name="mainSessionStack">
- <interceptor-ref name="mainSession">
- <param name="excludeMethods">login,logOut</param>
- <!-- 自定义参数 excludeActions -->
- <param name="excludeActions">sms_,main_</param>
- </interceptor-ref>
- <!-- 默认拦截器 -->
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <!-- 设置未默认的拦截器 -->
- <default-interceptor-ref name="mainSessionStack"></default-interceptor-ref>
<interceptors> <interceptor name="mainSession" class="net.zy.interceptor.MainSessionInterceptor"></interceptor> <interceptor-stack name="mainSessionStack"> <interceptor-ref name="mainSession"> <param name="excludeMethods">login,logOut</param> <!-- 自定义参数 excludeActions --> <param name="excludeActions">sms_,main_</param> </interceptor-ref> <!-- 默认拦截器 --> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 设置未默认的拦截器 --> <default-interceptor-ref name="mainSessionStack"></default-interceptor-ref>
在配置文件中,添加自定义参数。excludeActions
二、在拦截器类中,添加 excludeActions 成员变量,并且提供get,set方法,struts2会自定给我们set参数进来。
- package net.zy.interceptor;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import net.zy.models.SysUsers;
- import net.zy.models.VipBean;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.Interceptor;
- /**
- * 拦截器
- *
- * @author 妞见妞爱
- */
- public class MainSessionInterceptor implements Interceptor{
- private String excludeActions;
- private String excludeMethods;
- public String getExcludeMethods() {
- return excludeMethods;
- }
- public void setExcludeMethods(String excludeMethods) {
- this.excludeMethods = excludeMethods;
- }
- public String getExcludeActions() {
- return excludeActions;
- }
- public void setExcludeActions(String excludeActions) {
- this.excludeActions = excludeActions;
- }
- public void destroy() {
- // TODO Auto-generated method stub
- }
- public void init() {
- // TODO Auto-generated method stub
- }
- public String intercept(ActionInvocation invocation) throws Exception {
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpSession session = request.getSession();
- // 获取请求的action名称
- String actionName = invocation.getInvocationContext().getName();
- // 获取action后附带参数
- //Map parameters = invocation.getInvocationContext().getParameters();
- //配置文件中 排除的 action
- String [] excludeAction = excludeActions.split(",");
- for (int i = 0; i < excludeAction.length; i++) {
- if (actionName.startsWith(excludeAction[i])) {
- return invocation.invoke();
- }
- }
- //配置文件中 排除的 Method
- String [] excludeMethod = excludeMethods.split(",");
- for (int i = 0; i < excludeMethod.length; i++) {
- if (actionName.endsWith(excludeMethod[i])) {
- return invocation.invoke();
- }
- }
- SysUsers users = (SysUsers) session.getAttribute("user");
- if (users != null) {
- return invocation.invoke();
- }
- return "Timeout";
- }
- }
package net.zy.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import net.zy.models.SysUsers; import net.zy.models.VipBean; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; /** * 拦截器 * * @author 妞见妞爱 */ public class MainSessionInterceptor implements Interceptor{ private String excludeActions; private String excludeMethods; public String getExcludeMethods() { return excludeMethods; } public void setExcludeMethods(String excludeMethods) { this.excludeMethods = excludeMethods; } public String getExcludeActions() { return excludeActions; } public void setExcludeActions(String excludeActions) { this.excludeActions = excludeActions; } public void destroy() { // TODO Auto-generated method stub } public void init() { // TODO Auto-generated method stub } public String intercept(ActionInvocation invocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); // 获取请求的action名称 String actionName = invocation.getInvocationContext().getName(); // 获取action后附带参数 //Map parameters = invocation.getInvocationContext().getParameters(); //配置文件中 排除的 action String [] excludeAction = excludeActions.split(","); for (int i = 0; i < excludeAction.length; i++) { if (actionName.startsWith(excludeAction[i])) { return invocation.invoke(); } } //配置文件中 排除的 Method String [] excludeMethod = excludeMethods.split(","); for (int i = 0; i < excludeMethod.length; i++) { if (actionName.endsWith(excludeMethod[i])) { return invocation.invoke(); } } SysUsers users = (SysUsers) session.getAttribute("user"); if (users != null) { return invocation.invoke(); } return "Timeout"; } }
嘿嘿。。。就这样了....简单点。。。。。