struts2 已有拦截器使用以及自定义拦截器使用

1.struts2 已有拦截器的使用

官方文档解释:拦截器是动态拦截Action调用的对象,拦截器提供一种机制,允许开发者定义一段Action执行前或执行后的代码,也允许在一个Action执行前阻止其执行。

在struts2-core-..jar包里,有struts-default.xml,这里定义了已有的拦截器。想使用的话,直接在自己工程的struts.xml中加入代码:

<include file="struts-default.xml"></include>

例如,使用已有的timer拦截器,struts配置如下:

 1 <!DOCTYPE struts PUBLIC
2 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
3 "http://struts.apache.org/dtds/struts-2.0.dtd">
4 <struts>
5 <include file ="struts-default.xml"/>
6 <package name ="wen" extends ="struts-default">
7 <action name ="Timer" class ="wen.TimerInterceptorAction">
8 <interceptor-ref name ="timer"/>
9 <result>/Timer.jsp</result>
10 </action>
11 </package>
12 </struts>

 

2.struts2 自定义拦截器的使用

我的工程目录如下:

struts2 已有拦截器使用以及自定义拦截器使用_第1张图片

 

 


 

为了方便理解,在代码里添加了后台打印

 

 

 

 

 

 

(1)AuthorizatedAccess.java  模拟访问受限资源

 1 package com.action;
2
3 import com.opensymphony.xwork2.ActionSupport;
4
5 public class AuthorizatedAccess extends ActionSupport implements RoleAware{
6
7 private String role;
8 public void setRole(String role) {
9 // TODO Auto-generated method stub
10 System.out.println(AuthorizationInterceptor.counter++ + " AuthorizatedAccess setRole " + role);
11 this.role = role;
12 }
13 public String getRole(){
14 System.out.println(AuthorizationInterceptor.counter++ + " AuthorizatedAccess getRole " + role);
15 return role;
16 }
17 @Override
18 public String execute() throws Exception {
19 // TODO Auto-generated method stub
20 System.out.println(AuthorizationInterceptor.counter++ + " AuthorizatedAccess.execute()");
21 return SUCCESS;
22 }
23 }

(2)AuthorizationInterceptor.java  自定义拦截器

 1 package com.action;
2
3 import java.util.Map;
4
5 import org.omg.CORBA.PUBLIC_MEMBER;
6
7 import com.opensymphony.xwork2.Action;
8 import com.opensymphony.xwork2.ActionInvocation;
9 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
10 import com.sun.org.apache.commons.collections.StaticBucketMap;
11
12 public class AuthorizationInterceptor extends AbstractInterceptor{
13 public static int counter = 0;
14
15 @Override
16 public String intercept(ActionInvocation ai) throws Exception {
17 // TODO Auto-generated method stub
18 Map session = ai.getInvocationContext().getSession();
19 String role = (String)session.get("ROLE");
20 if (null != role) {
21 System.out.println(counter++ + " AuthorizationInterceptor role != null .......and role===" + role);
22 Object object = ai.getAction();
23 if (object instanceof RoleAware) {
24 RoleAware action = (RoleAware)object;
25 action.setRole(role);
26 }
27 String hahaString = ai.invoke();
28 System.out.println(counter++ + " AuthorizationInterceptor ai.invoke()====" + hahaString);
29 return haha;
30 }else {
31 System.out.println(counter++ + " AuthorizationInterceptor role == null .......");
32 return Action.LOGIN;
33 }
34 }
35
36 }

(3)Login.java

 1 package com.action;
