java - 百里香 + Spring : Neither BindingResult nor plain target object for bean name 'User' available as request attribute

标签 java html spring model-view-controller placeholder

当我想转到 myweb/register.html 时出现该错误 有人可以给我建议我做错了什么吗? 在 register.html 中我突出显示了 ${User}、*{firstName}、*{lastName}、*{email} 和 *{password}

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/register.html]")] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'User' available as request attribute

Controller :

    @RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("register"); // resources/templates/register.html
    return modelAndView;

注册.html:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Registration Form</title>
    <!-- link rel="stylesheet" type="text/css" th:href="@{/css/registration.css}" /-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-color: #ededed;">
<div style="background-color: #337ab7; height: 50px;"></div>
<div class="container-fluid" style="margin-top: 30px;">

    <div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;">
        <form autocomplete="off" action="#" th:action="@{/register}" th:object="${User}" method="POST" class="form-signin" role="form">
            <h3 class="form-signin-heading">Registration Form</h3>
            <div class="form-group">
                <div class="">
                    <input type="text" th:field="*{firstName}" placeholder="Name" class="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="">
                    <input type="text" th:field="*{lastName}" placeholder="Last Name" class="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="">
                    <input type="text" th:field="*{email}" placeholder="Email" class="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="">
                    <input type="password" th:field="*{password}" placeholder="Password" class="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="">
                    <button type="submit" class="btn btn-primary btn-block">Register User</button>
                </div>
            </div>
            <span th:utext="${successMessage}"></span>
        </form>
    </div>
</div>
</body>
</html>

用户.java:

package roster.schedule.user;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import roster.schedule.role.Role;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "auth_user")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "auth_user_id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    @Column(name = "technical")
    private byte technical;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "auth_user_role", joinColumns = @JoinColumn(name = "auth_user_id"), inverseJoinColumns = @JoinColumn(name = "auth_role_id"))
    private Set<Role> roles;

}

最佳答案

您必须将空的 User 对象添加到模型中。

尝试以下操作:

    @RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
    ModelAndView modelAndView = new ModelAndView();

    modelAndView.addObject("User", new User());

    modelAndView.setViewName("register"); // resources/templates/register.html
    return modelAndView;

关于java - 百里香 + Spring : Neither BindingResult nor plain target object for bean name 'User' available as request attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58016943/

相关文章:

java - 更改 ThreadPriorityPolicy 范围

java - 打开图像资源 Java JDK 8

java - 打破java中的递归

javascript - jQuery 随机化图片

java - Spring Boot 集成测试抛出错误并且无法在另一个 Maven 模块中获取现有 bean

java - 在 Spring 与 Java 配置集成中接收邮件

java - Android 观看多个EditText

html - 通过 CSS 动态应用 !important

html - 如何使用 css 使表格 td 占据整个宽度?

java - 在 Spring roo 应用程序中显示 jasper 报告 HTML 页面(Web 应用程序)