gpt4 book ai didi

java - Thymeleaf:编辑表单的对象列表

转载 作者:行者123 更新时间:2023-11-30 07:02:19 25 4
gpt4 key购买 nike

我想要有多个 PropertyBean 实例(由属性、值、数据类型组成)来描述文档。因此,在 Thymeleaf 前端(带有 Spring 后端)中,文档应显示为表格,并包含此类 PropertyBean 实例的列表。每个实例的“值”字符串应位于输入字段中,并且可以修改。 (后来还有其他人,但我认为从小事做起是个好主意。

这就是我所拥有的:

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());
}
}
}

这是 Spring 文档 Controller 的重要部分:

/******************************************************
* 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,甚至不知道是否可以访问properties_wrapper。

当我调用 get-Method 时,当前版本会导致异常:

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>但效果相同。

没有这个输入标签,所有内容都会在 get-call 中正确显示

最佳答案

没有*${} Thymeleaf 的 Spring 标准方言中的表达式。但是,您可以使用以下五个表达式1(引用:http://www.thymeleaf.org/doc/articles/standarddialect5minutes.html):

  • ${...} :变量表达式。这些是 Spring EL 表达式。

  • *{...} :选择表达式。与上面相同,不同之处在于它将仅在先前选择的对象上执行。 (th:object设置的对象)

  • #{...} :消息(i18n)表达式。用于从外部源检索特定于区域设置的消息。

  • @{...} :链接(URL)表达式。用过的构建 URL。
  • ~{...} : 片段表达式。代表片段标记并在模板中移动它们。

回答

这一行:

<input th:field="*${properties_wrapper.properties[__${status.index}__].value}"></input>

实际上应该是:

<input th:field="*{properties[__${status.index}__].value}"/> .

1 - 取自 Thymeleaf - What is the difference between th:field="${}" and th:field="*{}"?

关于java - Thymeleaf:编辑表单的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40717777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com