#sets.contains从百里香叶总是返回false

时间:2019-02-13 20:37:01

标签: spring set thymeleaf contains

在我当前的Spring项目中,我试图显示一个复选框列表,可以根据服务器中的值进行检查。这是html代码:

      <tr th:each="credencial : ${credenciais}">
        <td>
          <input type="checkbox" class="form-check-input" th:id="${credencial.id}" th:checked="${#sets.contains(command.credenciais, credencial)}? 'checked': ''" th:attr="data-usuario=${command.id},data-url=@{/usuario/toggle_credencial}" onchange="toggle_credencial(this)"/>
        </td>

        <td>
          <span th:text="${credencial.nome}"></span>
        </td>

        <td>
          <ul>
            <li th:each="autorizacao : ${credencial.autorizacoes}" th:text="${autorizacao.nome}"></li>
          </ul>
        </td>
      </tr>

但是当我运行该应用程序并打开此页面时,未选中任何复选框,因为#sets.contains始终返回false。

这里要比较的班级是:

@Entity
public class Credencial extends Model {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;

  @Column
  private String nome;

  @OneToMany(fetch = FetchType.EAGER)
  @Fetch(FetchMode.SELECT)
  private List<Autorizacao> autorizacoes;

...

  @Override
  public String toString() {
    return nome;
  }
}

我在超类equals中实现方法Model

public abstract class Model extends Object {
  public abstract Object getId();

  public boolean equals(Model object) {
    return toString().equals(object.toString());
  }

  public abstract String toString();
}

有人知道这是怎么回事吗?

1 个答案:

答案 0 :(得分:0)

th:checked attribute in Thymeleaf expects a true/false value,而不是字符串。这值得吗?

th:checked="${#sets.contains(command.credenciais, credencial)}"