struts2常用的结果类型有dispatcher(默认),chain,redirect,redirectAction。在很多时候也用到struts2的异常处理。所谓的动态结果是指在程序执行期间才能确定要访问的url,这可以在配置文件使用ognl表达式。
redirect,redirectAction结果类型与如何传递参数的例子:
<!-- 重定向 --> <action name="helloDeleteR" class="cn.enjoylife.prac.action.HelloAction" method="delete"> <result name="success" type="redirect">/success.jsp</result> </action> <!-- 传递参数的两种方式与redirectAction(重定向到action)结果类型的使用 --> <action name="helloDeleteRA" class="cn.enjoylife.prac.action.HelloAction" method="delete"> <result name="success" type="redirectAction"> <param name="actionName">helloList</param> <param name="namespace">/</param> <!-- 通过这个节点可以配置要传递的参数,name=参数名 ${}是参数值,是ognl表达式,里面的内容为上面HelloAction中的属性 多个param节点可以传递多个参数 --> <param name="hello">${hello}</param> </result> </action> <action name="helloDeleteRA2" class="cn.enjoylife.prac.action.HelloAction" method="delete"> <result name="success" type="redirectAction"> <!-- 也可以通过以下的方式来传递多个参数,但是&一定要转义 --> <param name="actionName">helloList?id=${hello}&choose=true</param> <param name="namespace">/</param> </result> </action>
常用通配符映射
说明:
* 匹配0个或多个字符,但(/)字符不匹配
**匹配0个或多个字符,但包括(/)字符 (感觉不太常用)
{1}代表一个占位符,与news_*中的*相对应
如果是action的name=*_*,则{1}对应第一个*,{2}对应第二个 *,依此类推。
如果访问/news_list.action,则会执行HelloAction中的list或doList方法,这时候method=list,会跳转到listSucc.jsp页面
优先级问题:框架优先选择没有使用通配符的action映射
配置片段:
<action name="news_*" class="cn.enjoylife.prac.action.HelloAction" method="{1}"> <result name="success" >/{1}Succ.jsp</result> </action>
action代码片段:
public String doList() throws Exception { System.out.println("list..."); return SUCCESS; } public String doDelete() throws Exception { System.out.println("delete..."); return SUCCESS; }