如何以 spring 形式包含多个模型元素?
How to include multiple model element in spring form?
您好,我正在为一个 UserVO 创建两个单独的 bean,用于存储 userId 和 userPassword.And 第二个 bean CompanyVO,用于存储其余详细信息。
我正在使用 spring form.I 想以 spring 形式包含 UserVO 和 CompanyVO。
这是我的代码
公司VO
public class CompanyVO {
private long companyId;
private String comapanyName;
private String companySize;
private String compnayDescription;
private String foundedYear;
private String websiteURL;
}
用户VO
public class UserVO {
private long userId;
private String emailId;
private String userPassword;
private long isActive;
}
我在我的控制器中使用了 ModelMap 来包含 UserVO 和 CompanyVO。
控制器
@RequestMapping("/registerCompanyForm")
public ModelAndView registerCompanyForm(ModelMap modelMap){
ModelAndView modelAndView=new ModelAndView();
modelMap.addAttribute("companyVO",new CompanyVO());
modelMap.addAttribute("userVO",new UserVO());
modelAndView.addAllObjects(modelMap);
modelAndView.setViewName("register");
return modelAndView;
}
register.jsp
<form:form action="/JobPortal/addCompany" method="post" modelAttribute="companyVO">
<div class="formrow">
<form:input path="${companyVO.comapanyName}" id="comapanyName" class="form-control"
placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="${companyVO.compnayDescription}" rows="5" cols="30" class="form-control"
placeholder="Enter Company Description"/>
</div>
<div class="formrow">
<form:select path="${companyVO.companySize}" class="form-control">
<form:option value="">Select Company Size</form:option>
<form:option value="0-50">0-50</form:option>
<form:option value="0-50">51-200</form:option>
<form:option value="200+">200+</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="${companyVO.websiteURL}" class="form-control" placeholder="Enter company website URL"/>
</div>
<div class="formrow">
<form:select path="${companyVO.foundedYear}" class="form-control">
<form:option value="">Select Foundation Year</form:option>
<form:option value="2016">2016</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="${userVO.emailId}" class="form-control" placeholder="Email"/>
</div>
<div class="formrow">
<form:input path="${userVO.userPassword}" class="form-control" placeholder="Enter Your Password"/>
</div>
<div class="formrow">
<input type="text" name="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="formrow">
<input type="checkbox" value="agree text c" name="cagree"/>
There are many variations of passages of Lorem Ipsum available
</div>
<input type="submit" class="btn" value="Register">
</form:form>
我收到以下错误
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.register_jsp._jspx_meth_form_005finput_005f0(register_jsp.java:913)
at org.apache.jsp.WEB_002dINF.views.register_jsp._jspService(register_jsp.java:287)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
Feb 11, 2017 7:11:32 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/JobPortal] threw exception [An exception occurred processing JSP page /WEB-INF/views/register.jsp at line 72
69: <c:out value="${companyVO.comapanyName}"></c:out>
<form:form action="/JobPortal/addCompany" method="post" >
<div class="formrow">
72 <form:input path="${companyVO.comapanyName}" id="comapanyName" class="form-control" placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="${companyVO.compnayDescription}" rows="5" cols="30" class="form-control" placeholder="Enter Company Description"/>
创建一个新的 class RegisterCmd
public class RegisterCmd {
private UserVO userVO;
private CompanyVO companyVO;
public UserVO getUserVO() {
return userVO;
}
public void setUserVO(UserVO userVO) {
this.userVO = userVO;
}
public CompanyVO getCompanyVO() {
return companyVO;
}
public void setCompanyVO(CompanyVO companyVO) {
this.companyVO = companyVO;
}
}
更改您的控制器:
@RequestMapping("/registerCompanyForm")
public ModelAndView registerCompanyForm(ModelMap modelMap){
ModelAndView modelAndView=new ModelAndView();
RegisterCmd registerCmd = new RegisterCmd();
modelMap.addAttribute("registerCmd", registerCmd);
modelAndView.addAllObjects(modelMap);
modelAndView.setViewName("register");
return modelAndView;
}
最后改变你的形式:
<form:form action="/JobPortal/addCompany" method="post" commandName="registerCmd">
<div class="formrow">
<form:input path="companyVO.comapanyName" id="comapanyName" class="form-control"
placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="companyVO.compnayDescription" rows="5" cols="30" class="form-control"
placeholder="Enter Company Description"/>
</div>
<div class="formrow">
<form:select path="companyVO.companySize" class="form-control">
<form:option value="">Select Company Size</form:option>
<form:option value="0-50">0-50</form:option>
<form:option value="0-50">51-200</form:option>
<form:option value="200+">200+</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="companyVO.websiteURL" class="form-control" placeholder="Enter company website URL"/>
</div>
<div class="formrow">
<form:select path="companyVO.foundedYear" class="form-control">
<form:option value="">Select Foundation Year</form:option>
<form:option value="2016">2016</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="userVO.emailId" class="form-control" placeholder="Email"/>
</div>
<div class="formrow">
<form:input path="userVO.userPassword" class="form-control" placeholder="Enter Your Password"/>
</div>
<div class="formrow">
<input type="text" name="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="formrow">
<input type="checkbox" value="agree text c" name="cagree"/>
There are many variations of passages of Lorem Ipsum available
</div>
<input type="submit" class="btn" value="Register">
</form:form>
您好,我正在为一个 UserVO 创建两个单独的 bean,用于存储 userId 和 userPassword.And 第二个 bean CompanyVO,用于存储其余详细信息。 我正在使用 spring form.I 想以 spring 形式包含 UserVO 和 CompanyVO。
这是我的代码
公司VO
public class CompanyVO {
private long companyId;
private String comapanyName;
private String companySize;
private String compnayDescription;
private String foundedYear;
private String websiteURL;
}
用户VO
public class UserVO {
private long userId;
private String emailId;
private String userPassword;
private long isActive;
}
我在我的控制器中使用了 ModelMap 来包含 UserVO 和 CompanyVO。
控制器
@RequestMapping("/registerCompanyForm")
public ModelAndView registerCompanyForm(ModelMap modelMap){
ModelAndView modelAndView=new ModelAndView();
modelMap.addAttribute("companyVO",new CompanyVO());
modelMap.addAttribute("userVO",new UserVO());
modelAndView.addAllObjects(modelMap);
modelAndView.setViewName("register");
return modelAndView;
}
register.jsp
<form:form action="/JobPortal/addCompany" method="post" modelAttribute="companyVO">
<div class="formrow">
<form:input path="${companyVO.comapanyName}" id="comapanyName" class="form-control"
placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="${companyVO.compnayDescription}" rows="5" cols="30" class="form-control"
placeholder="Enter Company Description"/>
</div>
<div class="formrow">
<form:select path="${companyVO.companySize}" class="form-control">
<form:option value="">Select Company Size</form:option>
<form:option value="0-50">0-50</form:option>
<form:option value="0-50">51-200</form:option>
<form:option value="200+">200+</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="${companyVO.websiteURL}" class="form-control" placeholder="Enter company website URL"/>
</div>
<div class="formrow">
<form:select path="${companyVO.foundedYear}" class="form-control">
<form:option value="">Select Foundation Year</form:option>
<form:option value="2016">2016</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="${userVO.emailId}" class="form-control" placeholder="Email"/>
</div>
<div class="formrow">
<form:input path="${userVO.userPassword}" class="form-control" placeholder="Enter Your Password"/>
</div>
<div class="formrow">
<input type="text" name="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="formrow">
<input type="checkbox" value="agree text c" name="cagree"/>
There are many variations of passages of Lorem Ipsum available
</div>
<input type="submit" class="btn" value="Register">
</form:form>
我收到以下错误
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.register_jsp._jspx_meth_form_005finput_005f0(register_jsp.java:913)
at org.apache.jsp.WEB_002dINF.views.register_jsp._jspService(register_jsp.java:287)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
Feb 11, 2017 7:11:32 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/JobPortal] threw exception [An exception occurred processing JSP page /WEB-INF/views/register.jsp at line 72
69: <c:out value="${companyVO.comapanyName}"></c:out>
<form:form action="/JobPortal/addCompany" method="post" >
<div class="formrow">
72 <form:input path="${companyVO.comapanyName}" id="comapanyName" class="form-control" placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="${companyVO.compnayDescription}" rows="5" cols="30" class="form-control" placeholder="Enter Company Description"/>
创建一个新的 class RegisterCmd
public class RegisterCmd {
private UserVO userVO;
private CompanyVO companyVO;
public UserVO getUserVO() {
return userVO;
}
public void setUserVO(UserVO userVO) {
this.userVO = userVO;
}
public CompanyVO getCompanyVO() {
return companyVO;
}
public void setCompanyVO(CompanyVO companyVO) {
this.companyVO = companyVO;
}
}
更改您的控制器:
@RequestMapping("/registerCompanyForm")
public ModelAndView registerCompanyForm(ModelMap modelMap){
ModelAndView modelAndView=new ModelAndView();
RegisterCmd registerCmd = new RegisterCmd();
modelMap.addAttribute("registerCmd", registerCmd);
modelAndView.addAllObjects(modelMap);
modelAndView.setViewName("register");
return modelAndView;
}
最后改变你的形式:
<form:form action="/JobPortal/addCompany" method="post" commandName="registerCmd">
<div class="formrow">
<form:input path="companyVO.comapanyName" id="comapanyName" class="form-control"
placeholder="Enter Company Name"/>
</div>
<div class="formrow">
<form:textarea path="companyVO.compnayDescription" rows="5" cols="30" class="form-control"
placeholder="Enter Company Description"/>
</div>
<div class="formrow">
<form:select path="companyVO.companySize" class="form-control">
<form:option value="">Select Company Size</form:option>
<form:option value="0-50">0-50</form:option>
<form:option value="0-50">51-200</form:option>
<form:option value="200+">200+</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="companyVO.websiteURL" class="form-control" placeholder="Enter company website URL"/>
</div>
<div class="formrow">
<form:select path="companyVO.foundedYear" class="form-control">
<form:option value="">Select Foundation Year</form:option>
<form:option value="2016">2016</form:option>
</form:select>
</div>
<div class="formrow">
<form:input path="userVO.emailId" class="form-control" placeholder="Email"/>
</div>
<div class="formrow">
<form:input path="userVO.userPassword" class="form-control" placeholder="Enter Your Password"/>
</div>
<div class="formrow">
<input type="text" name="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="formrow">
<input type="checkbox" value="agree text c" name="cagree"/>
There are many variations of passages of Lorem Ipsum available
</div>
<input type="submit" class="btn" value="Register">
</form:form>