经过前两节的学习,本人对Struts2的拦截器已经有了一定的理解。然而拦截器“博大精深”,还有很多东西需要在工作中慢慢体会并掌握。
本节就用学过的Struts2的拦截器知识模拟完成开头提出的那个小问题,即:当你以“游客”的身份去下载论坛的附件时会要求你登陆。这个问题的实质是当你下载时会在下载前在拦截器中检索当前用户的身份信息,如“session”中是否有你的用户名、电子邮件等。如果你登录了,自然会从session中找到你的消息,如果没有那你就杯具了,你懂得~
好了,新建一个拦截器类LoginMethodInterceptorUtil并继承MethodFilterInterceptor类,代码如下:
packagecom.iman.util; import com.opensymphony.xwork2.ActionContext; importcom.opensymphony.xwork2.ActionInvocation; importcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public classLoginMethodInterceptorUtil extends MethodFilterInterceptor { @Override protected StringdoIntercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated methodstub ActionContextac=invocation.getInvocationContext(); //获得容器实例 String username=(String)ac.getSession().get("username"); //从容器中读取sesion并获取用户信息 if(username!=null){ return invocation.invoke(); } return "input"; } }
这个拦截器类就是先获取容器,从容器中获取session,然后从session中读取用户信息,如果有的话放行,如果没有则返回“input”字符串。
接着在登陆action中将用户信息保存在session中,登陆方法如下所示:
……… public String login(){ if(username.equals("admin") && passworld.equals("123")){ ActionContext.getContext().getSession().put("username", username); return SUCCESS; } return ERROR; } …………..
好了,看struts.xml配置文件的配置情况:
<interceptor name="loginMethodInterceptor" class="com.iman.util.LoginMethodInterceptorUtil"></interceptor> </interceptors> <action name="loginAction"class="com.iman.action.LoginAction" method="login"> <result name="success">/results/loginSuccess.jsp</result> <result name="error">/results/loginError.jsp</result> </action> <action name="downloadAction" class="com.iman.action.LoginAction"> <result name="success">/interceptor/success.jsp</result> <result name="input">/interceptor/error.jsp</result> <interceptor-ref name="loginMethodInterceptor"></interceptor-ref> </action>
注意,登陆action不要拦截,这个你想想就知道了,我们只需要给downloadAction配置我们的loginMethodInterceptor拦截器就可以了。
下面页面如下所示:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>下载</title> </head> <body> <a href="../downloadAction">点我下载</a> </body> </html>
下载成功跳转页面:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s"uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>开始下载</title> </head> <body> <s:property value="#session['username']"/>,当你看到此页面时,下载已经开始...... </body> </html>
下载失败跳转页面:
<%@ page language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <meta http-equiv="refresh"content="3; url=./login.jsp"> <title>请登录</title> </head> <body> 对不起,你没有权限,请登录! </body> </html>
好了,程序算是弄完了,下面来测试拦截效果。
打开服务器并部署项目,首先不要登陆,即在session中没有用户信息的情况下下载。在地址栏输入:http://localhost:8000/Struts2Tutorial/interceptor/download.jsp,页面如下所示:
点击“点我下载”,由于没有登陆,session中固然没有用户的信息,拦截器在执行downloadAction之前从session中检索不出来用户的信息,所以,下载失败,页面如下所示:
我设置了页面自动跳转到登录页面login.jsp,登陆成功后将跳转到download.jsp页面,这时我们再点击“点我下载”,由于session中可以检索出用户的信息,所以会放行通过,即跳转到下载成功页面,页面如下所示:
这只是Struts2拦截器的一个很实用的功能而已,好了,就说这么多了,会了这些说明你对拦截器也了解的差不多了,祝你成功!
这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7089474,谢谢~