Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法显示表单支持bean的验证错误_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

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

Java 无法显示表单支持bean的验证错误,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我很难在SpringMVC应用程序中使用表单支持bean和表单支持bean中的复杂对象来显示验证错误 “hasErrors”功能似乎不支持“.”字符或无法解析复杂对象 加载以下代码时,我能够显示表单。但是,在验证器找到一个空作者姓名并重新加载create.html页面后,我得到一个stacktrace,说明Thymeleaf/Spring如何无法计算我的SPel表达式 有人知道当表达式声明为“author.name”时,为什么要加载“author.author?这就好像属性在表达式前面加了“au

我很难在SpringMVC应用程序中使用表单支持bean和表单支持bean中的复杂对象来显示验证错误

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

加载以下代码时,我能够显示表单。但是,在验证器找到一个空作者姓名并重新加载create.html页面后,我得到一个stacktrace,说明Thymeleaf/Spring如何无法计算我的SPel表达式

有人知道当表达式声明为“author.name”时,为什么要加载“author.author?这就好像属性在表达式前面加了“author.”

更新: 经过大量的故障排除,我发现验证器是问题所在,尽管我还不知道为什么

我已经包括了验证器

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>

在执行以下操作之前,您需要此类标记:



post getter和setter也在这里,这可能是问题所在。我认为您应该计算#fields.hasrerrors('name'),因为您是“内部”表单,该表单包含th:object=“${authorCommandObject}”所以表达式求值是为了在表单对象上找到属性author,而表单对象本身就是author?这不起作用,因为authorCommandObject包含一个author,而不是名称。所以我应该能够使用author.name而不仅仅是“name”。
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();
        }
    }
}
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?