【发布时间】: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<SchemaDto> -
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