尝试执行表单验证时出错

Posted

技术标签:

【中文标题】尝试执行表单验证时出错【英文标题】:Getting error when trying to perform form validation 【发布时间】:2020-01-13 06:06:04 【问题描述】:

我正在为一个网站创建一个注册页面,但是当我转到该页面时出现错误:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错

控制器

@RequestMapping(value =  "/signup" , method = RequestMethod.POST)
    public ModelAndView createUser(@Valid AppUser appUser, BindingResult bindingResult) 
        ModelAndView model = new ModelAndView();
        AppUser appUserExists = appUserService.findByEmail(appUser.getEmail());

        if (appUserExists != null) 
            bindingResult.rejectValue("email", "error.user", "This email already exists!");
        

        if (bindingResult.hasErrors()) 
            model.setViewName("user/signup");
         else 
            appUserService.saveUser(appUser);
            model.addObject("msg", "User has been registered succesfully!");
            model.addObject("appuser", new AppUser());
            model.setViewName("user/login");
        

        return model;
    

表格

<form class="form-horizontal" role="form" th:action="@/signup" th:object="$appUser" method="post" style="border: 1px solid #ccc">
    <label for="name">First Name</label> 
    <input type="text" th:field="*firstname" class="form-control" id="firstname" placeholder="First Name" required autofocus /> 
    <label for="name">Last Name</label> 
    <input type="text" th:field="*lastname" class="form-control" id="lastname" placeholder="Last name" required autofocus /> 
    <label for="email"><b>Email</b></label>
    <input type="email" th:field="*email" class="form-control" id="email" placeholder="email@domain.com" required autofocus /> 
    <label for="psw"><b>Password</b></label> 
    <input type="password" th:field="*password" class="form-control" id="password" placeholder="Password" required />
</form>

AppUser 类

@Entity
@Table(name = "user")
public class AppUser 

    @Id
    @Column(name = "id_user", length = 10, nullable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "firstname", length = 30, nullable = false, unique = true)
    private String firstName;

    @Column(name = "lastname", length = 30, nullable = false, unique = true)
    private String lastName;

    @Column(name = "email", length = 30, nullable = false, unique = true)
    private String email;

    @Column(name = "password", length = 500, nullable = false, unique = false)
    private String password;

    @Column(name = "active", nullable = false)
    private int active;

    public AppUser() 
        super();
    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

       ....


据我所知,这里没有任何问题,所以我被卡住了。 有什么想法吗?

【问题讨论】:

在控制器中,您将用户命名为 appuser,但在模板中,您将其用作 appUser。这可能是问题所在。 @Diego,谢谢,没看到。我修改了它,但我仍然得到同样的错误。 @iuliiamaria 你也可以发布你的 AppUser 类 @ShameeraAnuranga 我编辑了帖子并添加了 AppUser 类 我认为它的回复在这里***.com/questions/48359208/… 【参考方案1】:

控制器中的 appuser 变量应该是 appUser。并且模板中的姓和名必须是类中的姓和名。

【讨论】:

以上是关于尝试执行表单验证时出错的主要内容,如果未能解决你的问题,请参考以下文章

将 jQuery 验证规则添加到动态生成的表单元素时出错

ajax - 执行 php 脚本和数据验证时出错 - 如何解决?

html5表单验证,无效表单操作并在所有html5表单元素都被验证时执行jQuery?

表单验证失败后向下滚动到表单

struts2 表单验证问题

同一按钮单击时的表单验证和 Ajax 调用