问题
Hi I am creating two separate bean for one UserVO for storing userId and userPassword.And Second bean CompanyVO for storing rest of the details. I am using spring form.I want to include both UserVO and and CompanyVO in spring form.
Here is my code
CompanyVO
public class CompanyVO {
private long companyId;
private String comapanyName;
private String companySize;
private String compnayDescription;
private String foundedYear;
private String websiteURL;
}
UserVO
public class UserVO {
private long userId;
private String emailId;
private String userPassword;
private long isActive;
}
I have used ModelMap in my controller to include Both UserVO and CompanyVO.
Controller
@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>
I am getting following error
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"/>
回答1:
Create a new 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;
}
}
Change your controller:
@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;
}
And finally change your form:
<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>
来源:https://stackoverflow.com/questions/42176677/how-to-include-multiple-model-element-in-spring-form