我想有一個PropertyBean的多個實例的編輯列表(包括財產,價值,數據類型)來描述文檔。所以在Thymeleaf Frontend(Spring後端)中,文檔應該以表格的形式顯示,幷包含這些PropertyBean-Instance的列表。每個實例的「值」字符串應該位於輸入字段中,可以修改。 (後來還有其他人,但我認爲這將是從小事做起是個好主意Thymeleaf:對象的形式
這是我所:
package beans;
public class PropertyBean {
private String property;
private String value;
private String datatype;
public PropertyBean() {
}
public PropertyBean(String property, String value, String datatype) {
super();
this.property = property;
this.value = value;
this.datatype = datatype;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
@Override
public String toString() {
return "property = "+this.property+",value = "+this.value+", datatype = "+this.datatype;
}
}
的PropertyWrapper類(作爲Thymeleaf一個包裝):
package beans;
import java.util.ArrayList;
public class PropertyWrapper {
private ArrayList<PropertyBean> properties;
public PropertyWrapper() {};
public ArrayList<PropertyBean> getProperties() {
if (this.properties == null) {
this.properties = new ArrayList<PropertyBean>();
}
return properties;
}
public void setProperties(ArrayList<PropertyBean> properties) {
this.properties = properties;
}
public void newProperty(String property, String value, String datatype) {
PropertyBean newPr = new PropertyBean(property,value,datatype);
this.getProperties().add(newPr);
}
public void printAll() {
for (PropertyBean p : getProperties()) {
System.out.println(p.toString());
}
}
}
這
春節文檔控制的重要組成部分:
/******************************************************
* POST of one specific document
******************************************************/
@PostMapping("documents/{doc_id}")
public String editDocument(@PathVariable String doc_id,
@ModelAttribute("properties_wrapper") PropertyWrapper propertiesWrapper, Model model) {
//Just print the values
propertiesWrapper.printAll();
saveInDatabase(propertiesWrapper);
Message info_msg = new Message("Changes successuflly saved!", "alert-success");
return document(doc_id, model, info_msg);
}
/******************************************************
* GET of one specific document
******************************************************/
@RequestMapping(path = "documents/{doc_id}", method = RequestMethod.GET)
public String document(@PathVariable String doc_id, Model model, Message msg) {
PropertyWrapper prwrapper = loadFromDatabase(doc_id);
if (msg != null) {
model.addAttribute("msg", msg);
}
model.addAttribute("doc_id", doc_id);
model.addAttribute("properties_wrapper", prwrapper);
return "documentTmpl";
}
這裏是thymeleaf模板的形式部分:
<form action="#" method="post" th:action="@{/documents/{id}(id=${doc_id})}" th:object="${properties_wrapper}">
<h1 th:text="${doc_id}">Document</h1>
<table class="table table-striped">
<tr>
<th>Property</th>
<th>Value</th>
<th>Datatype</th>
</tr>
<tr th:each="propertyItem,status : ${properties_wrapper.properties}">
<td th:text="${propertyItem.property}">Property</td>
<td>
<!-- here the problem occurs: -->
<input th:field="*${properties_wrapper.properties[__${status.index}__].value}"></input>
</td>
<td th:text="${propertyItem.datatype}"> </td>
</tr>
</table>
<button type="submit" class="btn btn-default">Submit</button>
</form>
正如你可以在註釋中看到的,我不知道如何,甚至是否有可能訪問properties_wrapper。
本版本導致的異常,當我調用get-方法:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "*${properties_wrapper.properties[__${status.index}__].value}" (documentTmpl:26)
我也試過縮寫形式,即<input th:field="*${properties_wrapper.properties[__${status.index}__].value}"></input>
相反,我還試圖<input th:field="*${propertyItem.value}"></input>
,但具有相同的效果。
如果沒有這個輸入標籤,都在正常顯示一開始叫
非常感謝! '* $ {'提出了問題,但現在都解決了 – MS1