2016-11-21 53 views
0

我想有一個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>,但具有相同的效果。

如果沒有這個輸入標籤,都在正常顯示一開始叫

回答

1

有一個在春季標準方言爲Thymeleaf沒有*${}表達。但是,您可以使用以下五種表情(參考文獻:http://www.thymeleaf.org/doc/articles/standarddialect5minutes.html):

  • ${...}:變量表達式。這些是Spring EL表達式。

  • *{...}:選擇表達式。除上述內容外,它只會在先前選定的對象上執行。 (由th:object設置的對象)

  • #{...}:Message(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="*{}"?採取

+0

非常感謝! '* $ {'提出了問題,但現在都解決了 – MS1