struts.xml中的配置: <!-- 配置用戶模塊的action --> <action name="user_*" class="userAction" method="{1}"> <result name="registPage">/WEB-INF/jsp/regist.jsp</result> </action> /* 此時method={1}中的{1}代表user_*中的*,即加入你訪問路徑是/user_registPage.action,則此刻訪問的是該Action中的registPage方法。同理,如果通配符* == delete,則就訪問的是delete方法。 當name中含有多個通配符的時候,method={2} ,就代表第二個通配符,同理以此類推。 這種方式更靈活的簡化了struts.xml的配置文件。 */ regist.jsp頁面中的代碼段: <li id="headerRegister" class="headerRegister" style="display: list-item;"> <a href="${ pageContext.request.contextPath }/user_registPage.action">注冊</a>| </li> /* user_registPage.action對應 name="user_*",通配符*就是registPage, method="{1}"里面的{1}就是代表registPage()方法, 這個registPage()方法存在於 UserAction中 */ Action類: public class UserAction extends ActionSupport{ /** * 跳轉到注冊頁面的執行方法 */ public String registPage(){ return "registPage"; } }
<action name="*_*" method="{2}" class="action.{1}Action">
上面代碼定義了一個模式為*_*的action,只要匹配該模式的請求,都可以被該Action處理。
如果有URL為BOOK_save.action請求,因為匹配了*_*模式,第一個*為BOOK,第二個8為save,
則調用action.BOOKAction處理類中的save()方法來處理用戶請求。