struts2框架是一個非常優秀的mvc框架,時至今日已有很多公司采用其作為表示層的控制轉發工具,我非常喜歡struts2的攔截器特性和一整套的自定義標簽。在這根據個人使用struts2的經驗,與大家分享一些常用的struts2標簽,希望對大家有所幫助。
?
- 實例場景
- 需求分析
- 實例效果
- 后臺java代碼
public class UserBean implements Serializable{ private static final long serialVersionUID = -5808037703808170288L; private int userId; //編號 private String userName; //姓名 private String password; //密碼 private Date birthday = new Date(); //生日:格式yyyy-MM-dd,默認為當前時間 private int sex; //性別:0男,1女 private int[] hobby; //愛好,數組 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 : 獲取被選中的興趣愛好集合 */ 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*********** /** * 進入表單填寫頁面 */ 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 : 驗證表單數據 */ public void validateDoSubmit(){ if(userBean.getCity()<1){ this.addFieldError("userBean.city", "請選擇城市!"); return; } } public List<CityBean> getLstCityBean() { return tagsService.getCitys(); } public List<HobbyBean> getLstHobbyBean() { return tagsService.getHobbis(); } gettter、setter........ }
- 表單數據填寫頁面代碼分析
<%@ taglib prefix="s" uri="/struts-tags"%>?
<body> <h3>Debug標簽</h3> <s:debug></s:debug> <hr/> <h3>表單標簽</h3> <form action="<%=root%>/doSubmit.action" method="post"> <s:fielderror cssStyle="color:red"></s:fielderror> <table> <tr> <td>編號:</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="--請選擇--"></s:select></td> </tr> <tr> <td>愛好:</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" >--請選擇--</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">廣州</option> <option value="4">成都</option> <option value="5">深圳</option> </select>? <s:checkboxlist> 標簽:復選標簽,該標簽的使用方法跟<s:radio>標簽完全類似
<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">運動</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> 標簽:隱藏標簽,可以設置變量值,但是不在頁面顯示
- 提交后顯示的頁面
<body> <table> <tr> <td>編號:</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>愛好:</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}"/>]條愛好:<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}"/>]條愛好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
</s:iterator>
- OGNL簡介
之所以命名為OGNL,就是因為它處理對象很給力,struts能夠將對象層層解析,把各個對象的關系以圖的樣式展示出來。比如userBean.userId,之所以能找到這個對象,就是因為OGNL會先找 userBean 對象,然后再找 userBean 對象里的 userId屬性 。假設U serBean 這個類還包含了名為Role的javabean的實例,Role里面包含字段roleName,我們要找到roleName就可以直接寫<s:property value="user.role.roleName">,OGNL通過對象逐級導航找到子對象。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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