Struts的回显

来源:互联网 发布:京东盈利模式 知乎 编辑:程序博客网 时间:2024/05/22 13:13

思路:

从一个Action   redirect跳转到一个回显页面,其中Action里的AcrionForm和Edit页面中的ActionFom是同一个Fom类,而且从Action跳转到edit.jsp设置为服务器跳转,这样form的数据不会丢失。所以只要在进入回显页面的Action中查询出回显数据后,放到ActionForm中,就能在Edit页面中进行数据回显了。


struts-config.xml:

<struts-config><form-beans><form-bean name="personForm" type="com.xxc.form.PersonForm"></form-bean></form-beans><action-mappings><action path="/editPersonUi" type="com.xxc.actionUI.EditUiAction" name="personForm"><forward name="editUi" path="/CRUD/edit.jsp" redirect="false"></forward><!--redirect="false"表示进行服务器跳转 --></action><action path="/editPerson" type="com.xxc.action.PersonAction" name="personForm" parameter="method"></action></action-mappings></struts-config>

EditAction:

public class EditUiAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {PersonDao personDao = new PersonDaoImpl();Person p = personDao.findPersonById(request.getParameter("id"));PersonForm personForm = (PersonForm)form;//将form强转,然后赋值personForm.setId(p.getId());personForm.setUserName(p.getUserName());personForm.setBirthday(p.getBirthday());personForm.setDescribe(p.getDescribe());personForm.setEduName(p.getEduName());personForm.setSex(p.getSex());personForm.setLove(p.getLove().split(","));//如果有checkbox和select组件,那么首先需要将checkbox和select的所有内容传递过去,然后struts回进行自动回显,因为选择的值在上面的form里已经封装了String[] allLove = AllLove.allLoce;request.setAttribute("allLove", allLove);EduDao eduDao = new EduDaoImpl();List<Edu> eduList = eduDao.getAllEdu();request.setAttribute("eduList", eduList);return mapping.findForward("editUi");}}



Edit.jsp:

 <body> <html:form action="editPerson.do?method=edit" method="post"> <html:hidden property="id"/>    <table>    <tr>    <td>用户名:</td>    <td>    <html:text property="userName"></html:text>    </td>    </tr>    <tr>    <td>性别:</td>    <td>    <html:radio property="sex" value="1"></html:radio>男    <html:radio property="sex" value="0"></html:radio>女    </td>    </tr>    <tr>    <td>生日:</td>    <td>    <html:text property="birthday"></html:text>    </td>    </tr>    <tr>    <td>描述:</td>    <td>    <html:textarea property="describe" rows="10" cols="40"></html:textarea>    </td>    </tr>    <tr>    <td>爱好</td>    <td>    <c:if test="${! empty allLove}">    <c:forEach items="${allLove}" var="love">    <html:multibox property="love">    ${love}    </html:multibox>    ${love}    </c:forEach>    </c:if>    </td>    </tr>    <tr>    <td>级别</td>    <td><html:select property="eduName"><html:options collection="eduList" property="eduId" labelProperty="eduName"/></html:select>    </td>    </tr>    <tr>    <td>    <html:submit value="提交"/><html:reset value="重置"/>    </td>    <tr>    </table>    </html:form>  </body>



原创粉丝点击