struts2的actionmapper

1、result-type

#代码角度#在Action类中,每个 action 方法均返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果。

#配置角度#在struts.xml配置文件中,每个 Action 标签可包含多个 result 元素, 每个 result 标签分别对应着 action 方法的一个返回值。

result标签的属性

name: result名字, 默认值为 success,与 Action 方法返回值相匹配,否则可能出错。

type: result类型. 默认值为 dispatcher

我们的问题集中到这一点上,type究竟可以取哪些值呢?

对于这一问题的回答,需要借助于struts-default.xml文件,它位于struts2-core-2.3.29.jar下。

wKioL1dpph7ispjUAAEU8eURYRc072.png

在struts-default.xml中找到名为struts-default的package,它的下面定义了诸多的result-type。

wKiom1dppsrT1iGrAAEk8OeIgp0932.png

1
2
3
4
5
6
7
8
9
10
11
12
13
        <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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

在这里,我们只关心其中的三个:dispatcher, redirect, redirectAction

1
2
3
4
5
        <result-types>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
        </result-types>

1.1、dispatcher

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

ServletDispatcherResult

全名:org.apache.struts2.dispatcher.ServletDispatcherResult

(1)包含一个视图(Includes a view) 或者 转发到一个视图(forwards to a view)。

Includes or forwards to a view (usually a jsp). 

(2)实现机制:Struts将使用RequestDespatcher。

Behind the scenes Struts will use a RequestDispatcher, where the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute() - the Struts action is available.

(3)接受的参数有2个:location和parse

This result type takes the following parameters: 

location (default) - the location to go to after execution (ex. jsp). 

parse - true by default. If set to false, the location param will not be parsed for Ognl expressions. 

1.2、redirect

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

ServletRedirectResult

全名:org.apache.struts2.dispatcher.ServletRedirectResult

(1)实际机制:调用response.sendRedirect方法

Calls the sendRedirect method to the location specified. The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. 

(2)如果想要在使用redirect的时候传递参数,只能使用session和web parameters (url?name=value)。

The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions. 

(3)接受的参数有3个:location, parse和anchor

This result type takes the following parameters: 

location (default) - the location to go to after execution. 

parse - true by default. If set to false, the location param will not be parsed for Ognl expressions. 

anchor - Optional. Also known as "fragment" or colloquially as "hash". You can specify an anchor for a result. 

1.3、redirectAction

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

ServletActionRedirectResult

全名:org.apache.struts2.dispatcher.ServletActionRedirectResult

(1)实现机制:ActionMapperFactory工厂提供ActionMapper,这个mapper还是很重要的,以前java培训的老师讲过,ActionMapper引导浏览器到某个URL。

This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. 

(2)ServletActionRedirectResult比ServletRedirectResult要好一些。推荐使用ServletActionRedirectResult。

This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

(3)接受的参数有5个:actionName、namespace、suppressEmptyParameters、parse和anchor。

actionName (default) - The name of the action that will be redirected to. 

namespace - Used to determine which namespace the action is in that we're redirecting to. If namespace is null, the default will be the current namespace. 

suppressEmptyParameters - Optional boolean (defaults to false) that can prevent parameters with no values from being included in the redirect URL. 

parse - Boolean, true by default. If set to false, the actionName param will not be parsed for Ognl expressions. 

anchor - Optional. Also known as "fragment" or colloquially as "hash". You can specify an anchor for a result. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dispatcher------类型ServletDispatcherResult
该结果类型有一个 location 参数, 它是一个默认参数
转发控制权
 
 redirect----- 类型ServletRedirectResult
响应重定向到另一个资源(包括外部资源), 不是转发
redirect 参数:
location: 重定向目的地
param: 是否把 location 参数的值视为一个 OGNL 表达式来解释. 默认值为 true
 
redirectAction
响应重定向到另一个 Action
redirectAction类型ServletActionRedirectResult,
     为ServletDispatcherResult子类,redirectAction结果类型和redirect结果类型后台工作原理一样,即利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。
redirectAction主要用于重定向到action。如需重定向到另一个action,建议使用redirectAction。
1
2
3
4
5
6
7
8
9
10
11
12
13
<package name="default" extends="struts-default">
   <action name="test1" class="com.rupeng.Action1">
      <result type="redirectAction">
         <param name="actionName">test2.action</param>
         <param name="namespace">/test2NS</param>
      </result>
   </action>
</package>
<package name="secure" extends="struts-default" namespace="/test2NS">
    <action name="test2" class="com.rupeng.Action2">
        <result name="success">/success.jsp</result>
    </action>
</package>

2、全局result

全局result

概念:多个action中可共同使用的result。

1
2
3
4
5
<package ....>
    <global-results>
        <result name="globalSuccess">/globalSuccess.jsp</result>
    </global-results>
</package>

局部与全局出现相同的result时,局部起作用

原文地址:https://www.cnblogs.com/plan123/p/5606241.html