我正在尝试使用休眠验证器验证我的表单。但是在运行我的表单时我收到错误
Could not parse as each: "${e : fields.errors('userName')}" (template: "home" - line 28, col 14)
这是我的家.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<form th:action="@{/process}" method="post" th:object="${loginData}">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Username</label>
<input
type="text"
class="form-control"
id="exampleInputEmail1"
name="userName"
aria-describedby="emailHelp"
th:value="${loginData.userName}"
th:classappend="${#fields.hasErrors('userName')?'is-invalid':''}"
>
<div th:each="${e : #fields.errors('userName')}" th:text="${e}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Email</label>
<input
type="text"
class="form-control"
name="email"
id="exampleInputPassword1"
th:value="${loginData.email}"
th:classappend="${#fields.hasErrors('email')?'is-invalid':''}"
>
<div th:each="${e:#fields.errors('email')}" th:text="${e}" class="invalid-feedback"></div>
</div>
<div class="mb-3 form-check">
<input
type="checkbox"
class="form-check-input"
id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button
type="submit"
class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
在这个文件中我使用了thymeleaf。
这是我使用过的实体类:
package org.springboot.entities;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
public class LoginData {
@NotBlank(message = "The textbox cannot be blank")
@Size(min = 3, max= 15, message = "The field should be of length between 3-15")
private String userName;
@NotBlank(message = "The textbox cannot be blank")
private String email;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "LoginData [userName=" + userName + ", email=" + email + "]";
}
}
我的家庭控制器:
package org.springboot.controllers;
import javax.validation.Valid;
import org.springboot.entities.LoginData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class HomeController {
@GetMapping("/home")
public String viewForm(Model model) {
model.addAttribute("loginData", new LoginData());
return "home";
}
@PostMapping("/process")
public String process
(@Valid @ModelAttribute("loginData") LoginData loginData, BindingResult result) {
if(result.hasErrors()) {
System.out.println(result);
return "home";
}
System.out.println(loginData);
return "success";
}
}
请指导我解决这个错误。我实际上想在我的登录表单上显示错误消息。
提前致谢!
在 home.html 中 尝试这个:- th:each =“e:$ {#fields.errors('userName')}”