struts2结果类型

来源:互联网 发布:mysql 查看表锁情况 编辑:程序博客网 时间:2024/06/01 14:03

在struts2-core-2.3.1.1.jar/struts-defalut.xml配置文件下可以看到:

<package name="struts-default" abstract="true">        <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-types>
        ..........省略
</package>

chain:两个action之间的转发

dispatcher:action转发到jsp

freemarker:处理freemarker用的

httpheader:处理特殊http

redirect:action重定向到jsp

stream:以InputStream流的方式发送到浏览器

velocity:处理velocity模板

xslt:处理xml模板

plainttext:以源代码的形式展示给浏览器

对于转发以及跳转到JSP视图的结果类型:

 <result name="FromAction" type="dispatcher">
<!--  这里是jsp路径 -->
        /index.jsp
 </result>

对于转发以及跳转到Action的结果类型,可以指定三个参数:命名空间,action,方法

除了action参数,命名空间默认是"/",方法默认是action中指定的method参数.

                        <result name="FromAction" type="chain"><!-- 转发到的ToAction所在namespace属性值,默认为当前namespace--><param name="namespace">/app01</param><!-- 转发到的ToAction所在<action>标签的name属性值 --><param name="actionName">toRequest</param><!-- 转发到的ToAction所对应处理类的哪个控制方法,默认为ToAction所在<action>标签的method属性值 --><param name="method">toMethod</param></result>

对于plaintext方式,需要指定两个参数:

        <result name="success" type="plainText">              <param name="location">/index.jsp</param>              <param name="charset">GBK</param>          </result>   


对于Stream方式,指定流的内容类型,以及流的获取源.

<result name="downloadOK" type="stream"><!-- 设置下载的内容,默认为text/plain格式 --><param name="contentType">image/jpeg</param><!-- 调用Action的getImageStream()方法,获取InputStream --><param name="inputName">imageStream</param></result>

如果需要让浏览器提示下载,需要改一下http响应头:
<param name="contentDisposition">attachment;filename=${filename}</param>

${filename}会读取action中的filename实例变量.


在struts.xml配置文件中,<result>一般存在于<ation>内,此时的result是在当前action中有效,对其他action无效的,若想要实现一个对所有action都有效的result,可以在action外声明一个全局的result结果:

<!-- 全局结果 --><global-results><result name="global" type="redirect">/index.jsp</result></global-results>
注意:全局结果类型要写在拦截器声明下面,<action>上面,就比如这样:

                <!-- 定义拦截器 --><interceptors><!-- 我的拦截器 --><interceptor name="myInterceptor" class="yzr.MyInterceptor"/></interceptors><!-- 全局结果 --><global-results><result name="global" type="redirect">/index.jsp</result></global-results><action name="toRequest" class="yzr.ToAction" method="toMethod"><!-- 引入拦截器 --><interceptor-ref name="myInterceptor"/><interceptor-ref name="defaultStack"/><!-- 局部结果 --><result name="toResult" type="redirect">/result.jsp</result></action>


0 0
原创粉丝点击