![](/img/trans.png)
[英]In Spring Boot Thymeleaf , how to pass list of object to th:value ??? I would like to create tag input
[英]How to store a DTO in thymeleaf th:value? (Spring, Java)
我有這個 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 按日期和注釋顯示模式,編輯模式並處理它。 日期和注釋組合起來是獨一無二的。
<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
類型。 我想將其存儲在 session 中,以便在處理模式時選擇和編輯器不會丟失。th:each="schema: ${schemas}"
屬於SchemaDto
類型,其中${schemas}
是List<SchemaDto>
th:value="${schema}"
:我想將架構作為SchemaDto
獲取,但是當傳遞給th:field
時它會調用schema.toString()
。 我實際上需要SchemaDto
的所有三個屬性。 date
和note
用於在 select 中顯示架構,並且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);
}
我很樂意為您提供任何幫助::)
雖然我仍然不知道這是否可能,但我現在正在使用 Ajax 以避免重新加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.