简体   繁体   English

jsp 页面中的 modelAttribute 在非输入字段上返回 null

[英]modelAttribute in jsp page returning null on non input fields

Im using modelAttribute to bind the object with the model class in my form, As you have to mention the varibale on the get request of the form before hand i did that and all the values are being fairly printed on the page,我在我的表格中使用modelAttribute将object与model class绑定在一起,正如你必须提到的,我之前在表格的get请求上的变量是公平的,并且所有的值都打印在页面上

Now on the page im taking an input of an attribute which is further mapped with another entity by a OnetoOne mapping.现在在页面上,我输入了一个属性,该属性通过 OnetoOne 映射进一步映射到另一个实体。 But on posting, only the OneToOne entity is returning the value, all the rest attributes are returning null.但是在发布时,只有 OneToOne 实体返回值,所有 rest 属性都返回 null。

Controller Controller

@GetMapping("/users/{username}/complaint")
    public String userRegisterComplaintRoute(Model model, @PathVariable String username) {
        model.addAttribute("user", userService.findSingleUserDetails(username));
        return "complaint-index";
    }

    @PostMapping("/users/complaint")
    public String userRegisterComplaintPostRoute(Model model, User user) {
        System.out.println(user);
        // userService.addUserDetails(user);

        return "redirect:/users/" + user.getName() + "/complaint/status";
    }

Form形式

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Kolkata | CRS</title>
    </head>
    <body>
        <h1>Username ${user.username}</h1>
        <h1>Username ${user.name}</h1>
        <h1>Username ${user.password}</h1>
        <h1>Username ${user.roles}</h1>

        <form:form
            action="/users/complaint"
            method="post"
            modelAttribute="user"
        >
            <form:label path="complaint.text" for="complaint"
                >Complaint</form:label
            >
            <form:input
                type="text"
                name="complaint"
                id="complaint"
                path="complaint.text"
            ></form:input>

            <button type="submit">Submit</button>
        </form:form>
    </body>
</html>

User Class用户 Class

package com.naha.crimereportingsystem.user;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.naha.crimereportingsystem.complaint.Complaint;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private String username;
    private String password;
    private boolean active = true;
    private String roles = "ROLE_USER";

    @OneToOne(targetEntity = Complaint.class, cascade = CascadeType.ALL)
    private Complaint complaint;

    public String getUsername() {
        return username;
    }

    public Complaint getComplaint() {
        return complaint;
    }

    public void setComplaint(Complaint complaint) {
        this.complaint = complaint;
    }

    public String getName() {
        return name;
    }

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

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public User() {
    }

    public User(int id, String name, String username, String password, boolean active, String roles) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.password = password;
        this.active = active;
        this.roles = roles;
        this.complaint = new Complaint();
    }

    @Override
    public String toString() {
        return "id: " + this.id + "\nname: " + this.name + "\nusername: " + this.username + "\ncomplaint:"
                + this.complaint.getText();
    }

}

Compaint Class Compaint Class

package com.naha.crimereportingsystem.complaint;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Complaint {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String text;
    private String status = "Investigation Pending";

    public long getId() {
        return id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

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

    public Complaint() {
    }

    public Complaint(long id, String text, String status) {
        this.id = id;
        this.text = text;
        this.status = status;
    }

}

toString output toString output 在此处输入图像描述

When you are posting a form, only the user input data are sent with request.当您发布表单时,只有用户输入数据随请求一起发送。 And only those data gets bound to command object.只有那些数据被绑定到命令 object。

One naive solution is to put hidden fields in the form.一种天真的解决方案是将隐藏字段放入表单中。 But Spring has a nice solution for this.但是 Spring 对此有一个很好的解决方案。

Use @SessionAttributes on your controller.在 controller 上使用@SessionAttributes

@SessionAttributes("commandNameHere")
public class FooController {
   // code

    @RequestMapping("/foo", method = POST)
    public String foo(SessionStatus status) {
    // code
    
    status.setComplete(); // marks for cleanup of session attributes
}

Spring will place the command object into a transparent session. Spring 会将命令 object 放入透明的 session 中。 Fields that are not found in form data, will be bound from that session.在表单数据中找不到的字段将从该 session 绑定。

Also, in your post mapping, use @ModelAttribute on the command object to resolve the command object from model.此外,在您的后期映射中,使用命令 object 上的@ModelAttribute来解析来自 model 的命令 object。


Further Reading延伸阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM