zoukankan      html  css  js  c++  java
  • 【学习笔记】Struts2之配置处理结果

        Action只是Struts2控制器的一部分,所以它不能直接生成对浏览者的响应。Action只负责生成响应的视图组件,通常是JSP页面,而Action会为JSP页面提供显示数据。

        Action处理完用户的请求后,将会返回一个普通字符串(称之为逻辑视图),Struts2框架收到这个逻辑视图后,把请求转发到对应的视图资源,视图资源将结果呈现给用户。必须在struts.xml文件中完成逻辑视图和物理视图资源的映射,才可以让系统转到实际的视图资源。

    一、配置结果

    Struts2在struts.xml中使用<result…/>元素来配置结果,根据该元素所在位置的不同,Struts2提供了两个结果。

    • 局部结果:将<result…/>作为<action…/>元素的子元素配置。
    • 全局结果:将<result…/>作为<global-result…/>元素的子元素配置。

    配置<result…/>元素时通常需要指定如下两个属性:

    • name:该属性指定所配置的逻辑视图名,缺省值为success。
    • type:该属性指定结果类型,缺省值为dispatcher(用于与JSP整合的结果类型),可以通过修改配置文件改变默认的结果类型。

    典型的<result…/>配置片段如下:

    <action name="abyss" class="abyss.LoginAction" >
    	<result name="success" type="dispatcher" >
    		<param name="location">/thank.jsp</param>
    	</result>
    </action>

    对于不同的结果类型,<param../>元素的name属性的参数是不同的,在此处<param…/>元素的name属性可以为如下两个值:

    • location:该参数指定了该逻辑视图对应的实际视图资源。
    • parse:该参数指定是否允许在实际视图名称中使用OGNL表达式,该参数默认为true。

    通常无须指定parse参数的值,所以常常采用一下简化形式来配置实际视图资源。

    <action name="Login" class="abyss.LoginAcion" >
    	<result name="success" type="dispatcher">/thank.jsp</result>
    </action>
     

    二、Struts2支持的结果类型

    Struts2支持使用多种视图技术,例如JSP、Velocity和FreeMarker等。

    结果类型决定了Action处理结束后,下一步将调用哪种视图资源来呈现处理结果。

    Struts2的结果类型要求实现com.opensymphony.xwork2.Result,这是所有结果类型的通用接口,如果我们要自定义结果类型就需要实现该类。

    Struts2默认提供了一些结果类型,位于struts-core-2.2.1.jar中的struts-default.xml中,下面是配置片段:

    <result-types>
    	<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
    	<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
    	<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
    	<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
    	<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
    	<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
    	<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
    	<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
    	<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
    	<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
    </result-types>

    从上面可以看出如果指定了default="true"属性,表示该类型是默认结果类型。

    三、Action属性配置视图资源

    配置<result…/>元素时,可以使用${属性名}的方式来指定视图资源。${属性名}里的属性名就是对应Action实例里的属性,并且还可以使用完全的OGNL表达式,即${属性名.属性名.属性名}。例如:

    <action name="login" class="org.crazyit.app.action.LoginAction">			
    	<result name="success">/${result}.jsp</result>
    </action>

    其中视图资源由LoginAction类中的result属性决定的,如果LoginAction类中没有result属性,则值为null。

    四、使用preResultListener

    PreResultListener是一个监听器接口,它可以在Action完成控制处理之后,系统转入实际的物理视图之间被回调。(可以理解为,定义一个函数beforeResult(),在页面转到视图资源之前,系统会自动调用该函数)。

    PreResultListener监听器可由Action和拦截器添加。一旦为Action添加PreResultListenr监听器,该监听器会自动调用beforeResult()方法;一旦为拦截器添加了PreResultListener监听器,该监听器会对该拦截器所拦截的所有Action起作用。

    在Action中添加示例如下:

    ActionContext.getContext().getActionInvocation().addPreResultListener(
    		new PreResultListener(){
    
    			@Override
    			public void beforeResult(ActionInvocation invocation, String resultCode) {
    				invocation.getInvocationContext().put("extra", new Date()+"由"+resultCode+
    						"逻辑视图名转入");
    			}
    			
    		});
    Technorati 标签: ,

     

     
     

  • 相关阅读:
    为什么要有handler机制
    安卓五种数据存储的方式
    Activity生命周期详解
    JS的一些简单实例用法
    JSP 中的EL表达式详细介绍
    JSP九大内置对象和四个作用域
    JS实现---图片轮播效果
    实现 鼠标移动到表格的某行 该行换背景 ---myEclipse编写
    JS 菜单收拉样式
    spring中aware接口的
  • 原文地址:https://www.cnblogs.com/freeabyss/p/3293347.html
Copyright © 2011-2022 走看看