標籤:style blog java 使用 os 2014
action學習筆記3-有關於萬用字元的討論
使用萬用字元,將配置量降到最低,不過,一定要遵守"約定優於配置"的原則。
一:前端htm
<前端代碼html></head><body><a href="<%=context %>/actions/Studentadd">新增學生</a><a href="<%=context %>/actions/Studentdelete">刪除學生</a><a href="<%=context %>/actions/Teacher_add">添加老師</a><a href="<%=context %>/actions/Teacher_delete">刪除老師</a></body></html>
二:struct.xml
//struct.xml<struts> <constant name="struts.devMode" value="true" /> <package name="actions" extends="struts-default" namespace="/actions"> <action name="Student*" class="com.struts2.action.StudentAction" method="{1}"> <result>/Student{1}_success.jsp</result> </action> <action name="*_*" class="com.struts2.action.{1}Action" method="{2}"> <result>/{1}_{2}_success.jsp</result> </action> </package></struts>三:類包
//structs2調用的類包package com.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class StudentAction extends ActionSupport {public String add() {return SUCCESS;}public String delete() {return SUCCESS;}}四:過程分析
執行步驟:點擊<a href="<%=context %>/actions/Studentadd">新增學生</a> -> 通過struct.xml尋找/actions/Studentadd -> 沒有相匹配的action,則與namespace裡面的Student*匹配 -> 調用類class="com.struts2.action.StudentAction -> 執行方法的選擇,Studentadd與Student*匹配後,*就表示為add,{1},{2}的選擇之後討論。
五:討論添加一個新類
package com.bjsxt.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class TeacherAction extends ActionSupport {public String add() {return SUCCESS;}public String delete() {return SUCCESS;}}
當我們點擊<a href="<%=context %>/actions/TeacherAdd">添加老師</a> -> 通過struct.xml尋找/actions/TeacherAdd-> 沒有相匹配的action,則與namespace裡面的*_*匹配 -> 調用類class="com.struts2.action.{1}Action -> 執行方法的選擇,Teacher與*_*中第一個星號匹配後,第二個*就表示為add,{1},{2}的選擇根據*的順序決定。
六:結論約定的好的話,配置極其簡單。通過第五步的討論,當我們繼續添加新類之後,配置不需要修改。