我想有一个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