尝试在模板中合并多个值时遇到问题。根据Thymeleaf的说法,我应该可以将它们+一起组合在一起...
4.6合并文本
文本,无论它们是文字还是评估变量或消息表达式的结果,都可以使用+运算符轻松地进行连接:
th:text="'The name of the user is ' + ${user.name}"
这是我发现有效的示例:
<p th:text="${bean.field} + '!'">Static content</p>
但是,这不是:
<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>
从逻辑上讲,这应该可以,但是不能,我在做什么错?
Maven:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>2.0.16</version>
<scope>compile</scope>
</dependency>
这是我设置TemplateEngine和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;
}
}