struts2.xml 中的result标签type属性

在struts-default.xml 中可以找到如下type的参数

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

其中redirect表示重定向, dispatcher表示转发


列子1:

  • jsp页面提交一个name,在struts.xml中将type配置dispatcher在将信息传递到另一个jsp中显示
    jsp提交一个name:
  •   <body>
        <form action="${pageContext.request.contextPath}/form1.action">
            name:<input name="name">
            <input type='submit'>
        </form>
      </body>

    struts.xml type定义为dispatcher

         <package name="demo2" extends="struts-default" namespace="/">
            <action name="form1" class="form.Form1Action">
                <result name="success" type="dispatcher">
                    Hello.jsp
                </result>
            </action>
         </package>

    Form1Action类

    public class Form1Action extends ActionSupport{
        String name;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Form1Action() {
            System.out.println("执行。。。");
        }
        public String execute() throws Exception {
    
            return Action.SUCCESS;
        }
    }

    在Hello.jsp中接受参数

      <body>
        name=${name }<br>
        name=${requestScope.name}
      </body>

    结果: Hello.jsp成功输出印name


    列子2:

  • 将type定义为redirect,其他同上
    结果: 因为是重定向所以, Hello.jsp无法显示name
  • 但可以使用url带参数形式将值传递到Hello.jsp上,修改struts.xml如下:

         <package name="demo2" extends="struts-default" namespace="/">
            <action name="form1" class="form.Form1Action">
                <result name="success" type="redirect">
                    <param name="location">Hello.jsp</param>
                    <param name="name">${name}</param>
                </result>
            </action>
         </package>

    结果: 重定向到Hello.jsp中 ,url为http://localhost:8080/struts2/Hello.jsp?name=tom

    还有其他属性,可以参考这 : http://espace.iteye.com/blog/1546542

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值