- 浏览: 120037 次
- 性别:
- 来自: 深圳
文章分类
最新评论
Struts2的监听器:
在xwork-2.0.7.jar包下,在com.opensymphony.xwork2.interceptor包下有个PreResultListener接口,自定义的监听器需实现此接口。
1,首先写一个自定义的Struts2监听器
MyListener.java
package com.test.listener; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyListener implements PreResultListener { public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("result ="+resultCode); } }
2,再在自定义的方法拦截器中调用此监听器,如下所示
package com.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import com.test.listener.MyListener; public class MyMethodInterceptor extends MethodFilterInterceptor { protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("before myMethodFilterInterceptor...."); invocation.addPreResultListener(new MyListener()); String invoked = invocation.invoke(); System.out.println("after myMethodFilterInterceptor...."); return invoked; } }
执行的顺序是:
1,先执行拦截器中的方法,如doIntercept。
2,在拦截器中调用invocation.invoke()来执行目标action中的validate验证方法。
3,如果validate验证成功,就执行execute方法或其它自定义的方法。
执行完后,则执行监听器类的beforeResult方法,并且其resultCode参数是success。
如果validate验证失败,则执行监听器类的beforeResult方法,并且其resultCode参数是input。
4,监听器中的方法执行完后,则返回到拦截器中的方法继续完成剩下的部分。
以上就是监听器的简单编写与流程介绍。
-------------------------------------------------------------------------------
验权小例:假设用户需先登录login.jsp页面输入登录用户信息,输入完成后,自动跳转到register.jsp页面填写注册信息,如果用户直接访问register.jsp页面输入信息,则在提交时,就跳转到login.jsp页面要求用户输入登录信息。即不允许用户直接访问register.jsp页面输入注册信息。
1,首先编写拦截器AuthInterceptor.java
package com.interceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class AuthInterceptor implements Interceptor { public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception { //取出session Map session = invocation.getInvocationContext().getSession(); //判断user值是否为空,如果为空,就跳转到 login.jsp页面 if(session.get("user") == null){ return Action.LOGIN; }else{ invocation.invoke(); } return null; } }
2,填写struts2.xml配置文件
<interceptor name="authInterceptor" class="com.interceptor.AuthInterceptor"></interceptor> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login.jsp</result> <!--input标签表示,如果action中validate方法的FieldError中有值,就会跳转到input标签指定的JSP页面--> <result name="success">/register.jsp</result> <result name="failer">/login.jsp</result> </action> <global-results> <!-- 定义全局常量,对应的AuthInterceptor.java类的Action.LOGIN常量, type="redirect"表示重定向--> <result name="login" type="redirect">/login.jsp</result> </global-results> <action name="login" class="com.test.action.LoginAction"> <result name="input">/login.jsp</result> <!--login输入成功,就跳转到/register.jsp页面 --> <result name="success">/register.jsp</result> <result name="failer">/login.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction" > <interceptor-ref name="authInterceptor"></interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action>
3,在LoginAction.java类中应设置SESSION的值
public String execute() { Map map = ActionContext.getContext().getSession(); //随便放置一个valid字符串 map.put("user", "valid"); if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())){ return "success"; }else{ this.addFieldError("username", "username or password error"); return "failer"; } }
用户在login.jsp页面输入登录信息后,跳转到LoginAction中,在此action中,将valid字符串放入session的user变量中,使user不为空,根据struts.xml的配置,login.jsp页面输入完成后,会自动跳转到register.jsp页面,用户在register.jsp页面输入完成点击提交,会跳转到RegisterAction,在此action中会判断session中的user是否为空,如果不为空,则继续流程,否则重新跳转到login.jsp页面。
发表评论
-
Struts2(18):Struts2深入探索续2
2011-06-06 03:15 586Struts2与Servlet是低耦合的,这可使项目不依赖于具 ... -
Struts2(17):Struts2深入探索续
2011-06-06 00:26 11181,struts2有属性驱动方式与模型驱动两种方式:之前的例子 ... -
Struts2(16): Struts2深入探索
2011-06-05 12:56 930先来看看struts2包中的一些配置文件: 1,strut ... -
Struts2(15):Struts2的国际化续
2011-06-04 23:38 921Struts2中action错误的国际化 在action类的 ... -
Struts2(14):Struts2的国际化
2011-06-04 02:44 888先看看JAVA的国际化实现:JAVA国际化是使用java.ut ... -
Struts2(13):Struts2的上传下载续2
2011-06-04 00:07 926本节主要介绍上传任意数据的文件、指定文件类型上传与Struts ... -
Struts2(12):Struts2的上传下载续
2011-05-29 12:40 931使用Struts2结束common的fileupload和io ... -
Struts2(11):Struts2的上传下载
2011-05-29 01:00 860首先来看看在JSP中上传是如何做的: 从upload.jsp ... -
Struts2(7):Struts2的输入校验框架(xml配置文件校验)
2011-05-22 22:00 2978上一节使用了Action类中的validate方法进行校验,此 ... -
Struts2(6):Struts2的输入校验2
2011-05-22 18:13 1082遇到类型转换错误的时候(也就是说不能进行类型转换),strut ... -
Struts2(5):Struts2的输入校验
2011-05-22 16:23 11321,Register.jsp,一个注册页面,包括用户名,密码, ... -
Struts2(4):Struts2类型转换2
2011-05-22 12:35 10751,修改上一节的input.jsp文件 <s:form ... -
Strtus2(3):struts2的类型转换
2011-05-21 23:34 10001,新建一个Point类,包含两个变量x,y。只有set,ge ... -
Struts2(2):Struts2的输入检验
2011-05-21 12:44 9531:新建一个login2.jsp,使用Struts2的标签编写 ... -
Struts2(1):入门与配置
2011-05-21 11:51 5631,在MyEclipse中新建一个WEB工程:struts2d ... -
Struts2(9):开发自己的Struts2拦截器(编码与配置)
2011-05-15 21:16 1523要开发自己的Struts2拦截器类,需要实现Inte ... -
Struts2(8):拦截器的简单实现原理
2011-05-15 18:29 870什么是拦截器: 拦截器,在AOP(Aspect-Oriente ...
相关推荐
STRUTS:listener监听器
Struts2的监听器的使用实例 博文链接:https://zmx.iteye.com/blog/457435
默认登录页面 http://localhost:8080/Struts_ljq ...登录成功页面有add,delete,modify方法,配置有不同拦截,在后台看拦截信息。...有全局拦截器配置和方法拦截,及监听器简单运用, 仅新手学习demo
Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的...
该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置...
web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ...
导入struts2-spring-plugin包,在web.xml中设置spring的监听器, spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为...
该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts 2内建的FreeMarker管理器。 struts.freemarker.wrapper.altMap 该属性只支持true和false两个属性值,默认值是true。通常...
Struts2学习文档,Struts入门学习资料,包括(struts-xml,action,上传下载,监听器,拦截器,验证,类型转换,国际化等等)
相关技术: Jsp+Struts2+MD5加密 开发工具: MyEclipse8.6+Tomcat6.0+SQLserver2008 项目描述: 独立完成所有功能开发。数据库包括用户表和文件表,项目主体由Struts2框架搭建,前台jsp页面表单使用OGNL标签,XX_zh_...
第2篇为基础篇,内容包括Servlet技术、JSP技术、会话跟踪、过滤器Filter、监听器Listener等;第3篇为高级篇,内容包括JSTL标签库的使用、如何自定义标签、资源国际化、数据库概述、JDBC详解、XML概述等;第4篇为...
struts2课件 很好的struts2当输入login.jsp访问jsp页面填写完相关信息并提交给login.action时,它会首先被在web.xml中配置的过滤器监听到,过滤器会去查找strust.xml文件,并结合namespace查找名为login的action,...
①在web.xml中配置ApplicationContext.xml,并使用ContextLoader监听器实例化spring容器 ②把action交给spring管理,即在spring配置文件中定义action Bean并使用依赖注入功能在action中注入业务Bean,同时修改作用域...
NULL 博文链接:https://takeme.iteye.com/blog/1651508
学习常用知识(java,sql,oracle,ejb,ssh,struts,xml,监听器,拦截器,过滤器等等)这些都是本人的学习心得非常适用,希望能该初学者带来更大的帮助!!!
Struts2+Spring3+hibernate3全注解,操作oracle emp表的查询
2、创建独立的proxool.xml文件,同时存在Web的WEB-INF目录下,根据自己的数据库类型,填写不同的数据库驱动信息及具体配置信息,本文以oracle为例。 3、创建自定义数据库连接池监听 4、整合Struts2、Spring和proxool...
1.新建工程,加入所需要的包; 2.配置sturts.xml; 3.配置Spring监听器; 4.Spring配置文件; .......
Struts2.0 实现文件上传进度 原理和实现 主要是实现Struts2.0的监听器
Head First Servlets and JSP(中文版) 第二部分(共三部分) 在我的资源下可找到其他两部部分 ...13 过滤器的威力:过滤器和包装器 14 企业设计模式:模式和struts A 附录A:最终模拟测验 i 索引