gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:28:39 28 4
gpt4 key购买 nike

当我想转到 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com