2016-11-23 2 views
0

Conditional Expression (docs):Spring 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, используйте id, name и placeholder, когда возраст меньше нуля, как показано ниже

<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"/> 
Смежные вопросы