2016-11-28 3 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> 

${userCredentials}フォームオブジェクトが初期化されていないためです。私は何とかこのような私のビューの内側にこのオブジェクトを構築することはできますか?

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

答えて

0

私はあなたがしかし、新しいオブジェクトを返しますUserCredentials上の静的メソッドを作成し、それを使用することができ、それはnewを使用してオブジェクトを作成することはできないと思います。このような何か:

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> 
関連する問題