对于拦截器的基本使用这里我就懒得打字了,我这里就讲下如何用 Struts2 拦截器 拦截action。这是我个人的想法,如果有什么不对的,或者你们有什么更好的方法。请多多留言!

拦截器的默认拦截的方法参数是:includeMethods(要拦截的方法) 和 excludeMethods(不需要拦截的方法),多个的时候,用逗号分开;

但,现实中,有些时候。我们还是需要设置有3 个类的所有方法都不需要拦截和4个方法不需要拦截,那么如果使用excludeMethods 的话,就得把 3 个类中的所有方法都列出来。当然如果少就列列就算了,如果多呢。不就麻烦了。所以。下面我们将讨论如何拦截 类 这个级别的请求。

一、在配置文件中修改下:

  1. <interceptors>
  2. <interceptor name="mainSession" class="net.zy.interceptor.MainSessionInterceptor"></interceptor>
  3. <interceptor-stack name="mainSessionStack">
  4. <interceptor-ref name="mainSession">
  5. <param name="excludeMethods">login,logOut</param>
  6. <!-- 自定义参数 excludeActions -->
  7. <param name="excludeActions">sms_,main_</param>
  8. </interceptor-ref>
  9. <!-- 默认拦截器 -->
  10. <interceptor-ref name="defaultStack"></interceptor-ref>
  11. </interceptor-stack>
  12. </interceptors>
  13. <!-- 设置未默认的拦截器 -->
  14. <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参数进来。

  1. package net.zy.interceptor;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpSession;
  4. import net.zy.models.SysUsers;
  5. import net.zy.models.VipBean;
  6. import org.apache.struts2.ServletActionContext;
  7. import com.opensymphony.xwork2.ActionInvocation;
  8. import com.opensymphony.xwork2.interceptor.Interceptor;
  9. /**
  10. * 拦截器
  11. *
  12. * @author 妞见妞爱
  13. */
  14. public class MainSessionInterceptor implements Interceptor{
  15. private String excludeActions;
  16. private String excludeMethods;
  17. public String getExcludeMethods() {
  18. return excludeMethods;
  19. }
  20. public void setExcludeMethods(String excludeMethods) {
  21. this.excludeMethods = excludeMethods;
  22. }
  23. public String getExcludeActions() {
  24. return excludeActions;
  25. }
  26. public void setExcludeActions(String excludeActions) {
  27. this.excludeActions = excludeActions;
  28. }
  29. public void destroy() {
  30. // TODO Auto-generated method stub
  31. }
  32. public void init() {
  33. // TODO Auto-generated method stub
  34. }
  35. public String intercept(ActionInvocation invocation) throws Exception {
  36. HttpServletRequest request = ServletActionContext.getRequest();
  37. HttpSession session = request.getSession();
  38. // 获取请求的action名称
  39. String actionName = invocation.getInvocationContext().getName();
  40. // 获取action后附带参数
  41. //Map parameters = invocation.getInvocationContext().getParameters();
  42. //配置文件中 排除的 action
  43. String [] excludeAction = excludeActions.split(",");
  44. for (int i = 0; i < excludeAction.length; i++) {
  45. if (actionName.startsWith(excludeAction[i])) {
  46. return invocation.invoke();
  47. }
  48. }
  49. //配置文件中 排除的 Method
  50. String [] excludeMethod = excludeMethods.split(",");
  51. for (int i = 0; i < excludeMethod.length; i++) {
  52. if (actionName.endsWith(excludeMethod[i])) {
  53. return invocation.invoke();
  54. }
  55. }
  56. SysUsers  users = (SysUsers) session.getAttribute("user");
  57. if (users != null) {
  58. return invocation.invoke();
  59. }
  60. return "Timeout";
  61. }
  62. }
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";
} }

嘿嘿。。。就这样了....简单点。。。。。

