使用 thymeleaf 复选框未正确设置所选元素的列表 [英] List of selected element not correctly set using a thymeleaf checkbox

查看:29
本文介绍了使用 thymeleaf 复选框未正确设置所选元素的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是复选框显示:

我想要的:当我通过实例检查 2 拳头复选框时,我希望在我的控制器中具有 List;自然重视

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

当它以前工作时,复选框是一个list,我认为出现问题是因为我在使用list时做错了.>

这是我的相关网页代码:

<form action="#" th:action="@{/bus/topologie}"th:object="${topologie}" method="post" class="form-horizo​​ntal"><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>

此代码的解释如下(从 chrome 中的 html 代码复制):

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

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

@ModelAttribute("allNature")公共列表<自然>getAllNatures(拓扑拓扑)抛出异常{return natureService.getNaturesByVersionCadre(topologie.getCadre(), topologie.getVersionCadre());}

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

公共类拓扑{私人名单<自然>性质 = 新的 ArrayList();

最后是自然课:

public class Nature {@NotBlank私人字符串名称;@NotNull私人路线路线;//@NotNull//私有字符串 typeCl;公共枚举路由{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)");私人最终字符串nomRoutage;//私有最终字符串编码器;私人最终字符串正则表达式;路由(字符串nom,字符串正则表达式){this.nomRoutage = nom;this.regExp = regexp;}公共字符串 getNom() {返回 nomRoutage;}公共字符串 getRegExp() {返回正则表达式;}}公共性质(字符串nomNature){nom = nomNature;//TODO à modifier une fois récup faite dans fichier zkroutage = Routage.INSTANCE;}公共字符串 getNom() {返回名称;}公共无效setNom(字符串nom){this.nom = nom;}公共路由 getRoutage() {//TODO à modifier une fois récup faite dans fichier zk如果(路由==空){routage = Routage.INSTANCE;}回程;}公共无效设置路由(路由路由){//TODO à modifier une fois récup faite dans fichier zk如果(路由==空){routage = Routage.INSTANCE;}this.routage = routage;}}

解决方案

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

就像这样:

In my web page I want to display a list containing all natures and I want the selected ones to be set in a list.

My issue is that the list is correctly set in my list displayed but not in the list of selected elements : natures. Indeed, instead of the attribute String "nom" of my nature, it's its reference that is stored : fr.cnamts.navigo.domain.Nature@19c4a02 by instance

Here is the checkbox display :

What I want : When I check by instance the 2 fist checkbox, I want to have in my controller the List<Nature> natures valued to

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

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

When it worked before, the checkbox was a list<String>, I think the problem occured because I do something wrong using the list<Nature>.

Here is my relevant web page code :

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

This code is interpreted like this (copy from the html code in chrome) :

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

The relevant code in my controller :

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

And the relevant code in my object "Topologie" :

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

And eventually the Nature Class :

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

解决方案

Try to change your th:value attribute value to ${nature.nom}

Just like this:

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

这篇关于使用 thymeleaf 复选框未正确设置所选元素的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