【web開發學習筆記】Structs2 Action學習筆記(三)action萬用字元的使用

來源:互聯網
上載者:User

標籤: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}的選擇根據*的順序決定。

六:結論約定的好的話,配置極其簡單。通過第五步的討論,當我們繼續添加新類之後,配置不需要修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.