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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
resultType是Mybatis中用于配置查询语句返回结果类型的属性。它可以指定返回结果的数据类型,例如整型、字符串等。当返回结果是基本数据类型时,可以直接在resultType属性中配置对应的类型,如int。当返回结果是对象时,可以在resultType属性中配置实体类所在的包位置,例如"domain.User"。\[1\]\[3\]在查询映射时,Mybatis会将查询结果放在一个Map中,其中键是属性名,值是对应的属性值。当resultType属性被指定时,Mybatis会将Map中的键值对赋给resultType所指定的对象的属性。如果返回结果需要进一步转化为对象,可以使用resultMap属性来引用外部的ResultMap。\[2\]总之,resultType属性用于指定查询语句的返回结果类型。 #### 引用[.reference_title] - *1* *3* [详解Mybatis中的resultTyperesultMap](https://blog.csdn.net/cm_mc_cm_mc/article/details/120635780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ResultTypeResultMap的用法和区别](https://blog.csdn.net/xushiyu1996818/article/details/89075069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值