首页 > 解决方案 > Thymeleaf th:field 在表单显示中出现错误

问题描述

该表单实际上在没有 th:field 属性的情况下显示,但在我添加 th:field 属性时停止。我不知道为什么它一直指向我在表单字段中使用的 th:field

    @Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Sections {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer Id;
    
    @NotNull
    @Column(unique = true)
    String sectionName;

}

服务等级

    @Service
public class SectionService {

    @Autowired
    SectionRepository sectionRepository;
    
    public void addNewSection(Sections section) {
    
        sectionRepository.save(section);
        
    }

    public List<Sections> listAllSections() {
        
        return sectionRepository.findAll();
    }


}

控制器类

    @Controller
public class AcademicsController {

    @Autowired
    SectionService sectionService;
    
    @GetMapping("/sections")
    public String showSectionsForm(Model model) {
        model.addAttribute("sectionObject", new Sections());
        model.addAttribute("sectionObject", sectionService.listAllSections());
        return "views/sections";
    }
    
    @PostMapping("/sections")
    public String processSectionsForm(@ModelAttribute("sectionObject") Sections section) {
        
        sectionService.addNewSection(section);
    
        return "views/sections";
    }   
    
}

.html 文件

<body>

    <div layout:fragment="content" class="main-container">
        <div class="text-center border shadow">
            <h2>Academics</h2>
        </div>
        <br>
        <div class="Form p-3 border" >
            <div class="row no-gutters">
                <div class="col-md-4 col-lg-4">
                    <form method="post" th:object="${sectionObject}" th:action="@{/sections}"
                        class="form1 border shadow">
                        <div class="form-row">
                            <div class="form-group">
                                    <label for="sectionName">Section Name</label> <input type="text"
                                        class="form-control"  
                                        placeholder="Enter Section" th:field="*{sectionName}" >
                                    
                            </div>
                            
                        </div>
                        <button type="submit" class="btn btn-primary">Add</button>
                    </form>
                </div>

                <div class="col-md-8 col-lg-8">
                <form method="post" th:object="${sectionObject}" th:action="@{/sections}" class="form2 border shadow">

html表单的一部分

                    <div class="form-row">
                        <div class="input-group">
                            <div class="form-group has-search">
                                <span class="fa fa-search form-control-feedback"></span> <!-- <input
                                    type="search" class="form-control" name="courseCode"> -->
                                    <input
                                    type="search" class="form-control" >

                            </div>
                            
                        </div>
                    </div>


                    </form>

显示数据库表记录的 html 表单的一部分

                    <div class="form2 border shadow" style="height:600px;">
                        <table class="table table-striped table-advance table-hover"
                            >

                            <thead class="thead-dark">
                                <tr>
                                    <th>Section Id</th>
                                    <th>Section Name</th>
                                    <th >Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="mySections:${sectionObject}">
                                <td th:text="${mySections.Id}"></td>
                                    <td th:text="${mySections.sectionName}"></td>
                                    
                                    <td>
                                        <div class="btn-group">
                                            <a href="#"><i class="btn btn-success fa fa-edit mr-2"></i></a>
                                            <a href="#"><i class="btn btn-danger fa fa-trash-alt"></i></a>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

错误

Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "views/sections" - line 22, col 39)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 86 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "views/sections" - line 22, col 39)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)                                    

部分错误代码省略

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'sectionName' of bean class [java.util.ArrayList]: Bean property 'sectionName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:627)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:617)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:158)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)
    at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227)
    at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 112 more

标签: javaspring-bootthymeleaf

解决方案


您的控制器对 2 个不同的模型属性使用相同的键:

model.addAttribute("sectionObject", new Sections());
model.addAttribute("sectionObject", sectionService.listAllSections());

我建议为所有部分的列表使用不同的名称。例如:

model.addAttribute("sectionObject", new Sections());
model.addAttribute("allSections", sectionService.listAllSections());

并更新您的 Thymeleaf 模板以使用新名称:

<tr th:each="mySections:${allSections}">

推荐阅读