« | April 2018 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:8998595 建立时间:2004年12月20日 |

| |
[j2ee]spring如何在一个jsp中使用两个command对象 原创空间, 软件技术
邢红瑞 发表于 2005-9-12 9:06:48 |
有个巴西的网友,提出了这个问题,如何在一个jsp中使用两个command对象,并提交到两个不同的controller,我给个例子,这个例子是别人的,我进行了修改-servlet.xml文件 <bean name="inputaform " class="ee.bug.InputaFormController"> <property name="commandClass"><value>ee.bug.Input3Bean</value></property><property name="formView"><value>inputa</value></property><property name="successView"><value>resulta</value></property></bean><bean name="inputbform " class="ee.bug.InputbFormController"><property name="commandClass"><value>ee.bug.Input3Bean</value></property><property name="formView"><value>inputa</value></property><property name="successView"><value>resultb</value></property></bean>command类package ee.bug;
/** * * @author xing */public class Input3Bean { /** Creates a new instance of Input3Bean */ public Input3Bean() { } String username; int age; public void setUsername(String name) { username = name; } public String getUsername() { return username; } public void setAge(int age) { this.age = age; } public int getAge() { return age; }}InputaFormController类package ee.bug;import org.springframework.web.servlet.mvc.SimpleFormController;import org.springframework.web.servlet.ModelAndView;import org.springframework.validation.BindException;import javax.servlet.http.HttpServletRequest;import java.util.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * * @author xing */public class InputaFormController extends SimpleFormController{ private final Log log = LogFactory.getLog(getClass().getName()); public ModelAndView onSubmit(Object command) throws Exception { System.out.println("com=" + command.getClass().getName()); Input3Bean formBean = (Input3Bean)command; BindException errors = new BindException(command, "command"); Map model = errors.getModel(); model.put("username", formBean.getUsername()); return new ModelAndView(this.getSuccessView(), model); } protected Map referenceData(HttpServletRequest request) throws Exception { Map refData = new HashMap(); Input3Bean formBean=new Input3Bean(); formBean.setUsername("rod johnson"); log.debug(formBean.getUsername()); //这里使用两种不同的方法 // BindException errorsa = new BindException(formBean, "command1"); request.setAttribute("command1", formBean); BindException errorsb = new BindException(new Input3Bean(), "command2"); // refData.putAll(errorsa.getModel()); refData.putAll(errorsb.getModel()); return refData; }}
也可以在formBackingObject使用此方法,只是要将其放到request中InputbFormControllerpackage ee.bug;import org.springframework.web.servlet.mvc.SimpleFormController;import org.springframework.web.servlet.ModelAndView;import org.springframework.validation.BindException;import java.util.*;public class InputbFormController extends SimpleFormController { public ModelAndView onSubmit(Object command) throws Exception { Input3Bean formBean = (Input3Bean)command; BindException errors = new BindException(command, "command"); Map model = errors.getModel(); model.put("username", formBean.getUsername()); return new ModelAndView(this.getSuccessView(), model); }}inputa.jsp<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><HTML><head><title>Input 1 - Form based input</title></head><body>Form 1:<BR/><FORM name="form1" action="<c:url value="/inputaform.do"/>" method="POST">Username:<spring:bind path="command1.username"><INPUT type="text" name="username" size="32" value="<c:out value="${status.value}" />"><BR/></spring:bind><INPUT type = "submit" value=" Submit "/></FORM>Form 2:<BR/><FORM name="form2" action="<c:url value="/inputbform.do"/>" method="POST">Username:<spring:bind path="command2.username"><INPUT type="text" name="username" size="32"/><BR/></spring:bind><INPUT type = "submit" value=" Find "/></FORM></body></HTML>resulta.jsp<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><HTML><HEAD><TITLE></TITLE></HEAD><BODY><H1>The input is</H1><P>username: <c:out value="${username}"/>.</p></BODY></HTML>resulta.jsp<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><HTML><HEAD><TITLE></TITLE></HEAD><BODY><H1>The input is</H1><P>favorite: <c:out value="${username}"/>.</p></BODY></HTML> |
|
论坛自动发贴机作者qq:525093551 来祝贺 原创空间, 软件技术
mqq525093551发表评论于2007-2-6 11:45:11 |
真不错,非常好。请帮忙推荐我的软件:论坛自动发贴机 自动发贴软件 论坛发贴工具 联系Q:525093551 网站推广软件 论坛群发软件 论坛群发器信息群发软件 免费论坛群发软件联系qq:525093551 论坛自动发帖机 自动发帖软件 论坛发帖工具 论坛发帖机 论坛发帖软件 留言本群发 联系qq:525093551 t9c5c5gh |
|
» 1 »
|