常用的Struts標籤庫有以下五大類:
1.HTML Tag:
用來創建能夠和Struts框架以及其它相應的HTML標籤交互的HTML輸入表單;
如:<html:form></html:form>,<html:text/>,<html:password/>,<html:radio/>,<html:checkbox/>,<htmlmultibox>
2.Bean Tag
該標籤庫包含的標籤可以用來創建bean、訪問bean和訪問bean的屬性.
如:<bean:write/>:用於將bean的屬性輸送到jsp頁面,<bean:define/>定義一個新的bean
3.Logic Tag
該標籤庫包含的標籤可以用來進行邏輯判斷、集合迭代和流程式控制制.
如:<logic:iterate/>:用來循環迭代,<logic:eaual/>:用來進行邏輯判斷
4.Nested:增強對其他Struts標籤的嵌套使用能力
該標籤庫建立在前三個標籤庫的基礎上,具有前三個標籤庫的所有功能,只是允許標籤間的嵌套.
5.Template Tag
隨著Titles框架包的出現,此標記已開始減少使用
下面介紹幾個最常用的標籤:
<html:check box/>一般用於一個選項的複選框
<html:multibox/>一般用於多個選項的複選框
<bean:write name="user" property="username"/>等同於EL表達示:${user.username}
<bean:define id="inter" name="user" property="interest" type="java.lang.String"/>定義一個bean
<bean:message key=" " arg0=" "/> key 定義在資源文件中,args0[1,2]為參數
<logic:iterate name="list" id="user"> 等同於JSTL的:<c:foeach item=${list} var="user"/>
<logic:equal name="user" property="sex" value="0"/>等同於JSTL的:<c:when test=""/>
<logic:empty />標籤是用來判斷是否為空的.如果為空,該標籤體中嵌入的內容就會被處理
<logic:empty name="listForm" property = "persons"> <div>集合persons為空!</div> </logic:empty>
1.下面給一個表單的完整代碼:
<body> <center> <html:form action="/user"> 用戶名:<html:text property="user.username"></html:text><p/> 密碼:<html:text property="user.pwd"></html:text><p/> 性別:<html:radio property="user.sex" value="男">男</html:radio> <html:radio property="user.sex" value="女">女</html:radio><p/> 城市:<html:select property="user.city"> <html:option value="">請選擇</html:option> <html:option value="武漢">武漢</html:option> <html:option value="上海">上海</html:option> <html:option value="北京">北京</html:option> </html:select><p/> 愛好:<html:multibox property="interest" value="看書"/>看書 <html:multibox property="interest" value="遊戲"/>遊戲 <html:multibox property="interest" value="睡覺"/>睡覺<p/> <html:submit/><html:cancel/> </html:form> </center> </body> |
使用html標籤作為表單輸入,可以方便的使用驗證框架即:<html:errors property="username">
2.下面給一個顯示數據的代碼:
<table align="center" border="1" width="600"> <caption>用戶註冊信息</caption> <tr> <td>用戶名</td> <td>密碼</td> <td>性別</td> <td>城市</td> <td>愛好</td> <td colspan="2" align="center">操作</td> </tr> <logic:iterate name="list" id="user"> <tr> <td><bean:write name="user" property="username"/></td> <td><bean:write name="user" property="pwd"/></td> <td><bean:write name="user" property="sex"/></td> <td><bean:write name="user" property="city"/></td> <td> <bean:define id="interest" name="user" property="interest" type="java.lang.String"></bean:define> <logic:iterate id="inter" collection="<%=StringUtil.stringChange2(interest)%>"> ${inter} </logic:iterate> </td> <td><a href="del.do?userid=${user.userid}">刪除</a></td> <td><a href="upd.do?userid=${user.userid}">修改</a></td> </tr> </logic:iterate> </table> |
作為顯示數據,Struts標籤並不比Jstl與EL方便,因此,本人更習慣用後者
其實Struts標籤的好處,並不是上面這些,而是它可利用ActionForm來填充數據
比如我們在做頁面數據修改的時候,會讓當前頁面數據顯示到顯示修改頁面,這樣利於客戶端修改
以前我們這樣做的:根據id從資料庫查出,然後使用html的value屬性填充,現在有了Struts標籤,就不需要那麼麻煩了
直接在Action里填充ActionForm的數據就搞定了:
public ActionForward upd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; int userid = Integer.parseInt(request.getParameter("userid")); UserInfo user = biz.findUser(userid); String[]interest = StringUtil.stringChange2(user.getInterest()); //將用戶信息填充到ActionForm userForm.setUser(user); userForm.setInterest(interest); return mapping.findForward("upd"); } |
然後在修改頁面顯示,請往下看:
<html:form action="/doupd"> 用戶名:<html:text property="user.username"></html:text><p/> 密碼:<html:text property="user.pwd"></html:text><p/> 性別:<html:radio property="user.sex" value="男">男</html:radio> <html:radio property="user.sex" value="女">女</html:radio><p/> 城市:<html:select property="user.city"> <html:option value="">請選擇</html:option> <html:option value="武漢">武漢</html:option> <html:option value="上海">上海</html:option> <html:option value="北京">北京</html:option> </html:select><p/> 愛好:<html:multibox property="interest" value="看書"/>看書 <html:multibox property="interest" value="遊戲"/>遊戲 <html:multibox property="interest" value="睡覺"/>睡覺<p/> <html:submit value="修改"/> </html:form> |
[火星人 ] Struts核心標籤已經有669次圍觀