当前位置: 首页 > 知识库问答 >
问题:

百里香泉:绑定结果和 bean 名称“User”的普通目标对象都不能用作请求属性

鄂曦之
2023-03-14

当我想去 myweb/register.html 时,我有这个错误 有人可以给我建议我做错了什么?在注册中.html我突出显示了${User},*{firstName},*{lastName},*{email}和*{password}

路径为[]的上下文中servlet [dispatcherServlet]的Servlet.service()引发异常[请求处理失败;嵌套异常为org . thyme leaf . exceptions . template input exception:模板解析期间出错(模板:“类路径资源[templates/register.html]”),根本原因为

java.lang.IllegalStateExcgon:BindingResault和bean名称'user'的普通目标对象都不能作为请求属性使用

控制器:

    @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;

}

共有1个答案

楮自珍
2023-03-14

您必须将空用户对象添加到模型中。

尝试以下方法:

    @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;
 类似资料: