使用Thymeleaf处理复选框

时间:2019-09-04 19:02:53

标签: spring spring-boot spring-mvc checkbox thymeleaf

我是Thymeleaf的初学者,我有一个列表,其中包含模型中包含的对象。我想通过选择复选框来更改属性之一。

使用Spring MVC Thymeleaf来实现。

胸腺视图

<form action="#" th:action="@{/ajouterProlongation}"
                th:object="${prolForm}" method="post">

                <table class="container">
                    <thead>
                        <tr>
                            <th>CLIENT</th>
                            <th>NOM</th>
                            <th>GROUPE</th>
                            <th>AGENCE</th>
                            <th>NMBE IMP</th>
                            <th>MNT_IMP</th>
                            <th>NOMBRE JOURS</th>
                            <th>SD</th>
                            <th>DEPASSEMENT</th>
                            <th>NOMBRE JOURS SDB</th>
                            <th>TOT CREANCE</th>
                            <th>MAX NBJ</th>
                            <th>ENGAGEMENT</th>
                            <th>CLASSE</th>
                            <th>MOTIF DE PROLONGATION</th>
                            <th>COMMENTAIRE</th>
                            <th>PROPOSE</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="c, itemStat : ${prolForm.transfertAnticipes}">
                            <input class="hidden"
                                th:name="|transfertAnticipes[${itemStat.index}].matricule|"
                                th:value="${util.matricule}" />
                            <input class="hidden"
                                th:name="|transfertAnticipes[${itemStat.index}].cli|"
                                th:value="${c.cli}" />

                            <input class="hidden"
                                th:name="|transfertAnticipes[${itemStat.index}].dateSysteme|"
                                th:value="${c.dateSysteme}" />

                            <td th:text="${c.cli}"></td>
                            <td th:text="${c.nom}"></td>
                            <td th:text="${c.groupe}"></td>
                            <td th:text="${c.agence}"></td>
                            <td th:text="${c.nbreImp}"></td>
                            <td th:text="${c.mntImp}"></td>
                            <td th:text="${c.nombreJours}"></td>
                            <td th:text="${c.sd}"></td>
                            <td th:text="${c.depassement}"></td>
                            <td th:text="${c.nombreJoursSdb}"></td>
                            <td th:text="${c.totCreance}"></td>
                            <td th:text="${c.maxNbj}"></td>
                            <td th:text="${c.engagement}"></td>
                            <td th:text="${c.classe}"></td>
                            <td><select th:style="'width: 80px'"
                                th:name="|transfertAnticipes[${itemStat.index}].motifProlC|">
                                    <option th:each="m : ${motifProl}" th:value="${m.codenv}"
                                        th:selected="${m.codenv == c.mott}" th:text="${m.libelle}"></option>
                            </select></td>
                            <td><input type="text" maxlength="120"
                                th:style="'width:60px ; background-color: #dce8e6; border:0'"
                                th:name="|transfertAnticipes[${itemStat.index}].obs1|"
                                th:value="${c.obs1}" /></td>
                            <td><input type="checkbox" name="${c.prolProposeCBoolean}"/></td>
                        </tr>
                    </tbody>
                </table>
                <div class="bouton">
                    <input class="btn btn-success submit" type="submit"
                        value="Sauvegarder" />
                </div>
                <div class="bouton">
                    <input class="btn btn-success submit" type="reset" value="Annuler" />
                </div>
            </form>

我的模特

public class TransfertAnticipeDto {

    private List<TransfertAnticipe> transfertAnticipes;

    public TransfertAnticipeDto() {
        this.transfertAnticipes = new ArrayList<TransfertAnticipe>();
    }

    public TransfertAnticipeDto(List<TransfertAnticipe> transfertAnticipes) {
        super();
        this.transfertAnticipes = transfertAnticipes;
    }

    public void addTransfertAnticipe(TransfertAnticipe transfertAnticipe) {
        this.transfertAnticipes.add(transfertAnticipe);
    }

    public List<TransfertAnticipe> getTransfertAnticipes() {
        return transfertAnticipes;
    }

    public void setTransfertAnticipes(List<TransfertAnticipe> transfertAnticipes) {
        this.transfertAnticipes = transfertAnticipes;
    }
} 

public class TransfertAnticipe {
    private String cli;
    ...,
    private String prolProposeC;
    private boolean prolProposeCBoolean;

    public TransfertAnticipe() {
        super();
        // TODO Auto-generated constructor stub
    }

    public TransfertAnticipe(
            String cli,
            ...,
            String prolProposeC,
            boolean prolProposeCBoolean) {
        super();
        this.cli = cli;
                ...
        this.prolProposeC = prolProposeC;
        this.prolProposeCBoolean = prolProposeCBoolean;
    }
}

我的控制器

    @RequestMapping(value = "/ajouterProlongation")
    public String ajouterProlongation(@ModelAttribute TransfertAnticipeDto form, BindingResult bindingResult,
            Model model) {
        for (TransfertAnticipe ta : form.getTransfertAnticipes()) {

            System.out.println("----Prolongation! : " + ta.isProlProposeCBoolean());
            }
        return "244251";
}

我添加了属性prolProposeCBool​​ean(初始化为false),以便在复选框单击后获得新值,但是我总是会得到false。 这是单击后的控制台预览:

----Prolongation! : false
----Prolongation! : false
----Prolongation! : false
----Prolongation! : false
----Prolongation! : false

我希望属性(prolProposeCBool​​ean)根据复选框更改(如果选中,则为true,否则为false)。

用th:value尝试了th:field,但是也没有用。

0 个答案:

没有答案
相关问题