使用Struts有一段時(shí)間了,但也僅僅涉及到一部分內(nèi)容,比如Action做邏輯控制、FormBean對(duì)象化用戶提交的Form數(shù)據(jù)、國(guó)際化資源文件,Struts很重要的一部分Struts Tag卻一直沒(méi)有使用到,一是因?yàn)橐炀毜氖褂肧truts Tag需要一定的時(shí)間來(lái),二也是因?yàn)樽约河幸惶妆容^好的Tag可用。JSP頁(yè)面上Tag使用的較多雖然能讓頁(yè)面看起來(lái)比較整潔,但可讀性會(huì)相對(duì)降低,開(kāi)發(fā)時(shí)的靈活性也會(huì)降低。相對(duì)來(lái)說(shuō)還是比較看中Action的邏輯控制部分。
重新翻看Struts書本,特別注意了Tag部分,也還是覺(jué)得有些眩暈,或許使用熟練是才能體驗(yàn)出其中的玄妙。摘錄幾個(gè)Tag放在這里。
1、<bean:message key="hello.jsp.page.heading"/>
用于輸出資源文件中的內(nèi)容
2、<html:errors/>或者寫成<html:error property="xxx"/>
用于輸出錯(cuò)誤信息,當(dāng)指定peoperty時(shí),則只顯示對(duì)應(yīng)的錯(cuò)誤信息,如:
ActionErrors errors = new ActionErrors();
errors.add("xxx",new ActionError("username.null"));
3、<html:form action="/Helloworld">
Form表單
4、<html:text property="userName" size="16"/>
表單中的輸入域
5、<html:submit property="submit" value="Submit"/>
提交按鈕
6、<html:reset/>
重置按鈕
7、<bean:write name="bitiliu" property="userName"/>
從request中或者session中獲得bitiliu對(duì)象,并輸出userName屬性的值,可指定scope
8、非空邏輯判斷
<logic:notEmpty name="bitiliu" property="userName" scope="request">
?do something...??
</logic:notEmpty>
9、<html:link>
用于生成鏈接,可以增加參數(shù)
<%
?pageContext.setAttribute("name","I am a boy!");
?HashMap myMap = new HashMap();?
?myMap.put("name","bitiliu");
?myMap.put("password",new String[]{"1","2","3"});
?pageContext.setAttribute("myMap",myMap);
%>
<html:link page="/Test.jsp" paramId="haha" paramName="name" name="myMap">Test</html:link>
生成的超鏈接為:
<a href="/Struts2/Test.jsp?password=1&password=2&password=3&haha=I+am+a+boy%21&name=bitiliu">Test</a>
10、<html:img>
生成圖片標(biāo)記,如<html:img page="/logo.gif"/>,生成的HTML為<img src="/web/logo.gif">
<html:img>也可以包含請(qǐng)求參數(shù),可參考<html:link>
11、<html:cancel>
增加取消按鈕,點(diǎn)擊取消按鈕也會(huì)請(qǐng)求form的action事件,可以在execute方法中通過(guò)方法isCancelled(request)來(lái)判斷是否點(diǎn)擊了取消按鈕。
12、<html:hidden>
生成隱藏域,如<html:hidden property="name"/>,生成的HTML為<input type="hidden" name="name"/>
13、<html:checkbox property="check">
對(duì)應(yīng)的Form中應(yīng)該有一對(duì)應(yīng)屬性check類型為boolean
14、<html:multibox property="strArray" value="value1">
對(duì)應(yīng)的Form中對(duì)應(yīng)屬性strArray類型為String
15、<html:radio property="strArray" value="value1">
同<html:multibox>,但為單選框
16、<html:select>
下拉選擇框,可指定property、size和multiple(true\false),標(biāo)簽內(nèi)可包含<html:option>、<html:options>和<html:optionCollections>
17、<html:option>
可寫成:<html:option value="color.orange" key="color.orange"/>或<html:option value="color.orange">Orange</html:option>
18、<html:options>
一次生成多個(gè)option,使用示例:
Vector vec = new Vector();
vec.add(new org.apache.struts.util.LabelValueBean("label1","value1"));
vec.add(new org.apache.struts.util.LabelValueBean("label2","value2"));
pageContext.setAttribute("vec",vec);
<html:options collection="vec" property="value" labelProperty="label"/>
19、<html:file>
用于實(shí)現(xiàn)文件上傳,其中<html:form>的enctype="multipart/form-date",F(xiàn)orm的對(duì)應(yīng)屬性類型應(yīng)為File類型,
邏輯判斷標(biāo)簽:
1、<logic:equal>
相等,示例如下:
<logic:equal name="strValue" value="112" scope="request">equal</logic:equal>
標(biāo)簽從request中獲得strValue對(duì)象,然后和112比較,如果相等,則輸入字符串equal,否則不輸出。下面標(biāo)簽相同。
2、<logic:greateEqual>
大于等于
3、<logic:greaterThan>
大于
4、<logic:lessEqual>
小于等于
5、<logic:lessThan>
小于
6、<logic:notEqual>
不等于
7、<logic:match>
指定的值是變量的子串
8、<logic:notMatch>
指定的值不是變量的子串
9、<logic:iterate>
疊代標(biāo)簽
其它:
1、在國(guó)際化文件中添加兩個(gè)errors.header和errors.footer用于指定錯(cuò)誤信息顯示時(shí)前后追加的內(nèi)容。