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

时间:2016-03-17 21:47:56

标签: spring forms spring-boot thymeleaf user-registration

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

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

但是,当我转到该页面时,我收到了本帖子标题中提到的错误。

以下是相关表格:

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

这是我的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
}

用户实体的第一部分:

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

从我所看到的,这里没有错,所以我被困住了。

我跟随此示例:https://github.com/cfaddict/spring-boot-intro

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我不知道你是否有这样的课程

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

因为如果你没有@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>

阅读你的html,你可能应该有:field =“* {confirmPassword}”而不是th:field =“$ {confirmPassword}”。 另一种认为在我看来不起作用的是你重复名字属性。我的建议是不要重复,让百万美元为数据绑定构建正确的属性。

答案 1 :(得分:1)

问题在于您声明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;
    }
}