<results>标签在Struts2 MVC框架中扮演视图的角色。Action负责执行业务逻辑,下一步就是使用<results>标签显示视图。通常action会配置多个可能的结果字符串以及多个不同的视图来渲染一个处理结果,
Struts提供了许多预定义的结果类型,我们已经看到的是默认的结果类型dispatcher,它用于分发到JSP页面。Struts允许你使用其他标记语言为视图技术呈现结果,较常选用的包括Velocity,Freemaker,XSLT和Tiles。
- dispatcher
默认的类型,相当于servlet的foward,服务器端跳转。客户端看到的是struts2中配置的地址,而不是真正页面的地址。一般用于跳转到jsp页面
<result name="getlist">WEB-INF/views/holidayApply/list.jsp</result>
- redirect
重定向到一个jsp或者action或者外部网址 ,被跳转的页面中丢失传递的信息,如request
<result name="success" type="redirect">/index.jsp</result>
<result name="success" type="redirect">/login.do</result>
<result name="success" type="redirect">http://www.baidu.com</result>
//带参数
<result name="success" type="redirect">/login.do?userId=${userId }</result>
- chain
用来处理Action链,被跳转的action中仍能获取上个页面的值(将请求转发给一个Action,Action能通过getAttribute(“uname”)拿到值 ),如request信息。
<action name="action1" class="org.Action1">
<result name="success" type="chain">do</result>
</action>
<action name="action2" class="org.Action2">
<result name="success">login.jsp</result>
</action>
- redirectAction
重定向到一个Action ,跳转的页面中丢失传递的信息,如request
<result name="returnday" type="redirectAction">
<param name="actionName">holidayApplyAction_getlist.action</param>
</result>
- redirect-action
重定向到一个Action ,跳转的页面中丢失传递的信息,如request
<result name="success" type="redirect-action">
<param name="actionName">login</param> //重定向action名
<param name="userId">userId</param> //带的参数
</result>
- stream
向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据
<result name="excel" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
- freemaker
处理FreeMarker模板
- httpheader
控制特殊HTTP行为的结果类型
- velocity
处理Velocity模板
- xslt
处理XML/XLST模板
- plainText
显示原始文件内容,例如文件源代码
- plaintext
显示原始文件内容,例如文件源代码
<result name="err" type="plaintext">
<param name="location">具体的位置</param>
<param name="charSet">字符规范(如GBK)</param>
</result>
注:redirect与redirect-action区别
一、使用redirect需要后缀名 使用redirect-action不需要后缀名
二、type="redirect" 的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的 action,因此它可以省略.action的后缀直接写action的名称。