2017-10-13 76 views
0

創建HTML表單我有3類:與Spring + Hibernate的

一流,是一個實體,存儲字符串變量:

@Entity 
@Table(name = "mods_langs_texts", catalog = "artfunpw") 
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
public class ModsLangsTexts implements java.io.Serializable { 


    @Column(name = "Title", nullable = false, length = 300) 
    public String getTitle() { 
     return this.title; 
    } 

第二實體是關係:

@Entity 
@Table(name = "mods_langs_texts_relations", catalog = "artfunpw") 
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
public class ModsLangsTextsRelations implements java.io.Serializable { 

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JoinColumn(name = "textId", nullable = false) 
    public ModsLangsTexts getModsLangsTexts() { 
     return this.modLangsTexts; 
    } 

而第三個實體是主要類:

@Entity 
@Table(name = "mods", catalog = "artfunpw") 
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
public class Mod implements java.io.Serializable { 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "mod") 
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
    public Set<ModsLangsTextsRelations> getModsLangsTextsRelationses() { 
     return this.modsLangsTextsRelationses; 
    } 

我試圖創建一個與代碼的HTML表單:

m.addAttribute("formClass", mod); 

和HTML代碼:

<p th:text="${formClass.modsLangsTextsRelationses.toArray()[0].getModsLangsTexts().title}" /> 

       <form action="" th:object='${formClass}' method="POST"> 

        <input type="hidden" th:field='*{id}' /> 

        Title: <input type="text" th:field='${formClass.modsLangsTextsRelationses.toArray()[0].getModsLangsTexts().title}' /> 
        <br /> 

但它失敗,錯誤:

2017-10-13 14:33:56.661 ERROR 4744 --- [nio-8080-exec-9] org.thymeleaf.TemplateEngine    : [THYMELEAF][http-nio-8080-exec-9] Exception processing template "mods/editPages/editModPage": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (mods/editPages/editModPage:57) 

其中線57是行與代碼:

Title: <input type="text" th:field='${formClass.modsLangsTextsRelationses.toArray()[0].getModsLangsTexts().title}' /> 

與此同時,代碼

<p th:text="${formClass.modsLangsTextsRelationses.toArray()[0].getModsLangsTexts().title}" /> 

工作正常。

如何使用Spring + Hibernate + Thymeleaf從HTML表單代碼訪問內部對象?

回答

0

看一看的thymeleaf tutorial

th:field是一個字段引用您的form標籤得益於th:object屬性

從教程上宣佈的命令對象:

<form action="#" th:action="@{/seedstartermng}" th:object="${seedStarter}" method="post"> 
    <input type="text" th:field="*{datePlanted}" /> 
</form> 

適應你的情況它會是這樣的:

<form action="#" th:action="@{???}" th:object="${formClass.modsLangsTextsRelationses.toArray()[0].getModsLangsTexts()}" method="post"> 
    <input type="text" th:field="*{title}" /> 
</form> 

我有疑問它會這樣工作。看看section 7.6 : dynamic fields