struts2框架是一個(gè)非常優(yōu)秀的mvc框架,時(shí)至今日已有很多公司采用其作為表示層的控制轉(zhuǎn)發(fā)工具,我非常喜歡struts2的攔截器特性和一整套的自定義標(biāo)簽。在這根據(jù)個(gè)人使用struts2的經(jīng)驗(yàn),與大家分享一些常用的struts2標(biāo)簽,希望對(duì)大家有所幫助。
?
- 實(shí)例場(chǎng)景
- 需求分析
- 實(shí)例效果
- 后臺(tái)java代碼
public class UserBean implements Serializable{ private static final long serialVersionUID = -5808037703808170288L; private int userId; //編號(hào) private String userName; //姓名 private String password; //密碼 private Date birthday = new Date(); //生日:格式y(tǒng)yyy-MM-dd,默認(rèn)為當(dāng)前時(shí)間 private int sex; //性別:0男,1女 private int[] hobby; //愛(ài)好,數(shù)組 private int city; //所屬 城市 getter、setter... }
public class CityBean implements Serializable{ private static final long serialVersionUID = -6562852059776509594L; private int cityId; private String cityValue; public CityBean(int cityId, String cityValue) { super(); this.cityId = cityId; this.cityValue = cityValue; } getter、setter }?
public class TagsService { /** * Function : 獲取城市的集合 */ public List<CityBean> getCitys() /** * Function : 獲取興趣的集合 */ public List<HobbyBean> getHobbis() /** * Function : 獲取性別的集合 */ public List<SexBean> getSexs() /** * Function : 獲取被選中的興趣愛(ài)好集合 */ public List<HobbyBean> getCheckedHobbies(int hobbies[]) /** * Function : 獲取被選擇的城市集合 */ public CityBean getSelectedCity(int cityId) }
public class TagsAction extends ActionSupport { private static final long serialVersionUID = 4361410156958515185L; private TagsService tagsService = new TagsService(); //****formbean***** private List<CityBean> lstCityBean; private List<HobbyBean> lstHobbyBean; private UserBean userBean; //*******action method*********** /** * 進(jìn)入表單填寫(xiě)頁(yè)面 */ public String goIndex(){ userBean = new UserBean(); HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); request.setAttribute("lstSexBean", tagsService.getSexs()); return SUCCESS; } /** * Function : 提交表單 */ public String doSubmit(){ HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); request.setAttribute("lstHobby", tagsService.getCheckedHobbies(userBean.getHobby())); request.setAttribute("cityBean", tagsService.getSelectedCity(userBean.getCity())); return SUCCESS; } /** * Function : 驗(yàn)證表單數(shù)據(jù) */ public void validateDoSubmit(){ if(userBean.getCity()<1){ this.addFieldError("userBean.city", "請(qǐng)選擇城市!"); return; } } public List<CityBean> getLstCityBean() { return tagsService.getCitys(); } public List<HobbyBean> getLstHobbyBean() { return tagsService.getHobbis(); } gettter、setter........ }
- 表單數(shù)據(jù)填寫(xiě)頁(yè)面代碼分析
<%@ taglib prefix="s" uri="/struts-tags"%>?
<body> <h3>Debug標(biāo)簽</h3> <s:debug></s:debug> <hr/> <h3>表單標(biāo)簽</h3> <form action="<%=root%>/doSubmit.action" method="post"> <s:fielderror cssStyle="color:red"></s:fielderror> <table> <tr> <td>編號(hào):</td> <td><s:textfield name="userBean.userId"/></td> </tr> <tr> <td>姓名:</td> <td><s:textfield name="userBean.userName"></s:textfield></td> </tr> <tr> <td>密碼:</td> <td><s:password name="userBean.password"></s:password></td> </tr> <tr> <td>生日:</td> <td> <s:textfield name="userBean.birthday"> <s:param name="value"> <s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" /> </s:param> </s:textfield> </td> </tr> <tr> <td>性別:</td> <td><s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio></td> </tr> <tr> <td>城市:</td> <td><s:select name="userBean.city" list="lstCityBean" listKey="cityId" listValue="cityValue" headerKey="0" headerValue="--請(qǐng)選擇--"></s:select></td> </tr> <tr> <td>愛(ài)好:</td> <td><s:checkboxlist name="userBean.hobby" list="lstHobbyBean" listKey="hobbyId" listValue="hobbyValue"></s:checkboxlist></td> </tr> <tr> <s:hidden></s:hidden> <td><s:submit value="提交"/></td> <td><s:reset value="重置"/></td> </tr> </table> </form> </body>
?
<s:textfield name="userBean.birthday"> <s:param name="value"> <s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" /> </s:param> </s:textfield>
<s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio>
request.setAttribute("lstSexBean", tagsService.getSexs());
private List<SexBean> lstSexBean; public List<SexBean> getLstSexBean(){ return tagsService.getSexs(); }
<input type="radio" name="userBean.sex" id="userBean_sex0" checked="checked" value="0"/><label for="userBean_sex0">男</label> <input type="radio" name="userBean.sex" id="userBean_sex1" value="1"/><label for="userBean_sex1">女</label>
<select name="userBean.city" id="userBean_city"> <option value="0" selected="selected" >--請(qǐng)選擇--</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">廣州</option> <option value="4">成都</option> <option value="5">深圳</option> </select>? <s:checkboxlist> 標(biāo)簽:復(fù)選標(biāo)簽,該標(biāo)簽的使用方法跟<s:radio>標(biāo)簽完全類似
<input type="checkbox" name="userBean.hobby" value="1" id="userBean.hobby-1"/> <label for="userBean.hobby-1" class="checkboxLabel">唱歌</label> <input type="checkbox" name="userBean.hobby" value="2" id="userBean.hobby-2"/> <label for="userBean.hobby-2" class="checkboxLabel">跳舞</label> <input type="checkbox" name="userBean.hobby" value="3" id="userBean.hobby-3"/> <label for="userBean.hobby-3" class="checkboxLabel">運(yùn)動(dòng)</label> <input type="checkbox" name="userBean.hobby" value="4" id="userBean.hobby-4"/> <label for="userBean.hobby-4" class="checkboxLabel">旅游</label> <input type="checkbox" name="userBean.hobby" value="5" id="userBean.hobby-5"/> <label for="userBean.hobby-5" class="checkboxLabel">宅神</label> <input type="hidden" id="__multiselect_userBean_hobby" name="__multiselect_userBean.hobby" value="" />? <s:hidden> 標(biāo)簽:隱藏標(biāo)簽,可以設(shè)置變量值,但是不在頁(yè)面顯示
- 提交后顯示的頁(yè)面
<body> <table> <tr> <td>編號(hào):</td> <td><s:property value="userBean.userId"></s:property></td> </tr> <tr> <td>姓名:</td> <td><s:property value="userBean.userName"></s:property></td> </tr> <tr> <td>密碼:</td> <td><s:property value="userBean.password"></s:property></td> </tr> <tr> <td>生日:</td> <td><s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" /></td> </tr> <tr> <td>性別:</td> <td> <s:if test="userBean.sex==0"> 男 </s:if> <s:else> 女 </s:else> </td> </tr> <tr> <td>城市:</td> <td> <s:property value="#request.cityBean.cityValue"/> </td> </tr> <tr> <td>愛(ài)好:</td> <td> <s:if test="#request.lstHobby!=null"> <s:iterator value="#request.lstHobby" var="hobby" status="index" begin="0" end="#request.lstHobby.length-1"> 第[<s:property value="%{#attr.index.index+1}"/>]條愛(ài)好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/> </s:iterator> </s:if> </td> </tr> </table> </body>
<s:iterator value="#request.lstHobby" var="hobby" status="index" begin="0" end="#request.lstHobby.length-1">
第[<s:property value="%{#attr.index.index+1}"/>]條愛(ài)好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
</s:iterator>
- OGNL簡(jiǎn)介
之所以命名為OGNL,就是因?yàn)樗幚韺?duì)象很給力,struts能夠?qū)?duì)象層層解析,把各個(gè)對(duì)象的關(guān)系以圖的樣式展示出來(lái)。比如userBean.userId,之所以能找到這個(gè)對(duì)象,就是因?yàn)镺GNL會(huì)先找 userBean 對(duì)象,然后再找 userBean 對(duì)象里的 userId屬性 。假設(shè)U serBean 這個(gè)類還包含了名為Role的javabean的實(shí)例,Role里面包含字段roleName,我們要找到roleName就可以直接寫(xiě)<s:property value="user.role.roleName">,OGNL通過(guò)對(duì)象逐級(jí)導(dǎo)航找到子對(duì)象。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
