struts2几种result type探究

可以在struts2-core-{version}.jar中找到struts-default.xml,里面列举了当前可以使用的所有result-type以及对应的class

此处是struts2.2.3的

        <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>

1.chain

该类型是请求转发给其他的action,如果为jsp则会报错

需要注意的就是与redirect的区别,请求转发是还在当前请求,

而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了!

下面是apache给的示例,这个比较简单,没什么说的,

<package name="public" extends="struts-default">
    <!-- Chain creatAccount to login, using the default parameter -->
    <action name="createAccount" class="...">
        <result type="chain">login</result>
    </action>

    <action name="login" class="...">
        <!-- Chain to another namespace -->
        <result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
    </action>
</package>

2.dispatcher
请求转发视图资源,比如jsp,html,如果请求action会找不到资源,
请求转发的问题看1中的说明
apache给的示例。。也没啥说的
<result name="success" type="dispatcher">
  <param name="location">foo.jsp</param>
</result>


3.httpheader
这个挺有意思的,可以随便更改响应状态
比如下面的这个示例
		<action name="test" class="com.iss.action.TestAction">
 			<result name="success" type="httpheader">
				<param name="status">400</param>
			</result>
 		</action>

当action返回SUCCESS的时候,会将响应状态修改为400,客户端错误的请求,
还有其他的状态可以自行尝试,比如为100时,浏览器会在请求一段时间之后继续当前页面,500则为服务器内部错误等等
具体为什么会产生这样的结果,当然得把源码翻出来了
下面是HttpHeaderResult的一些成员变量,private的可以作为param的name属性
    private static final long serialVersionUID = 195648957144219214L;

    /** The default parameter */
    public static final String DEFAULT_PARAM = "status";


    private boolean parse = true;
    private Map<String,String> headers;
    private int status = -1;
    private int error = -1;
    private String errorMessage;

下面是具体执行操作过程,可以看到response.setStatus等操作

public void execute(ActionInvocation invocation) throws Exception {
        HttpServletResponse response = ServletActionContext.getResponse();
        ValueStack stack = ActionContext.getContext().getValueStack();
        
        if (status != -1) {
            response.setStatus(status);
        } else if (error != -1) {
            if (errorMessage != null) {
                String finalMessage = parse ? TextParseUtil.translateVariables(
                    errorMessage, stack) : errorMessage;
                response.sendError(error, finalMessage);
            } else
                response.sendError(error);
        }

        if (headers != null) {
            for (Iterator iterator = headers.entrySet().iterator();
                 iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String value = (String) entry.getValue();
                String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
                response.addHeader((String) entry.getKey(), finalValue);
            }
        }
    }

4.redirect

重定向操作,与请求转发的区别看1的介绍

让客户端请求另外的网络资源,可以为action,也可以为视图资源

下面是apache的示例

<result name="success" type="redirect">
  <param name="location">foo.jsp</param>
  <param name="parse">false</param>
</result>

5.redirectAction

重定向至Action,与redirect的区别找了好久,但是也没发现比较权威的说明

最明显的区别当然是redirectAction只能请求action,如果请求视图资源会报错

然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会

有说redirectAction无法通过url传参,但是我测试时完全可以通过request获取到,

此处区别如果有哪位知道的麻烦告知,Thank you!

下面是apache的传参的示例

<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.action?reportType=pie&width=100&height=100
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect-action">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
      </result>
   </action>
</package>

6.stream

这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档

此处给一个显示PDF文档示例

项目web.xml中

      <mime-mapping>  
            <extension>pdf</extension>  
            <mime-type>application/pdf</mime-type>  
        </mime-mapping>  

struts.xml中

<action name="test" class="com.iss.action.TestAction">
			<result name="success" type="stream">
				<param name="contentType">application/pdf</param>
				<param name="inputName">inputStream</param>
				<param name="contentDisposition">filename="a.pdf"</param>
			</result>
		</action>

TestAction.java中

public class TestAction extends ActionSupport
{
	
	private InputStream inputStream;


	public InputStream getInputStream()
	{
		return inputStream;
	}

	@Override
	public String execute() throws Exception
	{
		inputStream = new FileInputStream("xxxxx/a.pdf");//要下载或显示的文件路径
		return SUCCESS;
	}
}
需要注意的地方是struts.xml中inputName的值要与TestAction中的get方法名相关,如在此处是getInputStream,inputName则为inputStream


7.plainText

响应以plain形式返回给客户端

截取源码的最重要的一部分

 if (charset != null) {
            response.setContentType("text/plain; charset="+charSet);
        }
        else {
            response.setContentType("text/plain");
        }

8.freemarker,velocity,xslt

没有尝试过,有兴趣的可以尝试下


写得很累。。。

转载请注明出处:http://blog.csdn.net/zhouyuqwert/article/details/6765926

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值