【问题标题】:Setting boolean with th:checked使用 th:checked 设置布尔值
【发布时间】:2017-07-14 01:58:18
【问题描述】:

我有一个模型,其中包含一个布尔值“已删除”。我生成一个列表,每个元素中都有这个变量。

我将它打印在一个列表中,并尝试使用复选框进行设置,因此它会保存设置的任何内容。

型号

public class entry{
private boolean deleted;

public entry(boolean deleted){
this.deleted = deleted;
}

//setters & getters for deleted
}

然后我使用包装类在此列表中读取我的 thymeleaf 表单。哪个工作正常。

我可以让复选框根据每个元素的已删除状态显示选中/未选中,但如果我将复选框从选中更改为未选中...它不会将变量保存到该状态。

HTML

<div th:each="entry, stat: *{entryList}">
  <td>
    <label class="switch"> 
      <input name="index" th:type="checkbox" th:checked="*{entryList[__${stat.index}__].deleted}">
      </input> <span class="delete round">
    </span>
    </label>
 </td>
       </div>

TH:对象

<form action="#" th:action="@{submitData}" class="saveForm"
            method="post" th:object="${entryWrapper}">
...
...
</form>

包装类

public class EntryWrapper {

    @Autowired
    private List<Entry> entryList;

    public EntryWrapper(){

    }

    public EntryWrapper(List<Entry> entryList){
        this.entryList = entryList;
    }

    public List<Entry> getEntryList(){
        return entryList;
    }

    public void add(final Entry entry){
        this.entryList.add(entry);
    }

    public void setEntryList(List<Entry> entryList){
        this.entryList = entryList;
    }
}

【问题讨论】:

  • entryList 是什么样的?列出?另外,你的 th:object 是什么?
  • 已包含包装类内容和 th:object。 @{submitData} 只是将用户带到列表视图,而无法编辑条目。

标签: java checkbox thymeleaf


【解决方案1】:

你应该使用th:field而不是th:checked

<input type="checkbox" th:field="*{entryList[__${stat.index}__].deleted}">

th:field 将生成名称、id 和检查属性,因此当您发布表单时,它应该填充您的模型对象。

【讨论】:

  • 谢谢 :) 我试了一下,但根本无法选中复选框。但是,我相信这是使用
猜你喜欢
  • 2020-09-10
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2020-05-21
相关资源
最近更新 更多