struts2 result type类型

result标签中type的类型

类型 说明
chain 用于Action链式处理
dispatcher 用于整合JSP,是<result>元素默认的类型
freemarket 用来整合FreeMarket
httpheader 用来处理特殊的HTTP行为
redirect 用来重定向到其它文件
redirectAction 用来重定向到其它Action
stream 用来香浏览器返回一个InputStream
velocity 用来整合Velocity
xslt 用来整合XML/XSLT
plainText 用来显示页面的原始代码

result的type类型定义在struts-default.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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

1.dispatcher结果类型

  dispatcher结果类型用来表示“转发”到指定结果资源,它是struts2的默认结果类型

  (1)建一个test.jsp页面,内容如下:

    <s:form method="post" action="/Login_toLogin">
        <s:textfield name="username" label="用户名"/>
        <s:password name="password" label="密码"/>
        <s:submit></s:submit>
    </s:form>

  (2)在src/action下建一个类TestAction

package action;

public class TestAction {

    private String username;
    private String password;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute(){
        return "success";
    }
}

  TestAction类中封装了两个属性username和password

  (3)配置struts.xml文件中action

    <action name="test" class="action.TestAction">
        <result type="dispatcher">/testSuccess.jsp</result>
    </action>

  (4)新建一个testSuccess.jsp

    用户:<s:property value="username"/><br/>
    密码:<s:property value="password"/>

输出结果:

  使用dispatcher记过类型是,由于只是将结果转发到指定资源,所以能够暴力会请求的信息,而且在使用浏览器地址显示为test.action而不是testSuccess.jsp

2、redirect结果类型

  redirect结果类型用来“重定向”,到指定的结果资源,该资源可以是JSP文件,也可以action类。使用redirect结果类型时,系统将调用HttpServletResponse的sendRedirect()方法。

  还是上面的例子,只是在struts.xml中,修改result元素的结果类型为redirect,代码如下:

<action name="test" class="action.TestAction">
        <result type="redirect">/testSuccess.jsp</result>
</action>

  结果在url不在test.action,而是testSuccess.jsp。由于redirect使得浏览器再一次发出请求,所有原来的请求资源将不会存在,所以使用<s:property value="username"/>是无法获取到请求数据的

3、stream结果类型

  stream表示流,这种结果类型通常用于实现用户下载文件的Action配置中,参数有:

  (1)contentType:用来指定床底给浏览器stream类型。默认为text/plain

  (2)contentLength:指定数据了的字节长度

  (3)contentDiposition:指定文件下载的处理方式,包括内联(inline)和附件(attachment)这两种方式,内联方式表示浏览器会尝试直接显示文件,附件方式会弹出文件保存对话框,默认值为inline。

  (4)inputName:表示数据了属性。默认值为inputStream

  (5)bufferSize:表示缓冲区容量。默认为1024

原文地址:https://www.cnblogs.com/caoyc/p/5580783.html