spring Thymeleaf 注册页面 - 执行处理器“org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor”时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36072304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:00:06  来源:igfitidea点击:

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

springformsspring-bootthymeleafuser-registration

提问by MC123

I'm making a registration page for a website. I understand that in order for a new User to be created, an id is required, so we have the field:

我正在制作一个网站的注册页面。我知道为了创建一个新用户,需要一个 id,所以我们有这个字段:

<input type="hidden" th:field="{*id} />

However, when I go to the page, I get the error I mentioned in this post's title.

但是,当我转到该页面时,我收到了这篇文章标题中提到的错误。

Here is the form in question:

这是有问题的表格:

<form th:action="@{/users/register}" th:object="${user}" class="form-signin" method="POST">
    <h2 class="form-signin-heading">Register</h2>
    <input type="hidden" th:field="*{id}" />
    <label for="inputUsername" class="sr-only">Username*</label>
    <input type="text" th:field="*{username}" name="username" id="inputUsername" class="form-control" placeholder="Username" required="required" autofocus="autofocus" />
    <label for="inputEmail" class="sr-only">Email Address*</label>
    <input type="text" th:field="*{email}" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus" />
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" th:field="*{password}" name="password" id="inputPassword" class="form-control" placeholder="Password" required="required" />
    <label for="inputConfirmPassword" class="sr-only">Confirm Password</label>
    <input type="password" th:field="${confirmPassword}" name="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>

Here is my UserController:

这是我的用户控制器:

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

And the first part of the User entity:

以及 User 实体的第一部分:

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

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

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

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

public long getId() {
    return id;
}

From what I can see, there's nothing wrong here so I'm stuck.

据我所知,这里没有任何问题,所以我被卡住了。

I'm following this example: https://github.com/cfaddict/spring-boot-intro

我正在关注这个例子:https: //github.com/cfaccine/spring-boot-intro

Any ideas?

有任何想法吗?

采纳答案by 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

问题在于您声明 id 属性的方式。该字段使用引用类型 Long,该类型为 null。getter 使用原始 long。当 Spring 访问 id 字段时,它会尝试将导致错误的空值拆箱。将您的域类更改为

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

回答by 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:

因为如果你没有,@RequestMapping("/users")这是一个问题,因为如果你在课堂上没有这个注释,百里香叶中的正确动作应该是“@{/register}”,否则你没有在其他地方发布端点带有您发布的方法的单词应该有一个这样的模板:

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

更好地阅读你的 html 你可能应该有 th:field="*{confirmPassword}" 而不是 th:field="${confirmPassword}"。另一个认为在我看来不起作用的是你重复了 name 属性。我的建议是不要重复,让 thymeleaf 为数据绑定构建正确的属性。