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通过对象逐级导航找到子对象。