gpt4 book ai didi

java - 无法显示表单支持 bean 的验证错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:03 24 4
gpt4 key购买 nike

我很难尝试使用表单支持 bean 和该表单支持 bean 中的复杂对象来显示 Spring MVC 应用程序中的验证错误。

“hasErrors”功能似乎不支持“.”字符或无法解析复杂对象?

加载以下代码时,我可以显示表单。但是,在 validator 找到 null 作者姓名并重新加载 create.html 页面后,我得到了有关 Thymeleaf/Spring 如何无法评估我的 SPel 表达式的堆栈跟踪。

有谁知道为什么当表达式声明“author.name”时它会尝试加载“author.author”?就好像该属性(property)正在获得“作者”。添加到表达式的前面。

更新:经过大量故障排除后,我发现 validator 是问题所在,尽管我还不知道原因。

我已经包含了 validator 。

AuthorCommandObject.java

public class AuthorCommandObject {
private Author _author;
private Book _book;

public Author getAuthor() {
return _author;
}

public void setAuthor(Author author) {
_author = author;
}

public Author getBook() {
return _book;
}

public void setBook(Book book) {
_book = book;
}
}

作者.java

public class Author {
private long _authorId;
private String _name;

public long getAuthorId() {
return _authorId;
}

public void setAuthorId(long authorId) {
_authorId = authorId;
}

public String getName() {
return _name;
}

public void setName(String name) {
_name = name;
}
}

Book.java

public class Book {
private long _bookId;
private String _bookName;

public long getBookId() {
return _bookId;
}

public void setBookId(long bookId) {
_bookId = bookId;
}

public String getName() {
return _name;
}

public void setName(String name) {
_name = name;
}
}

AuthorFormController.java

    @Controller
@RequestMapping("/author")
public class AuthorFormController {

@InitBinder("authorCommandObject")
public void initBinder(WebDataBinder binder) {
binder.setValidator(new AutherCommandObjectValidator(new AuthorValidator(), new BookValidator()));
}

@RequestMapping(method=RequestMethod.GET, value ="/create", produces = "text/html")
public String createForm(Model model) {
AuthorCommandObject authorCommandObject = new AuthorCommandObject();
authorCommandObject.setAuthor(new Author());
authorCommandObject.setBook(new Book());
model.addAttribute("authorCommandObject", authorCommandObject);
return "/author/create";
}

@RequestMapping(method=RequestMethod.POST, value ="/save", produces = "text/html")
public String save(@Valid @ModelAttribute("authorCommandObject") AuthorCommandObject authorCommandObject, BindingResult result, Model model) {


if (result.hasErrors()) {
return "/author/create";
}

// the rest of the logic here

}
}

创建.html

    <form th:action="@{/author/save}" method="POST" class="form-horizontal" th:object="${authorCommandObject}">
Author Name:<input type="text" th:field="*{author.name}" />
<ul th:if="${#fields.hasErrors('author.name')}">
<li th:each="err : ${#fields.errors('author.name')}" th:text="${err}" />
</ul>
<input type="submit" value="Submit" class="btn btn-primary btn-lg" />
</form>

AuthorCommandObjectValidator.java

public class AuthorCommandObjectValidator implements Validator {

private final Validator _authorValidator;
private final Validator _bookValidator;

public AuthorCommandObjectValidator(Validator authorValidator, Validator bookValidator) {
_authorValidator = authorValidator;
_bookValidator = bookValidator;
}

@Override
public boolean supports(Class<?> clazz) {
return AuthorCommandObject.class.equals(clazz);
}

@Override
public void validate(Object obj, Errors errors) {
AuthorCommandObject authorCommandObject = (AuthorCommandObject)obj;
try {
errors.pushNestedPath("author");
ValidationUtils.invokeValidator(_authorValidator, authorCommandObject.getAuthor(), errors);

errors.pushNestedPath("book");
ValidationUtils.invokeValidator(_bookValidator, authorCommandObject.getBook(), errors);
} finally {
errors.popNestedPath();
}
}
}

AuthorValidator.java

public class AuthorValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Author.class.equals(clazz);
}

@Override
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "field.required");
}
}

部分堆栈跟踪(最后一行最有帮助)

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('author.name')"
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('author.name')"
org.springframework.beans.NotReadablePropertyException: Invalid property 'author.author' of bean class [com.sample.application.AuthorCommandObject]: Bean property 'author.author' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

最佳答案

之前你需要这样的标签:

<div th:object="${form}" th:remove="tag">
<ul>
<li th:each="err : ${#fields.errors('*')}" th:text="${err}" />
</ul>
</div>

关于java - 无法显示表单支持 bean 的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com