Java ThymileAF:编辑表单的对象列表
我希望有多个PropertyBean实例(由property、value、datatype组成)来描述文档。因此,在Thymeleaf前端(使用Spring后端)中,一个文档应该显示为一个表,其中包含一个此类PropertyBean实例的列表。每个实例的“值”字符串应位于输入字段中,允许修改。(后来,其他人也一样,但我认为从小处做起是个好主意 这就是我所拥有的:Java ThymileAF:编辑表单的对象列表,java,spring,forms,thymeleaf,Java,Spring,Forms,Thymeleaf,我希望有多个PropertyBean实例(由property、value、datatype组成)来描述文档。因此,在Thymeleaf前端(使用Spring后端)中,一个文档应该显示为一个表,其中包含一个此类PropertyBean实例的列表。每个实例的“值”字符串应位于输入字段中,允许修改。(后来,其他人也一样,但我认为从小处做起是个好主意 这就是我所拥有的: package beans; public class PropertyBean { private String prop
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;
}
}
PropertyRapper类(用作Thymeleaf的包装):
这是ThymileAF模板的组成部分:
<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>
我还尝试了缩写形式,即我也尝试了
而不是
,但效果相同
如果没有此输入标记,则在get调用中正确显示all在Thymeleaf的Spring标准方言中没有
*${}
表达式。但是,您可以使用以下五个表达式1(参考:):
:变量表达式。这些是Spring EL表达式${…}
:选择表达式。与上面相同,但它将仅在以前选择的对象上执行。(由*{…}
设置的对象)th:object
:消息(i18n)表达式。用于从外部源检索特定于语言环境的消息{…}
:链接(URL)表达式。已使用 创建URL@{…}
:片段表达式。表示 标记并在模板周围移动它们~{…}
实际上应该是:
1-摘自非常感谢!
*${
解决了问题,但现在都解决了
/******************************************************
* 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";
}
<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>
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "*${properties_wrapper.properties[__${status.index}__].value}" (documentTmpl:26)