Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

java - Thymeleaf: Concatenation - Could not parse as expression

I'm having an issue when trying to concat multiple values in my template. According to Thymeleaf here I should simply be able to + them together...

4.6 CONCATENATING TEXTS

Texts, no matter whether they are literals or the result of evaluating variable or message expressions, can be easily concatenated using the + operator:

th:text="'The name of the user is ' + ${user.name}"

Here is an example of what I found works:

<p th:text="${bean.field} + '!'">Static content</p>

This however doesn't:

<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>

Logically, this should work but its not, what am I doing wrong?


Maven:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring3</artifactId>
    <version>2.0.16</version>
    <scope>compile</scope>
</dependency>

Here is how I've set my TemplateEngine and TemplateResolver up:

<!-- 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;
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

But from what I see you have quite a simple error in syntax

<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>

the correct syntax would look like

<p th:text="${bean.field + '!' + bean.field}">Static content</p>

As a matter of fact, the syntax th:text="'static part' + ${bean.field}" is equal to th:text="${'static part' + bean.field}".

Try it out. Even though this is probably kind of useless now after 6 months.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...