ITeye博客

struts2中的拦截器(转)

zjcheng · 2010-01-08 16:31 · Struts2 · 企业架构 · 浏览器 · Apache · Struts · XML · JSP · Markdown

一.理解拦截器 1. 拦截器是在防问某个方法,字段之前或之后实施拦截,并且拦截器是可插拔的,拦截器是AOP的一种实现. 2. 拦截器栈(Interceptor Stack)。拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时, 拦截器链中的拦截器就会按其之前定义的顺序被调用。 二.实现原理 Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三.拦截器的配置 1. 普通的拦截器 Xml代码 [code="java"] 1.
2. 3. 4.
5. 6. 7.
8. 9. 10.
11. 12. 13.
14. 15. 16.
17. 18. 19.  
20. 21. 22.
23. 24. 25.
26. 27. 28. logon.jsp
29. 30. 31. /index.jsp
32. 33. 34.
35. 36. 37.    logon.jsp /index.jsp [/code] 2. 拦截器栈 Xml代码 [code="java"]1.
2. 3. 4.
5. 6. 7.
8. 9. 10.
11. 12. 13.
14. 15. 16.
17. 18. 19.
20. 21. 22.
23. 24. 25.
26. 27. 28.
29. 30. 31.     
32. 33. 34.
35. 36. 37. login.jsp
38. 39. 40. /index.jsp
41. 42. 43.
44. 45. 46.
[/code] [code="java"]       login.jsp /index.jsp [/code] 需要注意的是,如果为Action指定了一个拦截器,则系统默认的拦截器栈将会失去作用。为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器 四.自定义拦截器 1.直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。 2.或者继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor 3.通过元素来定义拦截器 4.通过元素来使用拦截器 五.使用拦截器实现权限控制 1.功能 使用自定义拦截器来完成用户权限的控制,当执行操作时,判断用户是否己经登陆,如果没有登陆跳转到登陆页面 2.拦截器类 Java代码 [code="java"]1.package com.interceptor; 2.
3.
import com.opensymphony.xwork2.Action; 4.
import com.opensymphony.xwork2.ActionContext; 5.
import com.opensymphony.xwork2.ActionInvocation; 6.
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 7.
8.
public class AuthorizationInterceptor extends AbstractInterceptor { 9.
10.
private static final long serialVersionUID = 1L; 11.
12.
@Override 13.
public String intercept(ActionInvocation actionInvocation) throws Exception { 14.
15.
ActionContext actionContext = actionInvocation.getInvocationContext(); 16.
17.
Object user = actionContext.get("user"); 18.
19.
if(user != null){ 20.
return actionInvocation.invoke(); 21.
} else{ 22.
actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆"); 23.
return Action.LOGIN; 24.
} 25.
} 26.
27.
} 28.
package com.interceptor; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthorizationInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L; @Override public String intercept(ActionInvocation actionInvocation) throws Exception { ActionContext actionContext = actionInvocation.getInvocationContext(); Object user = actionContext.get("user"); if(user != null){ return actionInvocation.invoke(); } else{ actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆"); return Action.LOGIN; } } }[/code] 2.配置 Xml代码[code="java"] 1.
2. 3. 4. 5. 6. 7. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
8. 9. 10. "http://struts.apache.org/dtds/struts-2.0.dtd">
11. 12.
13. 14. 15. 16. 17. 18.
19. 20.
21. 22. 23. 24. /logon/welcome.jsp
25. /logon/logon.jsp 26. 27. 28.
/logon/welcome.jsp /logon/logon.jsp [/code] 访问http://localhost:8080/struts2-interceptor/user.action时,会判断用户是否登陆 六.方法拦截器 1.Struts2提供MethodFilterInterceptor类,该类是AbstractInerceptor的子类,可以实现对Action方法的拦截. 2. MethodFilterInterceptor中有两个方法   setExcludeMethods:排除需要过滤的方法 setIncludeMethods:设置需要过滤的方法 如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截 3.实现 拦截器 Java代码 1.[code="java"]package com.interceptor; 2.
3.
import com.opensymphony.xwork2.ActionInvocation; 4.
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; 5.
6.
public class LogInterceptor extends MethodFilterInterceptor { 7.
8.
private static final long serialVersionUID = 1L; 9.
10.
private String name; 11.
12.
@Override 13.
protected String doIntercept(ActionInvocation actionInvocation) throws Exception { 14.
15.
System.out.println("拦截器名称:" + name); 16.
System.out.println("action:" + actionInvocation.getAction()); 17.
18.
return actionInvocation.invoke(); 19.
} 20.
21.
public String getName() { 22.
return name; 23.
} 24.
25.
public void setName(String name) { 26.
this.name = name; 27.
} 28.
} 29.
package com.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class LogInterceptor extends MethodFilterInterceptor { private static final long serialVersionUID = 1L; private String name; @Override protected String doIntercept(ActionInvocation actionInvocation) throws Exception { System.out.println("拦截器名称:" + name); System.out.println("action:" + actionInvocation.getAction()); return actionInvocation.invoke(); } public String getName() { return name; } public void setName(String name) { this.name = name; } } action Java代码 1.package com;

2.public class ManageAction {

3. public String execute(){ 4. System.out.println("execute....");
5. return "succee"; 6. }
7. 8. public String search(){
9. System.out.println("search...."); 10. return "succee"; 11. } 12. public String add(){ 13. System.out.println("add...."); 14. return "succee"; 15. } 16.}
package com; public class ManageAction { public String execute(){ System.out.println("execute...."); return "succee"; } public String search(){ System.out.println("search...."); return "succee"; } public String add(){ System.out.println("add...."); return "succee"; } }[/code] struts.xml配置 Xml代码 1.
2.
3. 日志拦截
4. 5. 6. execute,add
7.
8. search,execute 9.
10. /welcome.jsp 11. 日志拦截 execute,add search,execute /welcome.jsp 打开浏览器访问 http://localhost:8080/struts2-interceptor/manage.action 会报执行execute方法,会执行拦截器 拦截器名称:日志拦截 action:com.ManageAction@1a0ae6d execute.... 当访问 http://localhost:8080/struts2-interceptor/manage!search.action 执行search方法,不会执行拦截器

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。