如何设置表单:另一个模型中的model属性的路径

时间:2015-11-04 09:39:04

标签: spring jsp spring-mvc servlets jstl

我可以为模型设置单独的路径名,但如果我尝试在另一个模型中设置模型的路径,则会抛出错误。我坚持不懈地给我一个解决方案,如果你详细说明并解释我将要做什么以及最好的地方在哪里了解更多,那将是非常慷慨的。

我想分享我的代码以解决问题

这是我的控制者

@RequestMapping(value="contact", method=RequestMethod.GET)
public String contactability(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("contactDetails") ContactDetails contactDetails){
    try{
        if(contactDetails==null){
            contactDetails= new ContactDetails();
        }
        model.addAttribute("contactabality", contactDetails);

    }catch(Exception e){
        e.printStackTrace();
    }

    return "contactabality";

}

这是我的模特

我有两个模型

  1. 此处的联系方式是代码

    @Entity
    @Table(name="TB_CONTACT_DETAILS")
    public class ContactDetails implements Serializable {
    
    @Id
    @GeneratedValue
    @Column(name="CONTACT_ID")
    private Long contactId;
    
    @Column(name="IS_PERM_COMM_SAME")
    private Integer isPermCommSame;
    
    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL, mappedBy="contactDetails")
    private Set<AddressDetails> addressDetails = new HashSet<AddressDetails>();
    
    public ContactDetails() {
        // TODO Auto-generated constructor stub
    }
    
    public Long getContactId() {
        return contactId;
    }
    
    public void setContactId(Long contactId) {
        this.contactId = contactId;
    }
    
    public Integer getIsPermCommSame() {
        return isPermCommSame;
    }
    
    public void setIsPermCommSame(Integer isPermCommSame) {
        this.isPermCommSame = isPermCommSame;
    }
    
    public Set<AddressDetails> getAddressDetails() {
        return addressDetails;
    }
    
    public void setAddressDetails(Set<AddressDetails> addressDetails) {
        this.addressDetails = addressDetails;
    }
    

    }

  2. 是地址模型,这里是它的代码

    @Entity
    @Table(name="TB_ADDRESS_DETAILS")
    public class AddressDetails implements Serializable{
    
    @Id
    @GeneratedValue
    @Column(name="ADDRESS_ID")
    private Long addrId;
    
    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.DETACH)
    @JoinColumn(name="ADDR_TYPE")
    private AddressTypeMaster addressType;
    
    @Column(name="ADDRESS_COL1")
    private String addrCol1;
    
    @Column(name="ADDRESS_COL2")
    private String addrCol2;
    
    @Column(name="ADDRESS_COL3")
    private String addrCol3;
    
    @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.DETACH)
    @JoinColumn(name="CITY")
    private CityMaster city;
    
    @Column(name="PINCODE")
    private String pincode;
    
    @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.DETACH)
    @JoinColumn(name="STATE")
    private StateMaster state ;
    
    @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="CONTACT_ID")
    private ContactDetails contactDetails;
    
    @Column(name="ADDR_STATUS")
    private String addrStatus;
    
    @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="addressDetails")
    private EmployeeDetails employeeDetails;
    
    public AddressDetails() {
        // TODO Auto-generated constructor stub
    }
    
    public Long getAddrId() {
        return addrId;
    }
    
    public void setAddrId(Long addrId) {
        this.addrId = addrId;
    }
    
    public AddressTypeMaster getAddressType() {
        return addressType;
    }
    
    public void setAddressType(AddressTypeMaster addressType) {
        this.addressType = addressType;
    }
    
    
    public String getAddrCol1() {
        return addrCol1;
    }
    
    public void setAddrCol1(String addrCol1) {
        this.addrCol1 = addrCol1;
    }
    
    public String getAddrCol2() {
        return addrCol2;
    }
    
    public void setAddrCol2(String addrCol2) {
        this.addrCol2 = addrCol2;
    }
    
    public String getAddrCol3() {
        return addrCol3;
    }
    
    public void setAddrCol3(String addrCol3) {
        this.addrCol3 = addrCol3;
    }
    
    public CityMaster getCity() {
        return city;
    }
    
    public void setCity(CityMaster city) {
        this.city = city;
    }
    
    public String getPincode() {
        return pincode;
    }
    
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }
    
    public StateMaster getState() {
        return state;
    }
    
    public void setState(StateMaster state) {
        this.state = state;
    }
    
    public ContactDetails getContactDetails() {
        return contactDetails;
    }
    
    public void setContactDetails(ContactDetails contactDetails) {
        this.contactDetails = contactDetails;
    }
    
    public String getAddrStatus() {
        return addrStatus;
    }
    
    public void setAddrStatus(String addrStatus) {
        this.addrStatus = addrStatus;
    }
    
    public EmployeeDetails getEmployeeDetails() {
        return employeeDetails;
    }
    
    public void setEmployeeDetails(EmployeeDetails employeeDetails) {
        this.employeeDetails = employeeDetails;
    }
    

    }

  3. 这是我的JSP

        <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Generic Tool For Employee</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    
    <link rel="stylesheet" href="layout/css/bootstrap.min.css">
    <link rel="stylesheet" href="layout/css/validator/screen.css">
    <link rel="stylesheet" href="layout/datepicker/css/bootstrap-datepicker3.min.css">
    <link rel="stylesheet" href="layout/css/index.css">
    
      <div class="container-fluid">
            <h2>Contact Details</h2>
            <form:form commandName="contactabality" class="form-horizontal" id="contactDetailsForm" role="form" method="POST" action="persDetails">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">Address Details</h4>
                    </div>
                    <div class="panel-body">
                        <div class="col-sm-6">
                    <!--COMMUNICATION ADDRESS DETAILS-->
                         <h3 class="page-header">COMMUNICATION ADDRESS DETAILS</h3>
                             <div class="form-group">
                                 <label class="col-sm-4 control-label">Address 1<span class="mandatoryRed">*</span></label>
                                 <div class="col-sm-8">
                                     <form:input path="addressDetails.addrCol1" type="text" class="form-control" placeholder="Address 1" name="commAddr1" id="commAddr1"/>
                                 </div>
                             </div>
             <%--                 <div class="form-group">
                                 <label class="col-sm-4 control-label">Address 2</label>
                                 <div class="col-sm-8">
                                     <form:input path="addressDetails.addrCol2" type="text" class="form-control" placeholder="Address 2" name="commAddr2" id="commAddr2"/>
                                 </div>
                             </div>
                             <div class="form-group">
                                 <label class="col-sm-4 control-label">Address 3</label>
                                 <div class="col-sm-8">
                                     <form:input path="addressDetails.addrCol3" type="text" class="form-control" placeholder="Address 3" name="commAddr3" id="commAddr3"/>
                                 </div>
                             </div>
                             <div class="form-group">
                                 <label class="col-sm-4 control-label">City<span class="mandatoryRed">*</span></label>
                                 <div class="col-sm-8">
                                     <form:select path="addressDetails.city.cityId" class="form-control" name="commAddrCity" id="commAddrCity">
                                        <form:options items="${cities}"/>
                                     </form:select>
                                 </div>
                             </div>
                             <div class="form-group">
                                 <label class="col-sm-4 control-label">State<span class="mandatoryRed">*</span></label>
                                 <div class="col-sm-8">
                                     <form:select path="addressDetails.state.stateId" class="form-control" name="commAddrState" id="commAddrState">
                                        <form:options items="${states}" />
                                     </form:select>
                                 </div>
                             </div>
                             <div class="form-group">
                                 <label class="col-sm-4 control-label">Pincode<span class="mandatoryRed">*</span></label>
                                 <div class="col-sm-8">
                                     <form:input path="addressDetails.pincode" type="text" class="form-control" placeholder="Pincode" name="commAddrPincode" id="commAddrPincode" maxlength="6"/>
                                 </div>
                             </div>
                         </div>
                         <!--PERMANENT ADDRESS DETAILS-->
                        <div class="col-sm-6">
                            <h4 class="page-header">PERMANENT ADDRESS DETAILS</h4>
                            <div class="form-group">
                                <label class="col-sm-9">Is your permanent address same as communication address<span class="mandatoryRed">*</span></label>
                                <div class="col-sm-2">
                                    <div class="toggle-switch toggle-switch-success">
                                        <label>
                                            <form:checkbox path="isPermCommSame" id="PermAddrCheck" type="checkbox" value="2"/>
                                            <div class="toggle-switch-inner"></div>
                                            <div class="toggle-switch-switch"><i class="fa fa-check"></i></div>
                                        </label>
                                    </div>
    
                                </div>
                            </div>
                            <div class="row permAddrClss">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Address 1<span class="mandatoryRed">*</span></label>
                                    <div class="col-sm-8">
                                        <form:input path="addressDetails.addrCol1" type="text" class="form-control" placeholder="Address 1" name="PermAddr1" id="PermAddr1"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Address 2</label>
                                    <div class="col-sm-8">
                                        <form:input  path="addressDetails.addrCol2" type="text" class="form-control" placeholder="Address 2" name="PermAddr2" id="PermAddr2"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Address 3</label>
                                    <div class="col-sm-8">
                                        <form:input path="addressDetails.addrCol3" type="text" path="addressDetails.addrCol3" class="form-control" placeholder="Address 3" name="PermAddr3" id="PermAddr3"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">City<span class="mandatoryRed">*</span></label>
                                    <div class="col-sm-8">
                                        <form:select path="addressDetails.city.cityId" class="form-control" name="PermAddrCity" id="PermAddrCity">
                                            <form:options items="${cities}"  />
                                        </form:select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">State<span class="mandatoryRed">*</span></label>
                                    <div class="col-sm-8">
                                        <form:select path="addressDetails.state.stateId" class="form-control" name="PermAddrState" id="PermAddrState">
                                            <form:options items="${states}"/>
                                        </form:select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Pincode<span class="mandatoryRed">*</span></label>
                                    <div class="col-sm-8">
                                        <form:input path="addressDetails.pincode" type="text" class="form-control" placeholder="Pincode" name="PermAddrPincode" id="PermAddrPincode" maxlength="6"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>--%>
                <p>
                    <input class="submit btn btn-default" type="submit" value="Submit">
                </p>
            </form:form> 
        </div>
    
        <!-- SCRIPTS -->
        <script src="layout/js/vendors/jquery-1.9.1.min.js"></script>
        <script src="layout/js/vendors/jquery.validate.min.js"></script>
        <script src="layout/js/vendors/bootstrap-3.3.5.js"></script>
        <script src="layout/datepicker/js/bootstrap-datepicker.min.js"></script>
        <script src="layout/js/index.js"></script>
        <script src="layout/js/contactabality.js"></script>
    

    我的错误

      

    4:46:34,916 ERROR [org.apache.catalina.core.ContainerBase。[jboss.web]。[default-host]。[/ eob-web]。[jsp]](http-localhost / 127.0。 0.1:8080-5)JBWEB000236:Servlet jsp的Servlet.service()抛出异常:org.springframework.beans.NotReadablePropertyException:无效的属性&#39; addressDetails.addrCol1&#39; bean类[com.rectrix.eob.models.ContactDetails]:Bean属性&#39; addressDetails.addrCol1&#39;不可读或getter方法无效:返回类型

    AND

      

    14:46:34,922 ERROR [org.apache.catalina.core.ContainerBase。[jboss.web]。[default-host]。[/ eob-web]。[jsp]](http-localhost / 127.0。 0.1:8080-5)JBWEB000236:Servlet jsp的Servlet.service()抛出异常:org.apache.tiles.util.TilesIOException:JSPException包括路径&#39; /WEB-INF/views/jsp/contactabality.jsp' ;

    请帮我解决这个问题。 谢谢

1 个答案:

答案 0 :(得分:1)

private Set<AddressDetails> addressDetails = new HashSet<AddressDetails>(); 

是你的问题。我相信你只会在上面的列表中存储两种地址,我建议你将永久通信地址详细信息绑定为单独的属性,而不是绑定为列表

Transient bean ContactDetails@Transient private AddressDetails commAddressDetails以及相应的setter / getter中创建另外两个@Transient private AddressDetails permAddressDetails属性。

在持久编写自己的逻辑以构建one-to-many关系之前,在Jsp中为各个字段使用这些属性。如果您有静态的地址数,这将有效。