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";
} }
嘿嘿。。。就这样了....简单点。。。。。
Struts2 拦截器—拦截action的更多相关文章
- Struts自定义拦截器&拦截器工作原理
0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...
- 防止SpringMVC拦截器拦截js等静态资源文件
SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...
- Springboot前后端分离中,后端拦截器拦截后,前端没有对应的返回码可以判断
项目登录流程如下 用户进入前端登录界面,输入账号密码等,输入完成之后前端发送请求到后端(拦截器不会拦截登录请求),后端验证账号密码等成功之后生成Token并存储到数据库,数据库中包含该Token过期时 ...
- Springboot通过拦截器拦截请求信息收集到日志
1.需求 最近在工作中遇到的一个需求,将请求中的客户端类型.操作系统类型.ip.port.请求方式.URI以及请求参数值收集到日志中,网上找资料说用拦截器拦截所有请求然后收集信息,于是就开始了操作: ...
- Struts2使用自定义拦截器导致Action注入参数丢失、url参数
写struts2项目时发现前台超链接中的参数无法传到action, 所有带有传递参数的均无法正常使用了,在Action中所有的参数无法被注入. 后来经过debug发现其中的页面都要先经过拦截器,而后再 ...
- Struts2 自定义拦截器时Action无法接收到参数
问题:自定义拦截器,没有添加defaultStack导致Action无法接受到参数 解决办法: 方法一,添加defaultStack,然后在Action中引用 自定义的stack,其实defaultS ...
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
struts2.xml 内容 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
- struts2 权限拦截器 拦截没有登陆的请求
假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...
- 在JSP中常见问题,防止SpringMVC拦截器拦截js等静态资源文件的解决方案
方案一.拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml) <mvc:resources location="/" mapping="/**/* ...
随机推荐
- Java调用cmd压缩文件
今天在做一个java调用windows的压缩命令时遇到一奇怪问题代码如下: String cmd ="C:/Program Files (x86)/WinRAR/rar.exe a c:/t ...
- 局部敏感哈希-Locality Sensitive Hashing
局部敏感哈希 转载请注明http://blog.csdn.net/stdcoutzyx/article/details/44456679 在检索技术中,索引一直须要研究的核心技术.当下,索引技术主要分 ...
- Java 实现二分(折半)插入排序
设有一个序列a[0],a[1]...a[n];当中a[i-1]前是已经有序的,当插入时a[i]时,利用二分法搜索a[i]插入的位置 效率:O(N^2),对于初始基本有序的序列,效率上不如直接插入排序: ...
- JavaWeb学习总结(一)JavaWeb开发入门
静态网页和动态网页 静态网页:在服务器上没有经过服务器解释执行的网页. 动态网页:在服务器上经过服务器解释执行的网页. 无论是静态网页还是动态网页,客户端看到的网页都是由HTML所构成的,所以Java ...
- Microsoft Build 2016 Day 2
Microsoft Build 2016 Day 2 Microsoft Build 2016 Day 1 记录 Microsoft Build 2016 进行到了第二天,我觉得这一天的内容非常精彩, ...
- 让UIView窄斜
让UIView窄斜 by 吴雪莹 [UIView animateWithDuration:0.5 animations:^{ CGAffineTransform t = CGAffineTransfo ...
- WPF用SkewTransform画3D柱状图
WPF用SkewTransform画3D柱状图 SkewTransform主要是对控件实现一种2-D扭曲,具体内容可以查看以下链接: http://msdn.microsoft.com/zh-cn/l ...
- SQLSERVER图片查看工具SQL Image Viewer5.5.0.156
原文:SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 SQLSERVER图片查看工具SQL Image Viewer5.5.0.156 在2013年某一次北京SQL ...
- MySQL和Oracle开发差异
1) 数据类型差异 Oracle MySQL 注释 单独创建序列来实现 自动增长的数据类型 varchar2 varchar number tinyint,smallint,mediumint,in ...
- [文学阅读] 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 ...