首页 > 解决方案 > Spring Boot,Thymeleaf 表单错误 => 我想从表中选择编辑行

问题描述

我想在另一个 html 页面中编辑表格的一行。我有一个 thymeleaf 和 Spring MVC 的初学者......

profileAll.html

在此处输入图像描述

但是,我有这个错误: org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputRadioFieldTagProcessor'时出错(模板:“profilsAll” - 第 39 行,第 26 列) ....

原因:java.lang.IllegalStateException:Bean 名称“profil”的 BindingResult 和普通目标对象都不能用作请求属性

ProfilesAll.html

<form th:action="@{/profiles/update}" th:object="${profil}" method="POST">
    <table border="1">
        <thead>
            <tr> 
                <th>Select</th> 
                <th>UID</th>
                <th>SIP</th>
                <th>enterpriseVoiceEnabled</th>
                <th>voicePolicy</th>
                <th>dialPlan</th>
                <th>samAccountName</th>
                <th>exUmEnabled</th>
                <th>exchUser</th>
                <th>objectClass</th>
                <th>statusProfile</th>
            </tr>
        </thead>
        <tbody>
            <tr th:if="${skypeProfiles.empty}">
                <td colspan="2"> No skype profile available </td>
            <tr th:each="skypeProfile, profile:${skypeProfiles}">
                <td>
                    <input type="radio" th:field="*{profile}" th:value="${profile}" />
                </td>
                <td th:text=${skypeProfile.collaboraterId}>UID</td>
                <td th:text=${skypeProfile.SIP}>SIP</td>
                <td th:text=${skypeProfile.enterpriseVoiceEnabled}>enterpriseVoiceEnabled</td>
                <td th:text=${skypeProfile.voicePolicy}>voicePolicy</td>
                <td th:text=${skypeProfile.dialPlan}>dialPlan</td>
                <td th:text=${skypeProfile.samAccountName}>samAccountName</td>
                <td th:text=${skypeProfile.exUmEnabled}>exUmEnabled</td>
                <td th:text=${skypeProfile.exchUser}>exchUser</td>
                <td th:text=${skypeProfile.objectClass}>objectClass</td>
                <td th:text=${skypeProfile.statusProfile}>statusProfile</td>
            </tr>   
        </tbody>
    </table>
    <button type="submit" value ="Edit">Edit</button>
</form>

控制器 :

@PostMapping("profiles/update")
public String profilesUpdate(@ModelAttribute("profil") SkypeProfileSearchBean skypeProfile) {
    
    System.out.print("skypeProfile id "+skypeProfile.getCollaboraterId());
    return "profilsUpdate";
}

这段代码有问题,但我没有找到解决方案:

<input type="radio" th:field="*{profile}" th:value="${profile}" />

非常感谢您的帮助

标签: javaspring-bootthymeleaf

解决方案


您无需拥有两个 skypeProfiles 实例即可在对象中使用一个并显示一个。只要有一个实例就足够了。参考 - https://www.baeldung.com/spring-boot-crud-thymeleaf

替换<tr th:each="skypeProfile, profile:${skypeProfiles}"><tr th:each="skypeProfile : ${skypeProfiles}">

<input type="radio" th:field="*{profile}" th:value="${profile}" /><input type="radio" th:field="*{skypeProfile}" th:value="${skypeProfile}" />


推荐阅读