带有Thymeleaf的复选框和Spring MVC中的自定义列表对象(引导)

我正在使用Thymeleaf和Spring Boot以及MVC . 我所拥有的是一个表单,其中某些复选框正在填充从控制器添加到模型中的对象(q1)

我想知道的是,如何正确地使用Thymeleaf将所选复选框与我的结果对象(surveyData)绑定到(复杂)对象列表中 .

Edit 这也是我第一次不确定问题出在何处 . 我确实尝试调试 lot 但无法确定问题的确切位置 . 我尝试了各种不同的组合来实现这一目标并获得了各种不同的结果(当然主要是例外) . 我很难在这里发布所有这些内容,所以如果你能提出我想要尝试的内容并发布结果 .

到目前为止我所拥有的:(复选框的th:字段显然不起作用)

Code:

HTML代码段:

<form action="#" th:action="@{/controlleraction}" method="post" th:object="${surveyData}">
  <table class="table table-striped jambo_table bulk_action" id="table-resp">
    <thead>
      <tr>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <tr th:each="act,iter : ${q1}">
        <td>
          <input type="checkbox" class="check" th:value="${act.optionId}" th:field="*{keyResponsibilities.optionId.optionId}" />
        </td>
    </tbody>
  </table>
</form>

SurveyData.java

@Entity
@Table(name = "survey_data")
public class SurveyData implements Serializable{
    @Id
    @Column(name = "some_id")
    private Long someId;

    @ElementCollection
    @CollectionTable(name = "key_responsibilities", joinColumns = @JoinColumn(name = "data_id"))
    private List<KeyResponsibilities> keyResponsibilities = new ArrayList<>();


    public List<KeyResponsibilities> getKeyResponsibilities() {
        return keyResponsibilities;
    }

    public void setKeyResponsibilities(List<KeyResponsibilities> keyResponsibilities) {
        this.keyResponsibilities = keyResponsibilities;
    }
}

KeyResponsibilities.java

@Embeddable
public class KeyResponsibilities {
    @OneToOne
    @JoinColumn(name = "option_id")
    private OptionsMaster optionId;

    @Column(name = "other")
    private String other;

    public OptionsMaster getOptionId() {
        return optionId;
    }

    public void setOptionId(OptionsMaster optionId) {
        this.optionId = optionId;
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other;
    }
}

OptionsMaster.java

@Entity
@Table(name = "options_master")
public class OptionsMaster implements Comparable<OptionsMaster> {
    private int optionId;
    private String text;

    @Id
    @Column(name = "option_id")
    public int getOptionId() {
        return optionId;
    }

    public void setOptionId(int optionId) {
        this.optionId = optionId;
    }

    @Basic
    @Column(name = "text", length = -1)
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public int compareTo(OptionsMaster o) {
        return Integer.compare(this.getOptionId(),o.getOptionId());
    }

}

回答(0)