Thymeleaf registration page - Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'

不想你离开。 提交于 2019-12-04 08:18:15
ekem chitsiga

The problem is the way you have declared your id property. The field uses a reference type Long which is null. The getter uses a primitive long. When Spring accesses the id field it tries to unbox a null value causing an error. Change your domain class to be

@Entity
@Table(name = "users")
public class User {

    // Default constructor required by JPA
    public User() {}

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Long id;

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

    public Long getId() {
        return id;
    }
}
Valerio Vaudi

I don't know if you have a class like this

@Controller
@RequestMapping("/users")
public class MyController{

    @RequestMapping("/register")
    public String registerAction(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("confirmPassword", "");
        return "views/users/register";
    }

    @RequestMapping(value="/register", method = RequestMethod.POST)
    public String doRegister(User user) {
        User savedUser = userService.save(user);
        return "redirect:/"; //redirect to homepage
    }
}

becouse if you don't have the @RequestMapping("/users") this is a problem becous if you dont have this annotation in you class the correct actione in the thymeleaf templace should be "@{/register}" other wise yon don't have the endpoint pubblished in other words with the methods that you posted you should have a template like this:

<form th:action="@{/register}" th:object="${user}" class="form-signin" method="POST">
    <h2 class="form-signin-heading">Register</h2>
    <input type="hidden" th:field="*{id}" />
    .... should be as you written
    <input type="password" th:field="*{confirmPassword}" id="inputConfirmPassword" class="form-control" placeholder="Confirm password" required="required" />
    <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
</form>

reading beter the your html you probably should have th:field="*{confirmPassword}" and not th:field="${confirmPassword}". another think that in my opinion don't works is that you repeate name attribute. The my advice is don't repeate and let to thymeleaf the work of build the correct attribute for databinding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!