Struts-config.xml設定檔《action-mappings》元素的詳解

來源:互聯網
上載者:User

標籤:

action-mappings

       該元素用於將Action元素定義到ActionServlet類中,它含有0到多個<action/>元素,其格式如下:

<action-mappings>

<action path="Action請求的相對路徑,與頁面<html:form>的Action屬性值一致"

type="該Action的對應類的全路徑"

name="該Action綁定的FormBean,與<form-bean >的Name屬性值一致"

<forward name="與Action類中mapping.findForward("mapname")返回的mapname值一致" path="頁面跳轉的相對路徑"/>

</action>

</action-mappings>

       每個action子項目可包含一個或多個forward子項目。除了path、type和name屬性外,action還具有如下屬性:

l         scope:指定ActionForm Bean的範圍(session和request),預設為session。(可選);

l         input:當Bean發生錯誤時返回的路徑,在validate驗證架構中錯誤顯示的頁面(可選);

l         classname:指定一個調用這個Action類的ActionMapping類的全名。預設用org.apache.struts.action.ActionMapping(可選);

l         include:如果沒有forward的時候,它起forward的作用(可選);

l         validate:若為true,則會調用ActionForm的validate()方法或調用validate驗證,否則不調用,預設為true(可選)。

forward屬性也是可選的。

action元素定義舉例如下:

Example1.

Eg2. 有input屬性的例子:

 
  1. <action-mappings>
  2. <action   
  3.   
  4.      path="/userAction"   
  5.   
  6.      type="com.amigo.struts.action.UserAction"   
  7.   
  8.      name="UserForm"  
  9.   
  10.      scope="request"  
  11.   
  12.      validate = "false"  
  13.   
  14.      parameter="method" >   
  15.   
  16.      <forward name="error" path="/user/error.jsp" />  
  17.   
  18.      <forward name="success" path="/user/success.jsp"/>  
  19.   
  20.      <forward name="add" path="/user/addUser.jsp"/>  
  21.   
  22.      <forward name="update" path="/user/updateUser.jsp"/>  
  23.      <forward name="list" path="/user/userList.jsp"/>  
  24.   
  25.     </action>  
  26.   
  27. </action-mappings></p>  

 

Eg3. 僅有JSP的action元素:


 
  1. <action-mappings>  
  2.   
  3.     <action path="/calcAction"   
  4.   
  5.     type="com.amigo.struts.action.CalcAction"  
  6.   
  7.     name="CalcForm"  
  8.   
  9.     scope="request"  
  10.   
  11.     validate="true"  
  12.   
  13.     input="/index.jsp">  
  14.   
  15.     <forward name="success" path="/success.jsp"/>  
  16.     <forward name="error" path="/error.jsp"/>  
  17.   
  18.     </action>  
  19.   
  20. </action-mappings>  

 

首先,ActionServlet接到請求後調用ForwardAction的execute()方法,execute()根據配置的parameter屬性值來forward到那個URI。


 
  1. <action path="/menu"   
  2.   
  3.     parameter="/default.jsp"   
  4.   
  5.     type="org.apache.struts.actions.ForwardAction" />  

 

這樣做的效果是:沒有任何form被執行個體化,比較現實的情形可能是form在request更進階別的範圍中定義;或者這個action被用作在應用程式編譯好後充當系統參數,只需要更改這個設定檔而不需要重新編譯系統。

//轉自CSDN:http://blog.csdn.net/zhoukang0916/article/details/4210029

Struts-config.xml設定檔《action-mappings》元素的詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.