【问题标题】:List of selected element not correctly set using a thymeleaf checkbox使用百里香复选框未正确设置所选元素的列表
【发布时间】:2017-08-23 16:06:26
【问题描述】:

在我的网页中,我想显示一个包含所有性质的列表,并且我希望将选定的性质设置在一个列表中。

我的问题是该列表在我显示的列表中正确设置,但未在所选元素列表中设置:自然。确实,不是我本性的属性字符串“nom”,而是存储它的引用:fr.cnamts.navigo.domain.Nature@19c4a02 by instance

这里是复选框显示:

我想要什么:当我通过实例检查 2 拳头复选框时,我希望在我的控制器中拥有 List<Nature> natures

natures.get(0).nom = "bcmc_envtraco_out"
natures.get(0).routage = INSTANCE

natures.get(1).nom = "bcmc_medtab_out"
natures.get(1).routage = INSTANCE

当它之前工作时,复选框是list<String>,我认为出现问题是因为我使用list<Nature>做错了。

这是我的相关网页代码:

<form action="#" th:action="@{/bus/topologie}"
    th:object="${topologie}" method="post" class="form-horizontal">
<div class="col-sm-10" th:if="!${#lists.isEmpty(allNature)}">
    <div th:each="nature : ${allNature}" class="checkbox">
        <label th:for="${#ids.next('nature')}"> 
         <input type="checkbox" th:field="*{natures}" th:value="${nature}" class="checkboxNature" />
         <span th:text="${nature.nom}" class="col-sm-5">...</span>
         <span th:text="${nature.routage.nom}" class="col-sm-5">...</span>
        </label>
    </div>

这段代码是这样解释的(复制自 chrome 中的 html 代码):

<div class="checkbox">
    <label for="nature1"> 
     <input type="checkbox" class="checkboxNature" value="fr.cnamts.navigo.domain.Nature@19c4a02" id="natures3" name="natures"><input type="hidden" name="_natures" value="on">
     <span class="col-sm-5">bcmc_trabcmreca_out</span>
     <span class="col-sm-7">Routage sur instance</span>
    </label>
</div>

我的控制器中的相关代码:

@ModelAttribute("allNature")
public List<Nature> getAllNatures(Topologie topologie) throws Exception
{
    return natureService.getNaturesByVersionCadre(topologie.getCadre(), topologie.getVersionCadre());
}

以及我的对象“拓扑”中的相关代码:

public class Topologie {
    private List<Nature> natures = new ArrayList<Nature>();

最后是自然类:

public class Nature {

    @NotBlank
    private String nom;

    @NotNull
    private Routage routage;

    // @NotNull
    // private String typeCl;

    public enum Routage {
        INSTANCE("Routage sur instance", "^[A-Za-z0-9]{2}$ (instance)"), UCANSS(
                "Routage sur code UCANSS", "^[A-Za-z0-9]{2}$ (Code UCANSS)"), ACOSS(
                "Routage sur code ACOSS", "^[A-Za-z0-9]{2}$ (Code ACOSS)"), INSTANCE_MIAM(
                "Routage sur code instance MIAM", "^[A-Za-z0-9]{2}$ (instance)"), CODEREGIME_CODECAISSE(
                "Routage sur coderégime+code caisse",
                "^[0-9]{2}[0-9]{3}$  (Code régime code caisse)");

        private final String nomRoutage;
        // private final String codecle;
        private final String regExp;

        Routage(String nom, String regexp) {
            this.nomRoutage = nom;
            this.regExp = regexp;
        }

        public String getNom() {
            return nomRoutage;
        }

        public String getRegExp() {
            return regExp;
        }
    }

    public Nature(String nomNature) {
        nom = nomNature;
        // TODO à modifier une fois récup faite dans fichier zk
        routage = Routage.INSTANCE;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public Routage getRoutage() {
        // TODO à modifier une fois récup faite dans fichier zk
        if (routage == null) {
            routage = Routage.INSTANCE;
        }
        return routage;
    }

    public void setRoutage(Routage routage) {
        // TODO à modifier une fois récup faite dans fichier zk
        if (routage == null) {
            routage = Routage.INSTANCE;
        }
        this.routage = routage;
    }
}

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    尝试将您的 th:value 属性值更改为 ${nature.nom}

    就像这样:

    <input type="checkbox" th:field="*{natures}" th:value="${nature.nom}" class="checkboxNature" />
    

    【讨论】:

    • 感谢您的回答,但不会改变行为。
    • 好的,我检查过了,Thymeleaf 应该自己处理 th:checked 属性。然后尝试将 th:value 更改为 th:value="${nature.nom}"。
    • 非常感谢。你可以用这个改变你的答案,我会验证它;)
    • 完成 :) 很高兴能为您提供帮助。
    猜你喜欢
    • 2017-01-18
    • 2019-10-15
    • 2016-10-11
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2019-10-14
    相关资源
    最近更新 更多