`

2、struts2的action及result总结

 
阅读更多
1、action
具体视图的返回可以由用户自己定义的Action来决定,具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容

Action的实现
a.普通的java类,里面有public String execute方法即可
public class IndexAction1 {
public String execute() {
return "success";
}
}
b.实现Action接口
import com.opensymphony.xwork2.Action;

public class IndexAction2 implements Action {
@Override
public String execute() {
return "success";
}
}
c.最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction3 extends ActionSupport {

@Override
public String execute() {
return "success";
}
}

实现Action接口,也可以继承Actionsupport这个类.到底这两个有什么区别呢?

Action接口有:
public static final java.lang.String SUCCESS = "success";
public static final java.lang.String NONE = "none";
public static final java.lang.String ERROR = "error";
public static final java.lang.String INPUT = "input";
public static final java.lang.String LOGIN = "login";

public abstract java.lang.String execute() throws java.lang.Exception;

而Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,必须在配置该Action时配置input属性。

另外,Actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息.

这样在自定义标签时可以定义一个变量为new actionsupport对象实现国际化。

2、result
在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这
些result-type

       <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" />

            <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->

            <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

            <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />

        </result-types>



封装跳转逻辑

  Result的首要职责,是封装Action层到View层的跳转逻辑。之前我们已经反复提到,Struts2的Action是一个与Web容器无关的POJO。所以,在Action执行完毕之后,框架需要把代码的执行权重新交还给Web容器,并转向到相应的页面或者其他类型的View层。而这个跳转逻辑,就由Result来完成。这样,好处也是显而易见的,对Action屏蔽任何Web容器的相关信息,使得每个层次更加清晰。

  View层的显示类型非常多,有最常见的JSP、当下非常流行的Freemarker/Velocity模板、Redirect到一个新的地址、文本流、图片流、甚至是JSON对象等等。所以Result层的独立存在,就能够对这些显示类型进行区分,并封装合理的跳转逻辑。

  以JSP转向为例,在Struts2自带的ServletDispatcherResult中就存在着核心的JSP跳转逻辑:

常用的Result

  接下来,大致介绍一下Struts2内部已经实现的Result,并看看他们是如何工作的。

  dispatcher

Xml代码

<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>

  dispatcher主要用于返回JSP,HTML等以页面为基础View视图,这个也是Struts2默认的Result类型。在使用dispatcher时,唯一需要指定的,是JSP或者HTML页面的位置,这个位置将被用于定位返回的页面:

Xml代码

<result name="success">/index.jsp</result>

  而Struts2本身也没有对dispatcher做出什么特殊的处理,只是简单的使用Servlet API进行forward。

  freemarker / velocity

Xml代码
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>

  随着模板技术的越来越流行,使用Freemarker或者Velocity模板进行View层展示的开发者越来越多。Struts2同样为模板作为Result做出了支持。由于模板的显示需要模板(Template)与数据(Model)的紧密配合,所以在Struts2中,这两个Result的主要工作是为模板准备数据。

  以Freemarker为例,我们来看看它是如何为模板准备数据的:

Java代码
public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException { 
    this.location = location; 
    this.invocation = invocation; 
    this.configuration = getConfiguration(); 
    this.wrapper = getObjectWrapper(); 

    // 获取模板的位置 
    if (!location.startsWith("/")) { 
        ActionContext ctx = invocation.getInvocationContext(); 
        HttpServletRequest req = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); 
        String base = ResourceUtil.getResourceBase(req); 
        location = base + "/" + location; 
    } 

    // 得到模板 
    Template template = configuration.getTemplate(location, deduceLocale()); 
    // 为模板准备数据 
    TemplateModel model = createModel(); 

    // 根据模板和数据进行输出 
    // Give subclasses a chance to hook into preprocessing 
    if (preTemplateProcess(template, model)) { 
        try { 
            // Process the template 
            template.process(model, getWriter()); 
        } finally { 
            // Give subclasses a chance to hook into postprocessing 
            postTemplateProcess(template, model); 
        } 
    } 
}

public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException {
    this.location = location;
    this.invocation = invocation;
    this.configuration = getConfiguration();
    this.wrapper = getObjectWrapper();

    // 获取模板的位置
    if (!location.startsWith("/")) {
        ActionContext ctx = invocation.getInvocationContext();
        HttpServletRequest req = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        String base = ResourceUtil.getResourceBase(req);
        location = base + "/" + location;
    }

    // 得到模板
    Template template = configuration.getTemplate(location, deduceLocale());
    // 为模板准备数据
    TemplateModel model = createModel();

    // 根据模板和数据进行输出
    // Give subclasses a chance to hook into preprocessing
    if (preTemplateProcess(template, model)) {
        try {
            // Process the template
            template.process(model, getWriter());
        } finally {
            // Give subclasses a chance to hook into postprocessing
            postTemplateProcess(template, model);
        }
    }
}

  从源码中,我们可以看到,createModel()方法真正为模板准备需要显示的数据。而之前,我们已经看到过这个方法的源码,这个方法所准备的数据不仅包含ValueStack中的数据,还包含了被封装过的HttpServletRequest,HttpSession等对象的数据。从而使得模板能够以它特定的语法输出这些数据。 [SPAN]

  Velocity的Result也是类似,有兴趣的读者可以顺着思路继续深究源码。

  redirect

Xml代码
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

  如果你在Action执行完毕后,希望执行另一个Action,有2种方式可供选择。一种是forward,另外一种是redirect。有关forward和redirect的区别,这里我就不再展开,这应该属于Java程序员的基本知识。在Struts2中,分别对应这两种方式的Result,就是chain和redirect。

  先来谈谈redirect,既然是重定向,那么源地址与目标地址之间是2个不同的HttpServletRequest。所以目标地址将无法通过ValueStack等Struts2的特性来获取源Action中的数据。如果你需要对目标地址传递参数,那么需要在目标地址url或者配置文件中指出:

Xml代码
<!-- 
   The redirect-action url generated will be : 
   /genReport/generateReport.jsp?reportType=pie&width=100&height=100
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect">
         <param name="location">generateReport.jsp</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">${width}</param>
         <param name="height">${height}</param>
      </result>
   </action>

  同时,Redirect的Result支持在配置文件中,读取并解析源Action中ValueStack的值,并成为参数传递到Redirect的地址中。上面给出的例子中,width和height就是ValueStack中的值。

  chain

Xml代码
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

  再来谈谈chain,之前提到,chain其实只是在一个action执行完毕之后,forward到另外一个action,所以他们之间是共享HttpServletRequest的。在使用chain作为Result时,往往会配合使用ChainingInterceptor。有关ChainingInterceptor,Struts2的Reference说明了其作用:

  Struts2 Reference 写道:If you need to copy the properties from your previous Actions in the chain to the current action, you should apply the ChainingInterceptor. The Interceptor will copy the original parameters from the request, and the ValueStack is passed in to the target Action. The source Action is remembered by the ValueStack, allowing the target Action to access the properties of the preceding Action(s) using the ValueStack, and also makes these properties available to the final result of the chain, such as the JSP or Velocity page.

  也就是说,ChainingInterceptor的作用是在Action直接传递数据。事实上,源Action中ValueStack的数据会被做一次Copy,这样,2个Action中的数据都在ValueStack中,使得对于前台来说,通过ValueStack来取数据,是透明而共享的。chain这个Result有一些常用的使用情景,这点在Struts2的Reference中也有说明:

  Struts2 Reference 写道:One common use of Action chaining is to provide lookup lists (like for a dropdown list of states). Since these Actions get put on the ValueStack, their properties will be available in the view. This functionality can also be done using the ActionTag to execute an Action from the display page.

  比如说,一张页面中,你可能有许多数据要显示,而某些数据的获取方式可能被很多不同的页面共享(典型来说,“推荐文章”这个小栏目的数据获取,可能会被很多页面所共享)。这种情况下,可以把这部分逻辑抽取到一个独立Action中,并使用chain,将这个Action与主Action串联起来。这样,最后到达页面的时候,页面始终可以得到每个Action中的数据。

  不过chain这种Result,是在使用时需要慎重考虑的一种Result:

  Struts2 Reference 写道:As a rule, Action Chaining is not recommended. First explore other options, such as the Redirect After Post technique.

  而Struts2也做出了理由上的说明:

  Struts2 Reference 写道:Experience shows that chaining should be used with care. If chaining is overused, an application can turn into "spaghetti code". Actions should be treated as a Transaction Script, rather than as methods in a Business Facade. Be sure to ask yourself why you need to chain from one Action to another. Is a navigational issue, or could the logic in Action2 be pushed back to a support class or business facade so that Action1 can call it too?

Ideally, Action classes should be as short as possible. All the core logic should be pushed back to a support class or a business facade, so that Actions only call methods. Actions are best used as adapters, rather than as a class where coding logic is defined.

  从实战上将,使用chain作为Result也的确存在着上面所说的许多问题,我个人也是非常不推崇滥用这种Result。尤其是,对于使用Spring和Hibernate的朋友来说,如果你开启OpenSessionInView模式,那么Hibernate的session是跟随HttpServletRequest的,所以session在整个action链中共享。这会为我们的编程带来极大的麻烦。因为我们知道Hibernate的session会保留一份一级缓存,在action链中,共享一级缓存无疑会为你的调试工作带来很大的不方便。

  所以,谨慎使用chain作为你的Result,应该成为一条最佳实践。

  stream

Xml代码
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

  StreamResult等价于在Servlet中直接输出Stream流。这种Result被经常使用于输出图片、文档等二进制流到客户端。通过使用StreamResult,我们只需要在Action中准备好需要输出的InputStream即可。

Xml代码
<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="contentDisposition">filename="document.pdf"</param>
  <param name="bufferSize">1024</param>
</result>

  同时,StreamResult支持许多参数,对输出的Stream流进行参数控制。具体每个参数的作用,可以参考:http://struts.apache.org/2.0.14/docs/stream-result.html
2.2全局结果集
<package name="user" namespace="/user" extends="struts-default">
    	<global-results>
    		<result name="mainpage">/main.jsp</result>
    	</global-results>	    
</package>

2.3带参数的result
<package name="user" namespace="/user" extends="struts-default">
    <action name="user" class="com.wwm.struts2.user.action.UserAction">
	 <result type="redirect">/user_success.jsp?t=${type}</result>
    </action>	    
</package>

t=${type}取值栈中的值
其他

  Struts2的高度可扩展性保证了许多自定义的Result可以通过插件的形式发布出来。比较著名的有JSONResult,JFreeChartResult等等。有兴趣的读者可以在Struts2的官方网站上找到它们,并选择合适的加入到你的项目中去。
分享到:
评论

相关推荐

    struts2 result转向到action

    从jsp页面提交form表单后,在struts.xml中寻找匹配的action。在action中处理完业务后返回,在struts.xml中的result属性中转向到另外一个action。

    struts2的action的几种result type说明

    struts2的action的几种result type说明

    Struts2 in action中文版

    第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...

    struts2中result类型之redirectAction

    struts2中result类型之redirectAction

    jsp\No result defined for action and result input.doc

    jsp\No result defined for action and result input.doc

    Struts2从一个带参数action跳转到另一个action

    资源中示例了一个struts2的配置文件中配置的action的result结果为另外一个action的配置,该跳转的过程中是带所有参赛调整的,在另外一个action中可以接受到当前action的所有参数,此配置文件是我日常开发中的经验...

    struts2 in action

    《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...

    struts2中的result的type类型

    struts2 跳转类型 result type chain dispatcher redirect redirect action

    struts2实例 学生信息管理系统

    struts2实现的学生信息管理系统 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    Struts2实战.pdf

    《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...

    Struts2入门教程(全新完整版)

    十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...

    搭建struts2框架

    struts2框架的详细搭建 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    struts2零配置个人整理文档

    默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如: &lt;constant name="struts.convention....

    Struts2详解,Struts2与Struts1的区别

    Struts2和Struts1的常见区别 二. Struts2入门 3. 快速配置Struts2环境 4. 开发一个Struts2简单实例 5. struts.xml文件 三. Struts2深入开发 6. 常用&lt;Action&gt;标签配置和使用 7. 常用&lt;result&gt;标签配置和...

    struts2示例程序

    struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt; ...

    Struts2 学习笔记

    01 Struts2-Action 5 一、 Struts作用: 5 二、 搭建Struts2的运行环境: 5 三、 Namespace 6 四、 标签 6 五、 Action 6 六、 路径问题的说明 8 七、 Action的动态调用方法 8 八、 Action通配符(wildcard)的配置 9 ...

    Struts2的视频学习代码

    Struts2的视频学习代码主要包含Action,result以及ognl表达式等等,共两部分

    Struts2框架学习总结【自用】【原创】【详细】

    个人struts2框架学习后的梳理和总结,内容丰富,从配置文件到action类、result、ognl、类型转换器、国际化、拦截器、表单验证等等内容,还推荐了很多博客链接,有了它自学Struts2框架毫无压力!

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

    可以直接运行,并对整合spring和struts2步骤及需要注意的事项进行类总结 整合spring和struts2总结 1.将struts2和spring中的库文件复制到项目下(可以查看WEB-INF\lib目录下的文件) 注意:struts2-spring-...

    struts2流程与流程图

     一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果result。 Struts 2的核心控制器是FilterDispatcher,有3个重要的方法:destroy()、doFilter()和Init(),可以在Struts 2的下载...

Global site tag (gtag.js) - Google Analytics