html:base雖然這裏的標題是html:base標籤,但是這裏也是這篇指南要介紹的第一個Struts html標籤。因此我想在這裏從整體上簡單的介紹一下,最後給出一個對照表。其中的絕大多數標籤就不一一介紹了,要介紹的也不會在這裏介紹,而是將其獨立出來以顯重要性。 從用戶處收集數據是動態web應用非常重要的一個方面,因此構建輸入表單也就自然而然的成爲struts框架的一個重要內容。Struts html標籤庫含有創建Struts輸入表單的標籤,和其它標籤庫(bean、logic、nested和tiles)中的標籤一起協作就可以產生基於html的用戶界面。 下面的對照表會使您對Struts html標籤庫有一個整體的印象:
圖示 1. Struts HTML標籤和HTML元素對照表
html:cancelhtml:cancel標籤生成一個取消按鈕。當點擊該按鈕後action servlet會繞過相應的form bean的validate()方法,同時將控制權交給相應的action。在該action中可以使用Action.isCancelled(HttpServletRequest)方法判斷是否被取消了。如果返回true表示這個action被取消了,否則表示這個action沒有被取消。 請注意,如果您修改了html:cancel標籤的property屬性值,那麼struts提供的cancel探測機制就失效了,您自己必須提供類似的機制。 下面是可取消的action的配置文件,注意<set-property property="cancellable" value="true"/>這一行,如果不添加Struts會拋出org.apache.struts.action.InvalidCancelException異常。這是我在完成本指南的過程中發現的唯一向下不兼容的地方。 <action path="/cancel" type="org.solo.struts.action.CancelAction" name="cancelForm" scope="request"> <set-property property="cancellable" value="true"/> <forward name="success" path="/cancel.jsp" /> </action> 下面是html:cancel標籤的代碼: <html:cancel>取消</html:cancel> 下面是對應的action中的代碼: if(isCancelled(request)){ //action被取消時要做的事情寫在這裏 return mapping.findForward("cancel"); }else{ //action沒有被取消時要做的事情寫在這裏 return mapping.findForward("success"); } html:selecthtml:select標籤生成一個select元素。是單選還是多選取決於該標籤的multiple屬性。如果指定了multiple="true"則爲多選,此時對應的屬性應該是一個數組。如果沒有指定multiple="true"則爲單選,此時對應的屬性應該是標量。 注意:爲了正確的處理沒有做任何的選擇的情況,在ActionForm中的reset()方法中必須將標量屬性設置爲默認值而將數組的長度置爲0。 另外的一個重要問題就是struts如何生成option元素了,這個任務struts交給了html:option、html:options和html:optionsCollection三個標籤。 html:option標籤 html:option標籤生成一個HTML的option元素。該標籤必須嵌在html:select標籤中。它的顯示文本來自其標籤體,也可以來自於資源文件。它的value屬性用來指定什麼值將要被提交。 <html:option value="one">one</html:option> <html:option value="two">two</html:option> html:options標籤 html:options標籤生成多個HTML的option元素。該標籤必須嵌在html:select標籤中。而且工作方式有些特殊,它的name與property屬性和其它標籤的name與property屬性意義不一致,讓我們具體看一下它的工作方式。
指定collection屬性 讓我通過示例來介紹在指定collection屬性時該標籤的工作方式,首先要說明selectForm中的persons和listForm中的persons完全一致。請參見bean:define標籤。 下面的代碼先利用bean:define標籤將selectForm中的persons取到page作用域中,然後html:options標籤再依據collection="personCollection"選中這個persons並將其中的每一個對象(Person類型)生成一個option元素。該標籤的property="id"表示persons中的對象(Person類型)的id屬性將作爲option元素的value值。該標籤的labelProperty="name"表示persons中的對象(Person類型)的name屬性將作爲option元素的label值。 當這個select提交時所選擇的值將被提交到selectForm(name="selectForm")中的person對象(這是在SelectForm中聲明的一個Person類型的域專門用來接收提交的值)的id屬性中(property="person.id")。 <bean:define id="personCollection" name="selectForm" property="persons"/> <html:select name="selectForm" property="person.id" size="1"> <html:options collection="personCollection" property="id" labelProperty="name"/> </html:select> 沒有指定collection屬性 讓我通過示例來介紹沒有指定collection屬性時該標籤的工作方式,先來看看ids和names的定義: private List<String> ids = null; private List<String> names = null; 上面的代碼來自SelectForm,其中ids是一個String的列表,names也是一個String的列表。我們暫時假定這兩個列表含有相同數目的元素。有了這些讓我們開始介紹下面的代碼。html:options標籤用ids中的第i個值作爲option元素的value值同時使用names中相同位置的值(第i個值)作爲option元素的label值。如果ids比names長那麼多出的ids中的值將即作爲option的value又作爲option的label。如果ids比names短那麼多出的names的值會被丟掉。 當這個select提交時所選擇的值將被提交到selectForm(name="selectForm")中的person對象(這是在SelectForm中聲明的一個Person類型的域專門用來接收提交的值)的id屬性中(property="person.id")。 <html:select name="selectForm" property="person.id" size="1"> <html:options property="ids" labelProperty="names"/> </html:select> html:optionsCollection標籤 html:optionsCollection標籤生成多個HTML的option元素。該標籤必須嵌在html:select標籤中。它的功能和html:options標籤的相同,但是它的name與property屬性和其它標籤的name與property屬性意義一致,理解起來比較自然。 讓我通過示例來介紹html:optionsCollection標籤的用法。首先依據name="selectForm"和property="persons"取到selectForm中的persons列表,然後將列表中的對象(Person類型)的id屬性作爲option元素的value值(value="id"),將列表中的對象(Person類型)的name屬性作爲option元素的label值(label="name")。 <html:select name="selectForm" property="person.id" size="1"> <html:optionsCollection name="selectForm" property="persons" label="name" value="id"/> </html:select> 下面是一個多選的示例,雖然示例中使用了html:options標籤,但是html:option和html:optionsCollection也能夠用來多選。而且您還必須意識到html:option、html:options和html:optionsCollection這三個標籤可以同時使用。代碼中的personIds是SelectForm中聲明的一個String[]類型的數組用來接收提交的多個值。 <html:select name="selectForm" property="personIds" multiple="true" size="2"> <html:options property="ids" labelProperty="names"/> </html:select> html:checkboxhtml:check標籤生成一個checkbox。這裏的value值可以是true,yes或on。如果您要提交其它的值(如某種形式的標識)應該考慮使用html:multibox標籤。 注意:爲了正確的處理沒有選中的checkbox您必須在reset()中設置對應的屬性爲false。 下面的代碼示例了html:checkbox標籤的用法,其中CheckboxForm中聲明瞭三個boolean類型的域,如下: private boolean one = false; private boolean two = false; private boolean three = false; <html:checkbox name="checkboxForm" property="one"> One </html:checkbox> <html:checkbox name="checkboxForm" property="two"> Two </html:checkbox> <html:checkbox name="checkboxForm" property="three"> Three </html:checkbox> 如果選中後被提交則相應的屬性的值爲true。 html:radiohtml:radio標籤生成一個radio。主要的用法有兩種,下面我們通過代碼來示例。 下面的代碼示例了html:radio標籤的一般用法,如果被提交則選中的radio的value值將被提交到radioForm中的id中。 <html:radio name="radioForm" property="id" value="00001"> One </html:radio> <html:radio name="radioForm" property="id" value="00002"> Two </html:radio> 下面的代碼示例了html:radio標籤的典型用法,其中的persons和bean:define標籤中的一致,您可以參考bean:define標籤。我只介紹這個<html:radio idName="person" property="id" value="id">,idName指定html:radio要使用的bean(這裏爲person),value="id"表示person的id屬性將作爲radio元素的value值而property="id"表示提交時選中的radio的值將被提交給radioForm中的id屬性。 <logic:notEmpty name="radioForm" property="persons"> <logic:iterate id="person" name="radioForm" property="persons"> <html:radio idName="person" property="id" value="id"> <bean:write name="person" property="name"/> </html:radio> </logic:iterate> </logic:notEmpty> html:multiboxhtml:multibox標籤生成多個checkbox。當您要使用大量的checkbox時使用這個標籤非常方便,可以使您避免在ActionForm中聲明大量的boolean類型的變量,帶之以一個數組就行了。 注意:爲了正確的處理沒有選中的checkbox您必須在reset()中設置數組的長度爲0。 下面的代碼示例了html:multibox標籤的一般用法,如果被提交則選中的所有checkbox的value值將被提交到multiboxForm中的selectedItems中,這是一個String[]數組。 <html:multibox name="multiboxForm" property="selectedItems" value="00001"/> <html:multibox name="multiboxForm" property="selectedItems" value="00002"/> 下面的代碼示例了html:multibox標籤的典型用法: <logic:iterate id="person" name="multiboxForm" property="persons"> <html:multibox property="selectedItems"> <bean:write name="person" property="id"/> </html:multibox> <bean:write name="person" property="name"/> </logic:iterate> html:linkhtml:link標籤生成一個錨點(<a>)元素。我從html:link標籤如何構造基本url和如何構造query參數兩個方面來介紹這個標籤。 構造基本url是依據該標籤的如下四個屬性進行的,這四個屬性一次只能出現一個。讓我們看看它們:
構造query參數,下面列舉了可能的形式,其中baseurl只是一個佔位符:
下面我們以代碼示例來說明每一種html:link的用法,首先讓我們花點時間看看相關action中的代碼: <!-- 下面所有代碼的數據都是在這裏構造的 --> public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) { DataForm dataForm = (DataForm) form; dataForm.setParam("00001"); HashMap paramSingleMap = new HashMap(); paramSingleMap.put("p1","00001"); paramSingleMap.put("p2","00002"); paramSingleMap.put("p3","00003"); dataForm.setParamSingleMap(paramSingleMap); HashMap paramMultiMap = new HashMap(); paramMultiMap.put("p",new String[]{"00001","00002","00003"}); dataForm.setParamMultiMap(paramMultiMap); HashMap paramSMMap = new HashMap(); paramSMMap.put("p1","00001"); paramSMMap.put("p2","00002"); paramSMMap.put("p",new String[]{"00001","00002","00003"}); dataForm.setParamSMMap(paramSMMap); List<Person> persons = new ArrayList<Person>(); Person person1 = new Person(); person1.setId("00001"); person1.setName("趙辰"); Person person2 = new Person(); person2.setId("00002"); person2.setName("李爲芳"); Person person3 = new Person(); person3.setId("00003"); person3.setName("王微"); persons.add(person1); persons.add(person2); persons.add(person3); dataForm.setPersons(persons); return mapping.findForward("success"); } <!-- 這用來設置一個位置 --> <html:link linkName="top"/> <!-- 這用來定位到上面的那個位置 --> <html:link page="/link.do" anchor="top">Go Top</html:link> <html:link page="/link.do" paramId="p" paramName="dataForm" paramProperty="param"> 單參單值 </html:link><br/> <html:link page="/link.do" name="dataForm" property="paramSingleMap"> 多參單值 </html:link><br/> <html:link page="/link.do" name="dataForm" property="paramMultiMap"> 單參多值 </html:link><br/> <html:link page="/link.do" name="dataForm" property="paramSMMap"> 混合 </html:link> 下面的代碼示例了html:link標籤的indexed屬性和indexId屬性的用法,這兩個屬性只有html:link標籤嵌套在logic:iterate標籤中時纔可用。 <logic:iterate id="person" name="dataForm" property="persons"> <html:link action="/link.do" paramId="person" paramName="person" paramProperty="id" indexed="true" indexId="number"> person </html:link> <br/> </logic:iterate> <!-- 下面是上面代碼的運行結果(產生的html) <a href="/struts-demo/link.do?person=00001&number=0">趙辰</a><br/> <a href="/struts-demo/link.do?person=00002&number=1">李爲芳</a><br/> <a href="/struts-demo/link.do?person=00003&number=2">王微</a><br/> 其中的number是由indexId="number"確定的,而該參數的值爲元素在集合中的位置。 --> html:rewrite標籤和html:link標籤類似只是不生成錨點(<a>),而是簡單的輸出字符串。 html:errorshtml:errors標籤和html:messages標籤的功能相似,所以我們放到一起來介紹。 html:errors標籤將由name屬性指定的ActionMessages、ActionErrors、String和String[]直接輸出到頁面中。 html:messages標籤將用由name屬性(注意message屬性值對它的影響)指定的ActionMessages、ActionErrors、String和String[]創建一個新的屬性和scripting變量,使用id屬性值作爲名稱。 html:errors標籤和html:messages標籤的property屬性是用來爲errors和messages分類的。我們可以給這兩個標籤指定property屬性,以便只顯示某一類的錯誤或消息。 在資源文件增加了如下的內容: # -- standard errors -- errors.header=<ul> errors.prefix=<li> errors.suffix=</li> errors.footer=</ul> error=error with none value . error1=error1 with one value is {0} . error2=error2 with two values are {0} , {1} . error3=error3 with three values are {0} , {1} , {2} . error4=error4 with four values are {0} , {1} , {2} ,{3} . 下面的代碼示例了actionErrors的構造: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors actionErrors = new ActionErrors(); actionErrors.add("property1", new ActionMessage("error")); actionErrors.add("property2", new ActionMessage("error1","value0")); actionErrors.add("property2", new ActionMessage("error2","value0","value1")); actionErrors.add("property3", new ActionMessage("error3","value0","value1","value2")); actionErrors.add("property3", new ActionMessage("error4","value0","value1","value2","value3")); actionErrors.add("property4", new ActionMessage("error1",new Object[]{"value0"})); actionErrors.add("property4", new ActionMessage("error2",new Object[]{"value0","value1"})); actionErrors.add("property4", new ActionMessage("error3",new Object[]{"value0","value1", "value2"})); actionErrors.add("property5", new ActionMessage("error4",new Object[]{"value0","value1", "value2","value3"})); actionErrors.add("notBundle", new ActionMessage("not a bundle key",false)); return actionErrors; } errors標籤代碼示例: <html:errors/> <br/> <html:errors property="property4"/> messages標籤代碼示例: <logic:messagesPresent> <ul> <html:messages id="message"> <li><bean:write name="message"/></li> </html:messages> </ul> </logic:messagesPresent> <br/> <logic:messagesPresent> <ul> <html:messages id="message" property="property4"> <li><bean:write name="message"/></li> </html:messages> </ul> </logic:messagesPresent>
圖示 2. 上面的兩段代碼示例運行結果相同,如下所示:
html:javascripthtml:javascript標籤生成用於校驗的javascript腳本代碼。 下面的代碼片段示例了html:javascript標籤的基本用法,其中formName屬性的值是在validation.xml文件的<formset>中定義的form的名稱。有一點值得注意的是在確定<formset>時要使用合適的language屬性值。 <html:javascript formName="dataForm"/> html:xhtmlhtml:xhtml標籤在頁面中一出現就是告訴該頁的所有其它的Struts html標籤以XHTML1.0兼容的形式輸出。這和將html:html標籤的xhtml屬性值指定爲true有些類似。如果上述情況都沒有出現,Struts html標籤將以html4.01兼容的形式輸出。 |
Struts標籤-html
struts標籤——html
2007-05-23 14:52
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.