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>
<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>