【问题标题】:How to store a DTO in thymeleaf th:value? (Spring, Java)如何将 DTO 存储在 thymeleaf th:value 中? (春天,Java)
【发布时间】:2021-06-09 05:19:06
【问题描述】:

我有这个 DTO 和各自的 getter 和 setter。

/** This class encapsulates the attributes of a JSON Schema database object. */ 
public class SchemaDto {

  /** The JSON String representation. */
  private String json;

  /** The note of the schema. */
  private String note;

  /** The date of the schema. */
  private LocalDate date;

...

  /** Returns a unique string representation of the schema. */
  @Override
  public String toString() {
    return date.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) + " - " + note;
  }
}

在我的 HTML 页面上,我想在 <select /> 中加载数据库中的所有模式,并让用户按日期和注释选择模式,编辑模式并处理它。日期和注释组合起来是唯一的。

<select th:field="*{s1Schema}"
        th:onchange="|setEditorText('s1-editor', '*{s1Schema.json}')|" >
    <option th:each="schema : ${schemas}"
            th:value="${schema}"
            th:text="${schema.toString()}"></option>
</select>

让我再解释一下代码:

  • th:field="*{s1Schema}":应为 SchemaDto 类型。我想将其存储在会话中,以便在处理模式时选择和编辑器不会丢失。
  • th:each="schema : ${schemas}" 属于 SchemaDto 类型,其中 ${schemas} 是 List&lt;SchemaDto&gt;
  • th:value="${schema}":我想将架构作为SchemaDto 获取,但是当传递给th:field 时它会调用schema.toString()。

我实际上需要SchemaDto 的所有三个属性。 date 和 note 用于在选择中显示架构,json 应该加载到 ace 编辑器。

    /**
     * Sets the editor text.
     *
     * @param id The editor's id.
     * @param text The text to set.
     */
    const setEditorText = (id, text) => {
        let editor = ace.edit(id);
        editor.setValue(text, 1);
    }

如果有任何帮助,我将不胜感激! :)

【问题讨论】:

    标签: java html spring thymeleaf dto


    【解决方案1】:

    虽然我仍然不知道这是否可行,但我现在正在使用 Ajax 以避免重新加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2015-03-07
      • 1970-01-01
      • 2019-11-06
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多