1. 通配符的应用
一个小万能公式,解决大量的sturts.xml中的action创建。*,?,% 这些都是通配符。
第一种用法:解决同一个Action中多个方法问题
<!-- 说白了,就是一个方法名,一个action。使用通配符[*]解决一个Action中定义多个方法的问题! name="*" * 表示通配符:匹配的form表单中action中的名称 method="{1}" 表示取得到*的值 1:代表第一个*的值!如果后续再有* 则应该是{2} *:通配符,如果有精确则走精确地址匹配! --> <action name="*" class="com.bjsxt.action.LoginAction" method="{1}"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> |
第二种方式:解决不同Action中不同方法问题
Struts.xml
<!-- 解决多个action 中多个方法名问题 具体通配符配置方式:随意,随意的前提是必须有两个*一个表示Action、一个表示方法 name="*_*" 第一个*表示Action ,第二个*表示方法名。 *_* :表示前台页面form表单中action提交的具体格式。 --> <action name="*_*" class="com.bjsxt.action.{1}" method="{2}"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> |
Login.jsp
<center> <h1>登录</h1> <form action="DoLoginAction_login2.action" method="post"> userName: <input type="text" name="user.name"/><br> password: <input type="password" name="user.pwd"><br> <!-- springMVC 和 struts2 一个道理!都是通过对象.调用 --> address: <input type="text" name="user.adr.name"><br> <input type="submit" value="login"/> </form> </center> |
使用el表达式解决多个方法返回页面问题
<action name="*_*" class="com.bjsxt.action.{1}" method="{2}"> <result>${page}</result> <result name="redirect" type="redirect">${page}</result> <result name="redirectAction" type="redirectAction">${page}</result> <result name="chain" type="chain">${page}</result> </action> |
Action: getter,setter方法必须有。同时给page赋值。
public class LoginAction extends ActionSupport { // 通过对象方式取得到前台页面的数据 private User user; // 声明一个page变量 private String page; if ("admin".equals(user.getName())) { page = "/page.jsp"; return SUCCESS; } else { return ERROR; } |