Struts2拦截器使用

一、配置和使用拦截器

要想使用struts-default.xml中的拦截器,只要在struts.xml配置文件中加入

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

并继承其中的 struts-default包(package)

最后在定义action时,使用

<interceptor-ref name="xx"/>

引用拦截器或者拦截器栈。

例子

新建一个Action类

public class TimterInterceptorAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		try {
			Thread.sleep(500);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

}

配置文件如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<include file="struts-default.xml"></include>
	<package name ="com.chuiyuan.action" extends="struts-default">
	
		<!-- action hello -->
		<action name="timer" class="com.chuiyuan.action.TimterInterceptorAction">
			<interceptor-ref name="timer"/>
			<result>/timer.jsp</result>
		</action>
		
		
	</package>

</struts>

timer.jsp文件可以随便写。

当我们第一次加载的时候,会发现时间远比第二次长,这是因为第一次加载的时候要做一些初始化的工作。

这里也说明了timer的一个作用,可以粗略的估计性能。

二、自定义拦截器

拦截器都是无状态的,这样不会有并发问题。

下面是一个例子。

  • 首先是struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<include file="struts-default.xml"></include>
	<package name="com.chuiyuan.action"  extends ="struts-default">
	
		<interceptors>
			<interceptor name="auth" class="com.chuiyuan.action.AuthorizationInterceptor" />
		</interceptors>
		
		<action name="login" class= "com.chuiyuan.action.Login">
			<result type="chain">authorizatedAccess</result>
		</action>
		
		<action name="authorizatedAccess" class="com.chuiyuan.action.AuthorizatedAccess">
			<interceptor-ref name="auth"/>
			<result name="login">/login.jsp</result>
			<result name="success">/showUser.jsp</result >
		</action>
	</package>

</struts>

在这里,我们注意到,我们定义了一个拦截器auth,当我们访问

http://localhost:8080/InterceptorDemo/authorizatedAccess.action

时,会先通过auth拦截器,拦截器结果分为两种,已经登陆或者没有登陆(通过查看session中是否有key="ROLE"的字符串来判断,

如果没有登陆,拦截器返回login,使authorizatedAccess 转到login.jsp。

如果已经登陆,将用户role放入到Action中,并调用 Action,返回success。

  • 拦截器如下
public class AuthorizationInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		Map session = ai.getInvocationContext().getSession() ;
		String role = (String)session.get("ROLE");
		if (null!= role){
			Object ob = ai.getAction() ;
			if (ob instanceof RoleAware){
				RoleAware action = (RoleAware) ob ;
				action.setRole(role);
			}
			return ai.invoke();
		}else {
			return Action.LOGIN ;
		}
	}

}
  •  Action如下
public class AuthorizatedAccess extends ActionSupport implements RoleAware {
	private String role;
	
	@Override
	public void setRole(String role) {
		this.role = role ;
	}
	
	public String getRole(){
		return role;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS ;
	}

}

其中RoleAware是为了方便将Role放入到Action中定义的,如下

public interface RoleAware {
	void setRole(String role) ;
}
  • login.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
	<head>
		<title>login</title>
	</head>
	<body>
		<h1>login</h1>
		请选择一个角色:
		<s:bean id="roles"  name="com.chuiyuan.entity.Roles"/>
		<s:form action="login">
			<s:radio list="# { 'EMPLOYEE':'Employee', 'MANAGER' : 'Manager'}"
			 value="EMPLOYEE" name="role" label="Role" />
			<s:submit/>
		</s:form>
	</body>
</html>
  • showUser.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
	<head>
		<title>显示用户角色</title>
	</head>
	<body>
	<h1>您的角色是:<s:property value="role"/></h1>
	</body>
</html>

注意:

1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。 
2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。
  

 

posted @ 2015-06-18 23:19  chuiyuan  阅读(166)  评论(0编辑  收藏  举报