2
votes

I have that error when I want go to myweb/register.html Someone can give me advice what I doing wrong? In register.html I have highlighted ${User}, *{firstName}, *{lastName}, *{email} and *{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;

Register.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>

User.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
You showed a controller. You don't set a model value named User anywhere in it.chrylis -cautiouslyoptimistic-

1 Answers

1
votes

You have to add empty User object to the model.

Try following:

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