Thymeleaf:编辑表单

时间:2016-11-21 10:19:53

标签: java spring forms thymeleaf

我希望有一个PropertyBean的多个实例(由property,value,datatype组成)来描述文档。因此,在Thymeleaf Frontend(使用Spring后端)中,文档应显示为一个表,其中包含此类PropertyBean-Instances的列表。 "值"每个实例的字符串应位于输入字段中,可以进行修改。 (后来也是其他人,但我认为从小做起是个好主意。

这就是我所拥有的:

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文档控制器的重要部分:

/******************************************************
 * 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>

正如您在评论中看到的,我不知道如何或甚至可以访问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

中正确显示

1 个答案:

答案 0 :(得分:1)

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

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

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

  • #{...}:消息(i18n)表达式。用于从外部源检索特定于语言环境的消息。

  • @{...}:链接(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="*{}"?