【问题标题】:Spring form binding with embeded objecct嵌入对象的 Spring 表单绑定
【发布时间】:2014-06-12 10:48:47
【问题描述】:

我正在尝试将 java pojo 类与 jsp 绑定。我的示例同时使用了 spring 和 hibernate。 我的 java pojo 类嵌入了另一个 java pojo 类。但是在将 jsp 与 pojo 类绑定时,我得到了

org.springframework.beans.NotReadablePropertyException: Invalid property 'street' of bean class [com.junaid.spring.form.Employee]: Bean property 'street' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:98)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:224)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)

我的代码如下

 /**
* 
*/
package com.junaid.spring.form;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

  /**
   * @author jamju02
  *
 */
@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {
@Id
private String email;
private String firstName;
private String lastName;
@Embedded
private Address address; 
private String age;
private String password;

public enum Gender {
    MALE, FEMALE
}


/**
 * @return the email
 */
public String getEmail() {
    return email;
}
/**
 * @param email the email to set
 */
public void setEmail(String email) {
    this.email = email;
}
/**
 * @return the firstName
 */
public String getFirstName() {
    return firstName;
}
/**
 * @param firstName the firstName to set
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
/**
 * @return the lastName
 */
public String getLastName() {
    return lastName;
}
/**
 * @param lastName the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}
/**
 * @return the address
 */
public Address getAddress() {
    return address;
}
/**
 * @param address the address to set
 */
public void setAddress(Address address) {
    this.address = address;
}

/**
 * @return the password
 */
public String getPassword() {
    return password;
}
/**
 * @param password the password to set
 */
public void setPassword(String password) {
    this.password = password;
}
/**
 * @return the age
 */
public String getAge() {
    return age;
}
/**
 * @param age the age to set
 */
public void setAge(String age) {
    this.age = age;
}

}



 package com.junaid.spring.form;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* @author jamju02
*
*/
@Entity
@Table(name="EMPLOYEE")
public class Employee extends Person{

private String empId;
@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable
private List<Roles> userRoles = new ArrayList<Roles>();
@OneToMany(mappedBy="employee")
private List<Logins> loginDetails = new ArrayList<Logins>();
/**
 * @return the empId
 */
public String getEmpId() {
    return empId;
}
/**
 * @param empId the empId to set
 */
public void setEmpId(String empId) {
    this.empId = empId;
}
/**
 * @return the userRoles
 */
public List<Roles> getUserRoles() {
    return userRoles;
}
/**
 * @param userRoles the userRoles to set
 */
public void setUserRoles(List<Roles> userRoles) {
    this.userRoles = userRoles;
}
/**
 * @return the loginDetails
 */
public List<Logins> getLoginDetails() {
    return loginDetails;
}
/**
 * @param loginDetails the loginDetails to set
 */
public void setLoginDetails(List<Logins> loginDetails) {
    this.loginDetails = loginDetails;
}



}

================================================ ===== 包 com.junaid.spring.form;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * @author jamju02
 *
 */
@Embeddable
public class Address {
@Column(name="STREET_NAME")
private String street;
@Column(name="CITY_NAME")
private String city;
@Column(name="STATE_NAME")
private String state;
@Column(name="PINCODE")
private String pincode;
/**
 * @return the street
 */
public String getStreet() {
    return street;
}
/**
 * @param street the street to set
 */
public void setStreet(String street) {
    this.street = street;
}
/**
 * @return the city
 */
public String getCity() {
    return city;
}
/**
 * @param city the city to set
 */
public void setCity(String city) {
    this.city = city;
}
/**
 * @return the state
 */
public String getState() {
    return state;
}
/**
 * @param state the state to set
 */
public void setState(String state) {
    this.state = state;
}
/**
 * @return the pincode
 */
public String getPincode() {
    return pincode;
}
/**
 * @param pincode the pincode to set
 */
public void setPincode(String pincode) {
    this.pincode = pincode;
}

}

================================================ =======

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="springForm"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Save Page</title>
<style>
.error {
color: #ff0000;
font-style: italic;
font-weight: bold;
}
</style>
</head>
<body>

<springForm:form method="POST" action="register.do">
    <table>
        <tr>
            <td>First Name:</td>
            <td><springForm:input path="firstName" /></td>
            <td><springForm:errors path="firstName" cssClass="error" /></td>
        </tr>
         <tr>
            <td>Last Name:</td>
            <td><springForm:input path="lastName" /></td>
            <td><springForm:errors path="lastName" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><springForm:input path="email" /></td>
            <td><springForm:errors path="email" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Age:</td>
            <td><springForm:input path="age" /></td>
            <td><springForm:errors path="age" cssClass="error" /></td>
        </tr>


        <tr>
            <td>street:</td>
            <td><springForm:input path="street" /></td>
            <td><springForm:errors path="street" cssClass="error" /></td>
        </tr>
         <tr>
            <td>city:</td>
            <td><springForm:input path="city" /></td>
            <td><springForm:errors path="city" cssClass="error" /></td>
        </tr>
        <tr>
            <td>state:</td>
            <td><springForm:input path="state" /></td>
            <td><springForm:errors path="state" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Pincode:</td>
            <td><springForm:input path="pincode" /></td>
            <td><springForm:errors path="pincode" cssClass="error" /></td>
        </tr>

       <!--  <tr>
            <td>Gender:</td>
             <td><springForm:input path="sex" /></td>
            <td><springForm:errors path="sex" cssClass="error" /></td>
        </tr> -->
        <tr>
            <td>Birthday:</td>
            <td><springForm:password path="password" /></td>
            <td><springForm:errors path="password" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" value="Save Customer"></td>
        </tr>
    </table>


</springForm:form>

</body>
</html>

谁能帮我实现这个目标

【问题讨论】:

    标签: java hibernate spring-mvc


    【解决方案1】:

    你的 Person 类有一个 Address 的实例,它有一个 street 字段,所以你已经指定了这种方式

    <td><springForm:input path="address.street" /></td>
    

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 2017-05-09
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多