Struts2 拦截器—拦截action的更多相关文章

  1. Struts自定义拦截器&拦截器工作原理

    0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...

  2. 防止SpringMVC拦截器拦截js等静态资源文件

    SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...

  3. Springboot前后端分离中,后端拦截器拦截后,前端没有对应的返回码可以判断

    项目登录流程如下 用户进入前端登录界面,输入账号密码等,输入完成之后前端发送请求到后端(拦截器不会拦截登录请求),后端验证账号密码等成功之后生成Token并存储到数据库,数据库中包含该Token过期时 ...

  4. Springboot通过拦截器拦截请求信息收集到日志

    1.需求 最近在工作中遇到的一个需求,将请求中的客户端类型.操作系统类型.ip.port.请求方式.URI以及请求参数值收集到日志中,网上找资料说用拦截器拦截所有请求然后收集信息,于是就开始了操作: ...

  5. Struts2使用自定义拦截器导致Action注入参数丢失、url参数

    写struts2项目时发现前台超链接中的参数无法传到action, 所有带有传递参数的均无法正常使用了,在Action中所有的参数无法被注入. 后来经过debug发现其中的页面都要先经过拦截器,而后再 ...

  6. Struts2 自定义拦截器时Action无法接收到参数

    问题:自定义拦截器,没有添加defaultStack导致Action无法接受到参数 解决办法: 方法一,添加defaultStack,然后在Action中引用 自定义的stack,其实defaultS ...

  7. struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证

    struts2.xml 内容 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...

  8. struts2 权限拦截器 拦截没有登陆的请求

    假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...

  9. 在JSP中常见问题,防止SpringMVC拦截器拦截js等静态资源文件的解决方案

    方案一.拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml) <mvc:resources location="/" mapping="/**/* ...

随机推荐

  1. Java调用cmd压缩文件

    今天在做一个java调用windows的压缩命令时遇到一奇怪问题代码如下: String cmd ="C:/Program Files (x86)/WinRAR/rar.exe a c:/t ...

  2. 局部敏感哈希-Locality Sensitive Hashing

    局部敏感哈希 转载请注明http://blog.csdn.net/stdcoutzyx/article/details/44456679 在检索技术中,索引一直须要研究的核心技术.当下,索引技术主要分 ...

  3. Java 实现二分(折半)插入排序

    设有一个序列a[0],a[1]...a[n];当中a[i-1]前是已经有序的,当插入时a[i]时,利用二分法搜索a[i]插入的位置 效率:O(N^2),对于初始基本有序的序列,效率上不如直接插入排序: ...

  4. JavaWeb学习总结(一)JavaWeb开发入门

    静态网页和动态网页 静态网页:在服务器上没有经过服务器解释执行的网页. 动态网页:在服务器上经过服务器解释执行的网页. 无论是静态网页还是动态网页,客户端看到的网页都是由HTML所构成的,所以Java ...

  5. Microsoft Build 2016 Day 2

    Microsoft Build 2016 Day 2 Microsoft Build 2016 Day 1 记录 Microsoft Build 2016 进行到了第二天,我觉得这一天的内容非常精彩, ...

  6. 让UIView窄斜

    让UIView窄斜 by 吴雪莹 [UIView animateWithDuration:0.5 animations:^{ CGAffineTransform t = CGAffineTransfo ...

  7. WPF用SkewTransform画3D柱状图

    WPF用SkewTransform画3D柱状图 SkewTransform主要是对控件实现一种2-D扭曲,具体内容可以查看以下链接: http://msdn.microsoft.com/zh-cn/l ...

  8. SQLSERVER图片查看工具SQL Image Viewer5.5.0.156

    原文:SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 在2013年某一次北京SQL ...

  9. MySQL和Oracle开发差异

    1)  数据类型差异 Oracle MySQL 注释 单独创建序列来实现 自动增长的数据类型 varchar2 varchar number tinyint,smallint,mediumint,in ...

  10. [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments

    METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...