`

Struts2(10):Struts2的监听器与验权小例

阅读更多

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页面。

分享到:
评论

相关推荐

    STRUTS:listener监听器

    STRUTS:listener监听器

    Struts2的监听器的使用

    Struts2的监听器的使用实例 博文链接:https://zmx.iteye.com/blog/457435

    struts2拦截器及监听器示例demo

    默认登录页面 http://localhost:8080/Struts_ljq ...登录成功页面有add,delete,modify方法,配置有不同拦截,在后台看拦截信息。...有全局拦截器配置和方法拦截,及监听器简单运用, 仅新手学习demo

    整合struts2和spring源代码(可以直接在tomcat中运行)

    Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的...

    Struts2属性文件详解

    该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置...

    struts2+spring+hibernate整合示例

    web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ...

    struts2-spring-plugin-2.1.2.jar

    导入struts2-spring-plugin包,在web.xml中设置spring的监听器, spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为...

    Struts2\constant应用

    该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts 2内建的FreeMarker管理器。 struts.freemarker.wrapper.altMap 该属性只支持true和false两个属性值,默认值是true。通常...

    Struts2学习文档,Struts入门学习资料

    Struts2学习文档,Struts入门学习资料,包括(struts-xml,action,上传下载,监听器,拦截器,验证,类型转换,国际化等等)

    基于struts2 的网络U盘

    相关技术: Jsp+Struts2+MD5加密 开发工具: MyEclipse8.6+Tomcat6.0+SQLserver2008 项目描述: 独立完成所有功能开发。数据库包括用户表和文件表,项目主体由Struts2框架搭建,前台jsp页面表单使用OGNL标签,XX_zh_...

    Java Web整合开发王者归来(JSP+Servlet+Struts+Hibernate+Spring)

    第2篇为基础篇,内容包括Servlet技术、JSP技术、会话跟踪、过滤器Filter、监听器Listener等;第3篇为高级篇,内容包括JSTL标签库的使用、如何自定义标签、资源国际化、数据库概述、JDBC详解、XML概述等;第4篇为...

    struts2课件

    struts2课件 很好的struts2当输入login.jsp访问jsp页面填写完相关信息并提交给login.action时,它会首先被在web.xml中配置的过滤器监听到,过滤器会去查找strust.xml文件,并结合namespace查找名为login的action,...

    第17讲 Spring、hibernate和Struts2的整合

    ①在web.xml中配置ApplicationContext.xml,并使用ContextLoader监听器实例化spring容器 ②把action交给spring管理,即在spring配置文件中定义action Bean并使用依赖注入功能在action中注入业务Bean,同时修改作用域...

    Struts2 拦截结果监听器(三十)

    NULL 博文链接:https://takeme.iteye.com/blog/1651508

    学习常用知识(java,sql,oracle,ejb,ssh,struts,xml,监听器,拦截器,过滤器)

    学习常用知识(java,sql,oracle,ejb,ssh,struts,xml,监听器,拦截器,过滤器等等)这些都是本人的学习心得非常适用,希望能该初学者带来更大的帮助!!!

    Spring + struts +hibernate(SSHAnnotation) 全注解

    Struts2+Spring3+hibernate3全注解,操作oracle emp表的查询

    自定义PROXOOL监听器整合SSH框架

    2、创建独立的proxool.xml文件,同时存在Web的WEB-INF目录下,根据自己的数据库类型,填写不同的数据库驱动信息及具体配置信息,本文以oracle为例。 3、创建自定义数据库连接池监听 4、整合Struts2、Spring和proxool...

    Struts2框架整合Spring整合步骤

    1.新建工程,加入所需要的包; 2.配置sturts.xml; 3.配置Spring监听器; 4.Spring配置文件; .......

    Struts2.0 实现文件上传进度

    Struts2.0 实现文件上传进度 原理和实现 主要是实现Struts2.0的监听器

    Head First Servlets and JSP(中文版) 第二部分(共三部分)

    Head First Servlets and JSP(中文版) 第二部分(共三部分) 在我的资源下可找到其他两部部分 ...13 过滤器的威力:过滤器和包装器 14 企业设计模式:模式和struts A 附录A:最终模拟测验 i 索引

Global site tag (gtag.js) - Google Analytics