代码之家  ›  专栏  ›  技术社区  ›  kumarmo2

Spring MVC@ModelAttribute不使用用户定义的依赖项

  •  2
  • kumarmo2  · 技术社区  · 8 年前

    我正在尝试使用 @模型属性 . 我有两个用户定义的类,分别是Address和Student。这两个数据类的结构如下:

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

    大学生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;
        }
    
    }
    

    使用@modeldattribute,我试图绑定 学生对象 但只有 字符串和int属性正在绑定,但 地址依存关系仅为null。

    网状物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>
    

    学生控制器。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;
    
        }
    }
    

    <%@ 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>
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Sattyaki    8 年前

    Student 你有getter和setter的类 studentAddress 字段as getAddress() setAddress . 我认为您需要将这些方法名称更改为 getStudentAddress() setStudentAddress() . 如果有效,请告诉我。