2016-11-23 14 views
0

Conditional Expression (docs):春Thymeleaf - 目で条件式:フィールド

<tr th:class="${row.even}? 'even' : 'odd'"> 

私は目の中に条件式を使用します:フィールド。例えば

*only variable expressions ${...} or selection expressions {...} are allowed in Spring field bindings


:しかし、私はそれをしようとするたびに、私は次のエラーを取得する場所をその場:

// This works fine. 
<input type="text" th:value="${object.covered} ? 'yes' : 'no'" /> 

// This on the other hand, generates the error mentioned earlier. 
// Which does make sense, cause it would otherwise generate invalid attributes. 
<input type="text" th:field="${object.covered} ? 'yes' : 'no'" /> 

// Combining the two does not work. 
<input type="text" th:field="${object.covered}" th:value="${object.covered} ? 'yes' : 'no'" /> 

は基本的に、私が欲しいもの、目を作成することです値は条件式で決まります。

具体的には、私の実装では、モデルから数値(Java long)を入力フィールドに取り込みたいと考えています。そしてその数がゼロ以下であれば、代わりにプレースホルダーを使用したいと思います。

// Ultimateley, what I want to achieve is something like this. 
<input type="text" th:field="${person.age}" 
     th:value="${person.age} le 0 ? null : ${person.age}" 
     placeholder="age" /> 

th:fieldをどのように使用し、Spring Thymeleafで条件式を使用してその値を決定するか?

(Thymeleaf 2.1.5、およびスプリングブート1.4.2)

答えて

0

Thymeleaf th:field 3 HTMLがid属性生成、namevalue。あなたのケースでは、代わりにth:fieldを使用しての

<input th:if="${person.age > 0}" type="text" th:field="${person.age}" /> 
<input th:if="${person.age <= 0}" type="text" id="person.age" name="person.age" placeholder="age"/> 
以下のように年齢が、ゼロより小さいとき idnameplaceholderを使用