我正在尝试使用 @ModelAttribute 绑定数据。我有两个名为Address和Student的用户定义类。这两个数据类的结构如下:
Address.java :
package org.manya.dataClasses;
public class Address {
private String city;
private String state;
public Address(String city,String state)
{
this.city = city;
this.state = state;
System.out.println("from the constructor of Address");
}
public Address()
{
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Student.java :
package org.manya.dataClasses;
public class Student {
private String name;
private int age;
private Address studentAddress;
public Student(String name,int age,Address studentAddress)
{
System.out.println("from the constructor of Student = ");
if(studentAddress == null)
{
System.out.println("address is null");
}
this.name = name;
this.age = age;
this.studentAddress= studentAddress;
}
public Student()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return studentAddress;
}
public void setAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
使用@ModelAttribute我试图绑定学生对象,但仅限于 String和int属性被绑定,但 Address依赖项仅为null。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
**frontController-servlet.xml**
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.manya.Controllers"></context:component-scan>
<mvc:annotation-driven/>
<!-- <mvc:resources location="/resources/**" mapping="/home/*"></mvc:resources>
-->
<mvc:resources location="/resources/**" mapping="/static/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/resources/"/>
<property name="suffix" value=".html"/>
</bean> -->
</beans>
StudentController.java
package org.manya.Controllers;
import org.manya.dataClasses.Address;
import org.manya.dataClasses.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/student")
public class StudentController {
@GetMapping("/studentForm")
public String admissionForm()
{
return "admissionForm";
}
@PostMapping("/submitted")
public ModelAndView submitted(@ModelAttribute("student") Student stud)
{
ModelAndView mv = new ModelAndView("submitted");
return mv;
}
}
admissionForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- <form action="./submitted" method="post">
NAME : <input name="name"/>
AGE : <input name="age"/>
CITY : <input type="text" name="city"/>
STATE : <input type="text" name="state"/>
SUBMIT : <input type="submit"/>
</form> -->
<form action="./submitted" method="post">
NAME : <input name="name"/>
AGE : <input name="age"/>
CITY : <input type="text" name="studentAddress.city"/>
STATE : <input type="text" name="studentAddress.state"/>
SUBMIT : <input type="submit"/>
</form>
</body>
</html>
答案 0 :(得分:1)
In the Student
class you have getter and setter for studentAddress
field as getAddress()
and setAddress
. I think you need to change these method names to getStudentAddress()
and setStudentAddress()
. Let me know if it works.