在struts.xml文件中,result节点是action节点的子节点,其代表action方法执行后可能去的一个目的地,即一个action节点可以配置多个result子节点;其name属性对应着action方法的一个返回值,而type属性表示结果的响应类型。
一、结果类型
常用的结果响应类型主要有转发、重定向、转发到Action和重定向到Action,其在struts-default.xml文件中的定义如下:
<result-types>
<!--常用4:转发到Action,即将控制权转发给后一个Action,而前一个Action的状态在后一个Action中依然保持注意其不能通过dispatcher的方式转发到Action-->
<!--注意:其不能通过dispatcher的方式转发到Action-->
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<!--常用1:转发(默认值),即将控制权转发给应用程序中的指定资源,不能转发给外部资源-->
<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"/>
<!--常用2:重定向,即将响应重定向到另一个资源(可以是外部资源)-->
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<!--常用3:重定向到Action,可设置actionNames、namespace、parse等参数-->
<!--注意:通过redirect也可间接重定向到Action,如/namaspace/actionName.do-->
<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>
注意:可以采用另种方式重定向到Action,示例代码如下:
<!-- 重定向到Action 方式一 --!>
<result name="index" type="redirectAction">
<param name="actionName">TestAction</param>
<param name="namespace">/qiaobc</param>
</result>
<!-- 重定向到Action 方式二 --!>
<result name="index" type="redirect">/qiaobc/TestAction.do</result>
二、通配符映射
WEB应用中可能有成百上千个action声明,可以利用Struts2提供的通配符映射机制把多个彼此相似的映射关系进行简化合并。
通配符映射规则:
- 若找到多个匹配,没有通配符的匹配将胜出;
- 若指定动作不存在,Struts2将尝试把该URI与包含通配符“*”的动作名进行匹配 ;
- 被通配符匹配到的URI字符串的子串可以用{1}、{2}等来引用;其中,{1}匹配第一个子串,{2}匹配第二个子串……
- {0}匹配整个URI;
- 若Struts2找到的带有通配符的匹配不止一个,则按先后顺序进行匹配;
- 可以匹配零个或多个字符,但不包括“/”字符(需要使用“**”),如果需要对字符进行转义,则需使用“\”。
<package name="struts2-app" namespace="/app" extends="struts-default">
<action name="Book_add" class="org.simpleit.app.Book" method="add">
<result>/WEB-INF/jsp/Book.jsp</result>
</action>
<action name="Book_delete" class="org.simpleit.app.Book" method="delete">
<result>/WEB-INF/jsp/Book.jsp</result>
</action>
<action name="Author_add" class="org.simpleit.app.Author" method="add">
<result>/WEB-INF/jsp/Book.jsp</result>
</action>
<action name="Author_delete" class="org.simpleit.app.Author" method="delete">
<result>/WEB-INF/jsp/Book.jsp</result>
</action>
<!-- 以上四个action节点等价于: -->
<action name="*_*" class="org.simpleit.app.{1}" method="{2}">
<result>/WEB-INF/jsp/{1}.jsp</result>
</action>
</package>
三、动态方法调用
动态方法调用是通过URL动态调用Action中的方法;默认情况下,Struts2的动态方法调用功能处于禁用状态,需要通过配置常量的方式打开。
如:“/struts-app2/Product.action”调用Product类的execute()方法,而可以通过“/struts-app2/Product!save.action”来直接调用Product类的save()方法。