Struts2 ResultType笔记

 一个result代表了一个可能的输出。当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出。
在com.opensymphony.xwork2.Action接口中定义了一组标准的结果代码,可供开发人员使用,当然了只有我们的action继承ActionSupport 这个类才可以使用下面的结果代码,如下所示:
public interface Action
{
    public static final String SUCCESS = “success”;
    public static final String NONE = “none”;
    public static final String ERROR = “error”;
    public static final String INPUT = “input”;
    public static final String LOGIN = “login”;
}
   其中 Struts2应用在运行过程中若发现addFieldError()中有信息或者类型转换失败或着输入校验失败等情况
那么它会自动跳转到name为input的<result/>,然后转到INPUT所对应的页面
若JSP页面中表单是用普通<form>编写的,发生错误而返回该页面时,则原数据将消失
若JSP页面中表单是用<s:form/>编写的,发生错误而返回该页面时,则原数据仍存在
若没有提供name值为input的<result/>,那么发生错误时,将直接在浏览器中提示404错误

Struts2的resultType种类

a.struts2自带的类型实现

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

b. 一些插件支持的类型,ex:struts2-json-plugin

Struts2的resultType详解

1. dispatcher:用来转向页面,通常处理 JSP

Dispatcher结果类型的实现是org.apache.struts2.dispatcher.ServletDispatcherResult,该类的二个属性(property):location和parse,这两个属性可以通过struts.xml配置文件中的result元素的param子元素来设置。param元素的name属性指定结果类型实现类的属性名,param元素的内容是属性的值。例如:
  <result name=“success”  type=“dispatcher”>
    <param name=“location” >/success.jsp</param>
     <param name=“parse” >true</param>
  </result>

  说明:

       A、location参数用于指定action执行完毕后要转向的目标资源,parse属性是一个布尔类型的值,如果为true,则解析location参数中的OGNL表达式;如果为false,则不解析。parse属性的默认值就是true.location参数是默认的参数,在所有的Result实现类中,都定义了一个字符串类型的DEFAULT_PARAM静态常量,专门用于指定默认的参数名。故上面例子可以简化成:

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

        B、如果是带参数的,可以使用OGNL表达式

  <result name=“success” >viewNews.jsp?id=${id}</result>

2、redirect结果类型(重定向到一个Url,也可以是Action或一个页面)
Redirect结果类型在后台使用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL,它的实现类是org.apache.struts2.dispatcher.ServletRedirectResult.该类同样有二个属性(property):location和parse,在使用redirect结果类型的场景中,用户要完成一次与服务器之间的交互,浏览器需要完成两次请求,因此第一次请求中的数据在第二次请求中是不可用的,这意味在目标资源中是不能访问action实例、action错误以及错误等。
如果有某些数据需要在目标资源中访问,
  i、一种方式是将数据保存到Session中,
  ii、另一种方式是通过请求参数来传递数据。

  示例 

<result name="success" type="redirect">/foo.jsp?id=${id}</result>
<result name="topic" type="redirect">/findTopics.actiono?topicId=${topicId}</result> 

3、redirectAction结果类型(重定向到一个Action)

  他经常用于防止表单重复提交,比方说在增加完用户之后要显示列表。redirectAction结果类型的实现类是org.apache.struts2.dispatcher.ServletActionRedirectResult,该类是ServletRedirectResult的子类,因此我们也就可以判断出redirectAction结果类型和redirect结果类型的后台工作原理是一样的,即都是利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。

<result name="topic" type="redirectAction">   
<param name="actionName">findTopics</param>
<param name="namespace">/</param>   
<param name="topicId">${topicId}</param>   
</result>  

4、chain结果类型(action链)

  a. 使用chian的方式,后面的Action会和前面的Action共用同一个ActionContext
  b. 名称为“chain”的ResultType在配置的时候,除了前面示例中的actionName外,还有一个参数,名称为“namespace”,表示被链接的Action所在包的命名空间,如果不设置,默认的即是当前的命名空间。

  <result name="runTimeEx" type="chain"> 
          <param name="actionName">errorProcessor</param>  
  <param name="namespace">/</param>

PS:chain有一个坑需要注意的是action1传给action2时除了拦截器chain起作用外,表单处理拦截器param也会起作用,而且param在chain之后起作用,故如果param和chain都给同一个参数赋值的话,param会覆盖chain的效果,例如表单提交了一个c, action1里对c进行了处理, 处理结果还是保留在c上,接下来你想把处理后的c传给action2处理,action2也用c来接收,这时候就会出现action2中的c的值是表单提交过来的值而不是action1传过来的值,给你一种参数没传递的错觉,其实是被覆盖了,一般我的做法是action1中提供getB方法返回c的值,action2中不要提供setC而是提供setB来给c赋值。参考:https://blog.csdn.net/randomnet/article/details/8656759

其他几种就先不写啦,后续如有需要再补充了~~

最后说一句,不要在浏览器里面直接传递中文参数!!! 不要在浏览器里面直接传递中文参数!!! 不要在浏览器里面直接传递中文参数!!! 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章