Thymeleaf: Concatenation - nie można parsować jako wyrażenie
Mam problem podczas próby połączenia wielu wartości w moim szablonie. Według Thymeleaf tutaj powinienem po prostu być w stanie + je razem...
4.6 KONKATENACJA TEKSTÓW
Teksty, bez względu na to, czy są literałami, czy wynikiem oceny zmiennej czy wiadomości wyrażenia mogą być łatwo konkatenowane za pomocą operatora+:
th:text="'The name of the user is ' + ${user.name}"
Oto przykład tego, co znalazłem:
<p th:text="${bean.field} + '!'">Static content</p>
To jednak nie:
<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>
Logicznie rzecz biorąc, to powinno działać, ale nie jest, co robię źle?
Maven:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>2.0.16</version>
<scope>compile</scope>
</dependency>
Oto jak skonfigurowałem mój TemplateEngine i TemplateResolver:
<!-- Spring config -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="1"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="fileTemplateResolver"/>
<property name="templateResolvers">
<list>
<ref bean="templateResolver"/>
</list>
</property>
ThymeleafTemplatingService:
@Autowired private TemplateEngine templateEngine;
.....
String responseText = this.templateEngine.process(templateBean.getTemplateName(), templateBean.getContext());
AbstractTemplate.java:
public abstract class AbstractTemplate {
private final String templateName;
public AbstractTemplate(String templateName){
this.templateName=templateName;
}
public String getTemplateName() {
return templateName;
}
protected abstract HashMap<String, ?> getVariables();
public Context getContext(){
Context context = new Context();
for(Entry<String, ?> entry : getVariables().entrySet()){
context.setVariable(entry.getKey(), entry.getValue());
}
return context;
}
}
2 answers
Ale z tego co widzę masz dość prosty błąd w składni
<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>
Poprawna składnia wyglądałaby jak
<p th:text="${bean.field + '!' + bean.field}">Static content</p>
W rzeczywistości składnia th:text="'static part' + ${bean.field}"
jest równa th:text="${'static part' + bean.field}"
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-18 12:15:30
Możesz łączyć wiele rodzajów wyrażeń, sorroundując swoje proste / złożone wyrażenie między znakami ||
:
<p th:text="|${bean.field} ! ${bean.field}|">Static content</p>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-15 00:19:18