/ / Не мога да покажа грешки при потвърждаване на формуляра за поддръжка на боб - java, spring-mvc, thyleneaf

Невъзможно е да се показват грешки при потвърждаване на блан за поддръжка на формуляри - java, spring-mvc, tymeleaf

Трудно ми е да се опитвам да покажа грешки при валидиране в моето пролетно MVC приложение, използвайки боб с формална подкрепа и сложни обекти в този боб, който поддържа формата.

Изглежда функцията "hasErrors" не поддържа "." символи или не може да разреши сложни обекти?

При зареждането на следния код аз съм в състояние да покажаформата. Въпреки това, след като валидаторът намери името на нулевия автор и презареди страница create.html, получавам информация за това как Thymeleaf / Spring не може да оцени израза ми на SPel.

Някой знае ли защо се опитва да зареди "автор.автор", когато изразът" име на автор "е?" Сякаш имотът се "автор". пренасочен към израза.

UPDATE: След много отстраняване на проблеми открих, че валидаторът е проблемът, въпреки че все още не знам защо.

Включих валидаторите.

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

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

}
}

create.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");
}
}

Частично Stacktrace (последната линия е най-полезна)

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?

Отговори:

0 за отговор № 1

Трябва ви такъв тип маркер преди:

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