本文介绍了如何在弹簧形式中包含多个模型元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个UserVO创建两个单独的bean,用于存储userId和userPassword.以及为另一个bean CompanyVO创建用于存储其余详细信息.我正在使用Spring表单.我想在Spring表单中同时包含UserVO和CompanyVO.

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.

这是我的代码

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.

I have used ModelMap in my controller to include Both UserVO and 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

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"/>

推荐答案

创建一个新的类RegisterCmd

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

更改您的控制器:

@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>

这篇关于如何在弹簧形式中包含多个模型元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 13:15