在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