正在创建表单上的输入数据但不会传递正在传递的值,我该如何解决这个问题?

问题描述 投票:0回答:2
  1. 我为学生设置了OneToMany映射,因此学生可能有多个地址。项目是正确映射的,但是当我通过表单持久化数据时,会为地址生成id(用户已经创建,我只需要单击添加地址,我也得到正确的用户)但是当我获取时值为null保存,这是我的java和jsp片段
@PostMapping("/addAddress")
  public String showFormForAddAddress(@RequestParam("studentId") int id, Model model) {
      Student student = studentService.findStudentById(id);
      Address address = new Address();
      model.addAttribute("address", address);
      List<Address> addresses = student.getAddresses();
      addresses.add(address);
      student.setAddresses(addresses);
      studentService.saveStudent(student);

      return "address-form";
  }

  @GetMapping("/addAddress")
  public String addAddress(@RequestParam("studentId") int id, Model model) {
      Address address = new Address();
      address.setStudentId(id);
      model.addAttribute("address", address);
      return "address-form";
  }

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Address Form</title>
</head>
<body>
    <div id="wrapper">
        <div id="header">
            <h2>SRM Student Relationship Manager (Address)</h2>
        </div>
    </div>
    <div id="container">
        <h3>Save Address</h3>
        <form:form action="addAddress" modelAttribute="address" method="POST">



              <form:input path="studentId" />

            <table>
                <tbody>
                    <tr>
                        <td><label>Address One</label></td>
                        <td><form:input path="addressOne" /></td>
                    </tr>
                    <tr>
                        <td><label>City</label></td>
                        <td><form:input path="city" /></td>
                    </tr>
                    <tr>
                        <td><label>State</label></td>
                        <td><form:input path="state" /></td>
                    </tr>
                    <tr>
                        <td><label>Zip Code</label></td>
                        <td><form:input path="zipCode" /></td>
                    </tr>
                    <tr>
                        <td><label>Address Two</label></td>
                        <td><form:input path="addressTwo" /></td>
                    </tr>
                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Save"></td>
                    </tr>
                </tbody>
            </table>
        </form:form>
    </div>
</body>
</html>
  1. 同样为了保证,这些是我的实体
@Entity
@Table(name = "address", catalog = "user_db")
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "address_id")
    private Integer id;
    private String addressOne;
    private String city;
    private String state;
    private String zipCode;
    private String addressTwo;
    @Column(name = "user_id")
    private Integer studentId;
@Entity
@Table(name = "Student", catalog = "user_db")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_id")
    private int id;

    private String firstName;
    private String lastName;
    private String email;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "student_id", referencedColumnName = "student_id")
    private List<Address> addresses;

我觉得这可能是一个命名问题,但我不能完全指责它,我也尝试删除帖子,只是使用get,仍然没有工作。

java hibernate jsp jstl
2个回答
1
投票

这应该是showForForAddMethod

@PostMapping("/addAddress")
    public String showFormForAddAddress(@RequestParam("studentId") int id, Model model,
            @ModelAttribute("address") Address address) {
        Student student = studentService.findStudentById(id);

        model.addAttribute("address", address);
        List<Address> addresses = student.getAddresses();
        addresses.add(address);
        student.setAddresses(addresses);
        studentService.saveStudent(student);

        return "redirect:/student/list";
    }

如果我创建一个新对象,则信息不会被映射,因此它会丢失,但是当我将地址作为参数中的模型属性传递时,这指向了学生对象


0
投票

在你的showFormForAddAddress方法中,你编写了model.addAttribute("address", address);,它将值放入模型属性中。

但是你试图从这种方法中获取地址信息,这是错误的。

您可以使用以下方法获取相关信息:

Map<String,Object> modelMap = model.getModel().asMap();
Object object = modelMap.get("address");

之后,您可以将结果对象转换为地址类型并使用它

© www.soinside.com 2019 - 2024. All rights reserved.