【问题标题】:How form data are binding to the DTO class' private members by spring MVCSpring MVC 如何将表单数据绑定到 DTO 类的私有成员
【发布时间】:2018-02-17 16:01:33
【问题描述】:

我正在开发一个 Spring MVC 项目,我正在从 UI 表单中获取一些数据。我有一个包含私有成员的 DTO 类。当我提交表单数据时,由于@RequestMapping 注释,FrontController(Dispatcher Servlet) 会调用 Controller 类的方法。在此方法中,我将 DTO 类的引用作为参数传递,并通过调用 getter 方法在控制台上打印 DTO 类的成员变量的值。 但我不明白 DTO 类的对象是如何自动创建的,并且表单数据是如何绑定到 DTO 类的成员变量的。请告诉我它是如何工作的?

控制器类

package com.soni.web.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.soni.web.dto.StudentRecordsDTO;

@Component
@RequestMapping("/")
public class StudentRecords {

    public StudentRecords() {
System.out.println(getClass().getName());
    }

    @RequestMapping(value="/signup", method = RequestMethod.POST)
    public String signup(StudentRecordsDTO studentRecordsDTO) {

    System.out.println(studentRecordsDTO.getId());
    System.out.println(studentRecordsDTO.getName());

    return "/registration_success.html";
   }

}

DTO 类

package com.soni.web.dto;

public class StudentRecordsDTO {

    private int id;
    private String name;

    public StudentRecordsDTO(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

HTML 表单

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="signup" method ="post">
<input type  = "num" name="id">
<input type = "text" name="name">
<input type = "password" name="pwd">
<input type = "num" name = "nm">
<input type = "submit" value="press">
</form>
</body>
</html>

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    由于您使用的是 POST 方法,因此您作为 BODY(JSON 数据)的一部分发送的任何内容都将转换为 POJO(普通的旧 java 对象),在这种情况下,即 StudentRecordsDTO,即您的 signup() 方法的参数。

    如果您从客户端发送的 json 数据看起来像这样

    {
      "id" : 1,
      "name" : "Chirag"
    }
    

    然后在内部发生 JSON 反序列化时,更多关于序列化/反序列化here

    “id”映射到类中的“id”属性,并使用java约定的setter方法将值设置为对象。

    同样,“name”映射到类中的“name”属性。

    所以,内部你可以这样想。

    StudentRecordsDTO studentRecordsDTO = new StudentRecordsDTO();
    studentRecordsDTO.setId(1);
    studentRecordsDTO.setName("Chirag")
    

    这个 studentRecordsDTO 对象被传递给你的控制器方法,其值设置为你传递的值。

    您也可以在此处使用@RequestBody 以提高可读性。

    @RequestMapping(value="/signup", method = RequestMethod.POST)
    public String signup(RequestBody StudentRecordsDTO studentRecordsDTO) {
    
      System.out.println(studentRecordsDTO.getId());
      System.out.println(studentRecordsDTO.getName());
    
      return "/registration_success.html";
    }
    

    所以 spring 在后台为你做这件事。你也可以看看

    @RequestHeader 设置作为请求的一部分直接传递的标头。 @RequestParam 在 GET 方法的情况下额外的请求参数。

    spring 提供了其他各种抽象(和定制),因此您可以专注于核心业务逻辑。

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 2016-07-15
      • 1970-01-01
      • 2015-02-02
      • 2013-04-04
      • 1970-01-01
      • 2016-04-20
      • 2010-09-22
      • 1970-01-01
      相关资源
      最近更新 更多