2016-11-28 123 views
0

林傳遞模型屬性從控制器到thymeleaf形式,所以我可以結合對象是這樣的:Thymeleaf構造對象

<div class="container" style="max-width: 600px" th:fragment="signupForm"> 
    <form name="f" th:action="@{/signup}" th:object="${userCredentials}" method="post" class="form-horizontal"> 
        <input th:placeholder="#{messages.form.email}" type="text" th:field="*{email}" 
          name="email" id="email" class="form-control"/> 
        <input th:placeholder="#{messages.form.name}" type="text" th:field="*{name}" 
          name="name" id="name" class="form-control"/> 
        <input type="password" th:placeholder="#{messages.form.password}" th:field="*{password}" 
          id="password" name="password"/> 
        <button type="submit" th:text="#{messages.form.signup}"></button> 
    </form> 
</div> 

但是我想重新使用此形式從其它視圖片段,但我不能t因爲${userCredentials}表單對象沒有被初始化。我可以以這種方式構建這個對象嗎?

<div th:if="${userCredentials == null}" th:with="userCredentials=new UserCredentials()"></div> 

回答

0

我不認爲這是可能創建使用new一個對象,你可以創建但是返回一個新的對象上UserCredentials一個靜態方法,並使用它。事情是這樣的:

public class UserCredentials { 
    public static UserCredentials create() { 
    return new UserCredentials(); 
    } 
} 

,並在thymeleaf

<div th:if="${userCredentials == null}" th:with="userCredentials=${T(your.package.here.UserCredentials).create()}"></div>