2
3 import java.util.Map;
4
5 import org.apache.struts2.interceptor.SessionAware;
6
7 import com.opensymphony.xwork2.ActionSupport;
8
9 public class Login extends ActionSupport implements SessionAware{
10 private String role;
11 private Map session;
12 public String getRole(){
13 System.out.println(AuthorizationInterceptor.counter++ + " Login getRole()...." + role);
14 return role;
15 }
16
17 public void setRole(String role){
18 System.out.println(AuthorizationInterceptor.counter++ + " Login setRole()...." + role);
19 this.role = role;
20 }
21 public void setSession(Map session) {
22 // TODO Auto-generated method stub
23 this.session = session;
24 System.out.println(AuthorizationInterceptor.counter++ + " Login setSession=====" + session);
25 }
26
27 @Override
28 public String execute() throws Exception {
29 // TODO Auto-generated method stub
30 System.out.println(AuthorizationInterceptor.counter++ + " Login execute 获取role======" + role);
31 session.put("ROLE", role);
32 return SUCCESS;
33 }
34 }

(4)RoleAware.java

1 package com.action;
2
3 public interface RoleAware {
4 void setRole(String role);
5 }

(5)Roles.java

 1 package com.lihui;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 public class Roles {
7 public Map<String, String> getRoles(){
8 Map<String, String> roles = new HashMap<String, String>();
9 roles.put("EMPLOYEE", "员工");
10 roles.put("MANAGER", "老板");
11 return roles;
12 }
13 }

(6)Login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%@ taglib prefix="s" uri="/struts-tags"%>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>登录</title>
7 </head>
8
9 <body>
10 <h1>Login</h1>
11 请选择一个角色:
12 <s:bean id="roles" name="com.lihui.Roles"/>
13 <s:form action="login">
14 <s:radio list="#roles.roles" value="EMPLOYEE" name="role" label="角色"></s:radio>
15 <s:submit value="提交"></s:submit>
16 </s:form>
17 </body>
18 </html>

(7)ShowUser.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%@ taglib prefix="s" uri="/struts-tags" %>
3
4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 <html>
6 <head>
7 <title>显示用户的角色</title>
8 </head>
9
10 <body>
11 <h1>您的角色是:
12 <s:property value="role"/>
13 </h1>
14 </body>
15 </html>

(8)struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5 <struts>
6 <include file="struts-default.xml"></include>
7 <package name="com.action" extends="struts-default" namespace="/test">
8 <interceptors>
9 <interceptor name="auth" class="com.action.AuthorizationInterceptor"></interceptor>
10 </interceptors>
11 <action name="login" class="com.action.Login">
12 <result type="chain">
13 <param name="actionName">authorizatedAccess</param>
14 <param name="namespace">/test</param>
15 </result>
16 </action>
17 <action name="authorizatedAccess" class="com.action.AuthorizatedAccess">
18 <interceptor-ref name="auth"></interceptor-ref>
19 <result name="success">/pages/ShowUser.jsp</result>
20 <result name="SUCCESS">/pages/ShowUser.jsp</result>
21 <result name="login">/pages/Login.jsp</result>
22 </action>
23 </package>
24 </struts>

然后输入网址:http://localhost:8080/Struts2Proj/test/authorizatedAccess.action 之所以中间还有个 /test 是因为程序中用了命名空间 namespace="/test"。而又之所以使用命名空间是因为在配置 struts.xml 时报错。

网页显示如下:

struts2 已有拦截器使用以及自定义拦截器使用_第2张图片 

第二个图是在一个图选择了员工并点击提交之后显示的界面。至于程序到底是怎么运行的,看一下后台输出就知道了。

struts2 已有拦截器使用以及自定义拦截器使用_第3张图片

0:一开始的时候 Role 肯定是空的,所以直接跳向登录界面。

在登录界面,选择了某个角色之后,点击提交,根据struts.xlml 首先会调用登录类 Login.java ,然后跳转到AuthorizatedAccess这个action,然后在这个action调用时先调用拦截器,如果role不为空,则通过 ai.invoke() 来调用AuthorizationInterceptor.java ,最后根据 AuthorizationInterceptor.java   的返回结果确定调用 Login.jsp 还是 ShowUser.jsp.







你可能感兴趣的:(struts2)