org.apache.jasper.JasperException:org.springframework.beans.NotReadablePropertyException:无效的属性'地址'豆类

时间:2015-04-20 13:58:32

标签: spring-mvc

控制器类

package com.techvision.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.techvision.model.Student;

@Controller
public class StudentController {

@RequestMapping(value="/student",method=RequestMethod.GET)
public ModelAndView student(){
    return new ModelAndView("student", "command", new Student());
}
 @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("studentweb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }
}

学生班

package com.techvision.model;

public class Student {

private Integer age;
private String name;
private Integer id;
private Contact contact;

public Contact getContact() {
    return contact;
}
public void setContact(Contact contact) {
    this.contact = contact;
}
public Integer getAge() {
    return age;
}
public void setAge(Integer age) {
    this.age = age;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

}

联系班级

package com.techvision.model;

public class Contact {
private Integer contactid;
private String address;
public Integer getContactid() {
    return contactid;
}
public void setContactid(Integer contactid) {
    this.contactid = contactid;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

的web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Form Handling</display-name>

<servlet>
    <servlet-name>helloweb</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>helloweb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

的HelloWeb-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.techvision" />
<mvc:annotation-driven/> 
   <bean     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

Student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/studentweb/addStudent">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="address">Contact</form:label></td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>  
</form:form>
</body>
</html>

我需要将学生数据插入学生表和学生地址联系表。

当我运行程序时,我收到以下错误。

  

HTTP状态500 - org.apache.jasper.JasperException:   org.springframework.beans.NotReadablePropertyException:无效   bean类的属性'address'[com.techvision.model.Student]:Bean   属性“地址”不可读或具有无效的getter方法:

getter的返回类型是否与setter的参数类型匹配?

1 个答案:

答案 0 :(得分:0)

由于您的地址属性与联系人嵌套,因此您的路径信息应如下所示。

<td><form:label path="contact.address">Contact</form:label></td>
<td><form:input path="contact.address" /></td>