Struts的回显

思路:

从一个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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值