拦截器是Struts2的基础,什么是拦截器呢?(二)

packagetutorial;

importcom.opensymphony.xwork2.ActionSupport;

publicclassAuthorizatedAccessextendsActionSupportimplementsRoleAware{

privateStringrole;

publicvoidsetRole(Stringrole){

this.role=role;

}

publicStringgetRole(){

returnrole;

}

@Override

publicStringexecute(){

returnSUCCESS;

}

}

以下是ShowUser.jsp的代码:

<%@pagecontentType="text/html;charset=UTF-8"%>

<%@taglibprefix="s"uri="/struts-tags"%>

<html>

<head>

<title>AuthorizatedUser</title>

</head>

<body>

<h1>Yourroleis:<s:propertyvalue="role"/></h1>

</body>

</html>

然后,创建tutorial.Roles初始化角色列表,代码如下:

packagetutorial;

importjava.util.Hashtable;

importjava.util.Map;

publicclassRoles{

publicMap<String,String>getRoles(){

Map<String,String>roles=newHashtable<String,String>(2);

roles.put("EMPLOYEE","Employee");

roles.put("MANAGER","Manager");

returnroles;

}

}

接下来,新建Login.jsp实例化tutorial.Roles,并将其roles属性赋予<s:radio>标志,代码如下:

<%@pagecontentType="text/html;charset=UTF-8"%>

<%@taglibprefix="s"uri="/struts-tags"%>

<html>

<head>

<title>Login</title>

</head>

<body>

<h1>Login</h1>

Pleaseselectarolebelow:

<s:beanid="roles"name="tutorial.Roles"/>

<s:formaction="Login">

<s:radiolist="#roles.roles"value="'EMPLOYEE'"name="role"label="Role"/>

<s:submit/>

</s:form>

</body>

</html>

创建Action类tutorial.Login将role放到session中,并转到Action类tutorial.AuthorizatedAccess,代码如下:

packagetutorial;

importjava.util.Map;

importorg.apache.struts2.interceptor.SessionAware;

importcom.opensymphony.xwork2.ActionSupport;

publicclassLoginextendsActionSupportimplementsSessionAware{

privateStringrole;

privateMapsession;

publicStringgetRole(){

returnrole;

}

publicvoidsetRole(Stringrole){

this.role=role;

}

publicvoidsetSession(Mapsession){

this.session=session;

}

@Override

publicStringexecute(){

session.put("ROLE",role);

returnSUCCESS;

}

}

最后,配置struts.xml文件,内容如下:

<!DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<includefile="struts-default.xml"/>

<packagename="InterceptorDemo"extends="struts-default">

<interceptors>

<interceptorname="auth"class="tutorial.AuthorizationInterceptor"/>

</interceptors>

<actionname="Timer"class="tutorial.TimerInterceptorAction">

<interceptor-refname="timer"/>

<result>/Timer.jsp</result>

</action>

<actionname="Login"class="tutorial.Login">

<resulttype="chain">AuthorizatedAccess</result>

</action>

<actionname="AuthorizatedAccess"class="tutorial.AuthorizatedAccess">

<interceptor-refname="auth"/>

<resultname="login">/Login.jsp</result>

<resultname="success">/ShowRole.jsp</result>

</action>

</package>

</struts>

发布运行应用程序,在浏览器地址栏中输入:http://localhost:8080/Struts2_Interceptor/AuthorizatedAccess.action。由于此时,session还没有键为“ROLE”的值,所以返回Login.jsp页面,如图2所示:

图2Login.jsp

选中Employee,点击Submit,出现图3所示页面:

图3ShowRole.jsp

总结

拦截器是Struts2比较重要的一个功能。通过正确地使用拦截器,我们可以编写高可复用的代码

Trackback:http://tb.blog.csdn.net/TrackBack.aspx?PostId=2242764

相关推荐