Spring-Boot表单发布未绑定模型

问题描述 投票:0回答:1

我有点困惑,因为根据文档,这应该可以工作,但是在发布表单时,我所有的模型字段都为空。如果有人可以指出我所缺少的,那就太好了。

这是我的模特:

package hello.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

@Entity
public class Post {
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull
    private String email;

    @NotNull
    private String content;

    public Post() { }

    public Integer getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Post{" +
                "email='" + email + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

我的百里香片段,形式:

<div xmlns:th="http://www.thymeleaf.org" th:fragment="wysiwyg" class="panel-body">
    <form class="form-horizontal" role="form" method="post" action="#" th:action="@{/post}" th:object="post">
        <div class="form-group">
            <label for="email">Email</label>
            <input id="email" type="email" class="form-control" placeholder="Enter email..." th:field="*{email}"
                   required/>
            <small id="emailHelp" class="form-text text-muted">Enter your email address just in case.</small>
        </div>
        <div class="form-group">
            <label for="content">Post</label>
            <textarea id="content" class="form-control" th:field="*{content}"></textarea>
            <small id="bodyHelp" class="form-text text-muted">Type your post in here.</small>
        </div>
        <button id="btnPost" class="btn btn-primary" type="submit">Submit</button>
    </form>

    <script>
        $(document).ready(function() {
            $('#teszt2').summernote({
                "height": 200
            });
        });
    </script>
</div>

和适当的控制器方法:

@PostMapping("/post")
public String testPost(Post post, Model model) {
    System.out.println(post);
    return "index";
}

预期的行为是拥有Post {email = $ email,content = $ content},但是我得到的唯一是:Post {email = null,content = null},我不明白为什么。我究竟做错了什么?我想念什么?所有在线教程都声称这可以正常工作,但显然对我不起作用。

@@更新:

有点更新。所以我可以使它工作,但只能这样:

@GetMapping("/post")
public String openEditor(Model model) {
    model.addAttribute("post", new Post());
    model.addAttribute("content", "wysiwyg");

    return "index";
}

所以我创建了一个新的空白帖子。在我的片段中,我将形式从th:object =“ post”更改为th:object =“ $ {post}”。然后在我的PostMapping中:

@PostMapping("/post")
public String testPost(@ModelAttribute("post") Post post, Model model) {
    System.out.println(post); // Works well
    return "index";
}

为什么如果我不使用ModelAttribute并且首先不创建空的帖子,那么它不起作用。我希望它能正常工作……显然我错了

spring-boot spring-mvc java-8 thymeleaf summernote
1个回答
0
投票

我认为您不应将您的Entity对象直接与提交表单联系在一起。

我建议遵循标准做法来创建PostRequest类,并使用要从表单中获取的字段。在@PostMapping方法签名中声明为@RequestBody PostRequest postRequest。将对象包含在控制器方法中之后,您可以验证输入并从对象中创建实体,然后将其进一步用于存储在数据库中。

这种做法很简单,请勿将您的请求/响应对象与数据库实体对象混合使用。当您在数据库中存储任何敏感信息时,这也是有风险的。

不确定Thymeleaf模板如何工作,但是应该有一种方法可以遵循这种做法。希望这会有所帮助。

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