一、Struts的Action的一些屬性的具體意思?
Struts中Action的一段定義如下
<action>
attribute="aaForm"
input="/aa.jsp"
name="aaForm"
path="/aa"
scope="request"
type="com.yourcompany.struts.action.AaAction">
<forward name="aa" path="/aa.jsp" />
</action>
其中的attribute和name是什麼意思代表什麼、<forward name="aa" path="/aa.jsp" />這個中的name又代表什麼、轉寄的名字嗎?
Action能轉寄多個jsp嗎?正確一個、錯誤的時候轉給另一個。應該怎麼寫
二、struts-config.xml檔案
/*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="cn.hxex.sample.struts.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
input="/form/user.jsp"
name="userForm"
path="/user"
scope="request"
type="cn.hxex.sample.struts.action.UserAction" >
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
<message-resources parameter="cn.hxex.sample.struts.ApplicationResources" />
</struts-config>
*/
三、Struts中Action的屬性詳解
/*
attribute:指定ActionForm儲存到指定上下文所使用的屬性名稱,如果不指定attribute屬性值,
將使用name屬性值作為儲存時的屬性名稱。即attribute的預設值就是name屬性值。
input: 該Action中相關ActionForm擷取使用者輸入的輸入頁面,當將ActionForm設為自動驗證輸入資料,發現不合法資料返回錯誤時,將返回該頁面
name: 當前Action中用到的ActionForm的名字,其具體資訊在設定檔其他地方另有詳細定義
scop:指定儲存ActionForm內容相關的範圍。即Action中所用到的ActionForm的生存期,可以為“request”或“session”,隨著生存期的設定,該Action也會在相應的時間被建立
validate:如果本屬性為true則在Action動作之前其對應的ActionForm的validate方法會自動被調用,一般用以驗證使用者輸入的資料
forward:設定處理使用者請求的serverlet或其它資源如Jsp頁面等。如指定了這屬性則type屬性就會失去作用
嚴格來說<forward,include,type>屬性應該使用且只使用其中的一個。 這屬性用於跳轉到另一個非Action處理常式中。
unknown: 如果將該屬性設定為true,那麼就是聲明這個Action將處理整個應用中所有未找到相應處理Action的請求,當然,一個應用系統中也只會有一個Action的unknown屬性可以設為true
Prefix:用來匹配請求參數與bean屬性的首碼
Suffix: 用來匹配請求參數與bean屬性的尾碼
*/
與scope有關,比如scope="request" 時,可以通過request.getAttribute(attribute的值)來擷取Form對象
而name對應於前面form的定義。forward 中的name可以說是頁面的一個別名,轉寄時根據名字就行了
可以轉寄多個,如定義了兩個forward success和error
<forward name="success" path="/aa.jsp" />
<forward name="error" path="/bb.jsp" />
try
{
……
}
catch(Exception)
{
……
return mapping.findFord("error");
}
return mapping.findFord("success");