【问题标题】:Thymeleaf: Concatenation - Could not parse as expressionThymeleaf:连接 - 无法解析为表达式
【发布时间】:2013-04-13 17:17:35
【问题描述】:

尝试在模板中连接多个值时遇到问题。 根据 Thymeleaf here 的说法,我应该能够简单地将它们 + 在一起......

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>

从逻辑上讲,这应该可行,但不是,我做错了什么?


马文:

<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());

抽象模板.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;
  }
}

【问题讨论】:

  • 我也发生了同样的错误!!!!!!!!!!!!但我使用百里香和 scala
  • 我设法使它工作的唯一方法是使用预处理。 &lt;p th:text="${'__${bean.property1}__' + '::' + '__${bean.property2}__'}"&gt;default text&lt;/p&gt;
  • 这个例子对我有用。你用的是什么版本的百里香?您是否使用任何其他方言?

标签: java html thymeleaf


【解决方案1】:

但据我所知,您的语法错误很简单

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

正确的语法应该是这样的

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

事实上,th:text="'static part' + ${bean.field}" 的语法等于th:text="${'static part' + bean.field}"

试试看。尽管这在 6 个月后现在可能有点没用了。

【讨论】:

  • 谢谢,对我有帮助。
  • 6个月后没用?6年后还是有用的
  • 8 年后仍然有用
  • 因此,如果涉及多个属性,“外部连接”语法将停止工作......有趣。我认为本地化的属性也会发生类似的事情?
【解决方案2】:

您可以通过在|| 字符之间环绕您的简单/复杂表达式来连接多种表达式:

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

【讨论】:

  • 我使用的是 Thymeleaf 版本:2.1.1.RELEASE(应该是最后一个版本)
  • 2.1.5版本上工作得很好
  • 如果文本包含|怎么办?例如。 "|${fullName} Stories \| Twiza|" 我得到无法解析为表达式。
  • 这里怎么做它不起作用你能把它转换成你的语法吗...th:class="'hotel listing col organic urgencyMsg available organic h-' + ${record.hotelInfo.hotelId} + '-organic'"&gt;我正在使用3.0.2
  • 在文本中使用竖线(|):&lt;p&gt;[[${fullName}]] Stories | Twiza &lt;/p&gt;
【解决方案3】:

请注意,使用 | char,您可以通过 IDE 收到警告, 例如,我收到最新版本 IntelliJ 的警告, 所以最好的解决方案是使用这种语法:

th:text="${'static_content - ' + you_variable}"

【讨论】:

    【解决方案4】:

    我们可以像这样连接:


    <h5 th:text ="${currentItem.first_name}+ ' ' + ${currentItem.last_name}"></h5>
    

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2017-03-28
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2017-09-22
      • 2021-09-16
      • 2021-01-02
      相关资源
      最近更新 更多