如何在弹簧形式中包含多个模型元素?

时间:2017-02-11 13:54:35

标签: java spring spring-mvc spring-form spring-validator

您好我正在为一个UserVO创建两个单独的bean来存储userId和userPassword.And第二个bean CompanyVO用于存储其余的细节。 我正在使用spring form。我想以spring形式包含UserVO和CompanyVO。

这是我的代码

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;
}

我在我的控制器中使用了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"/>

1 个答案:

答案 0 :(得分:0)

创建一个新类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>