当前位置: 首页 > 知识库问答 >
问题:

Spring靴、百里香和琴弦

隆选
2023-03-14

你好,我正在使用Spring boot 2.4. x,我想使用thymeleaf作为我的模板引擎。

我已经添加了百里香,并进行了以下测试

@Test
void name() {
    StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();

    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(stringTemplateResolver);

    Context ctx = new Context();
    ctx.setVariable("franco", "prova");

    String processedTemplate = templateEngine.process("<html>${franco}</html>", ctx);

    Assertions.assertThat(processedTemplate).isEqualTo("<html>prova</html>");
}

但模板引擎不替换变量,并且已处理的模板变量的值与输入字符串相同。

我有什么错?谢谢你

共有1个答案

谷梁宝
2023-03-14

百里叶不会直接在 HTML 中处理变量。您必须使用文本内联语法:

<html>[[${franco}]]</html>

或者以标准方式(即使用以< code>th:为前缀的属性)这样做:

<html th:text="${franco}" />

<html><span th:text="${franco}" /></html>

(另外,作为旁注 字符串模板解决方案是默认的解析器,因此您不必对其进行配置。您的完整代码可能如下所示。

@Test
void name() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();

    Context ctx = new Context();
    ctx.setVariable("franco", "prova");

    String processedTemplate = templateEngine.process("<html>[[${franco}]]</html>", ctx);
    Assertions.assertThat(processedTemplate).isEqualTo("<html>prova</html>");
}
 类似资料:
  • 我使用Spring的引导2.6.3和我试图使用thymeleaf最近得到一个与html的确认消息后,我通过邮件确认我的帐户(我只是使用确认邮件来验证新帐户),所以我看了一些关于thymeleaf的视频,我标记所有的视频都有一个名为模板的文件夹和静态在src/main/ressource和我没有他们,所以我创建了一个名为模板的文件夹,在文件夹中我创建了一个html文件只是为了尝试它,如果它(html

  • 我是Springboot的初学者,所以决定在购物车上工作。似乎找不到org.springframework.expression.spel.SpelEvalue ationException: EL1007E的根:在null上找不到属性或字段'name'。嵌套异常是org.thymeleaf.exceptions.TemplateProcessingException: Exception评估Sp

  • 问题内容: 我有一个字符串列表- 这些是我感兴趣的属性名称:我想联接这些字符串的值,但不使用属性名称,而是使用它们的属性值。我看到起点是-但是如何说出将列表中的元素与属性文件中的值进行匹配? 该列表将是:并且在属性文件中,我具有: 我想获取字符串: 问题答案: 您可以利用和 从@Controller返回模型: 您可以像这样加入翻译后的邮件列表: 哪个应该产生您期望的结果,例如: 名称和地址是消息文

  • 我不明白,如果html模板与我在ModelAndView中获得的属性链接,如何将RESTFull服务的@Controller更改为@RestController 在胸腺叶模板中 但我想进入索引页面,在后台获取用户名 我可以使用ajax来更新标记“p”,但这样使用thymeleaf并没有什么好处,我可以使用jsp。那么,在Rest时使用thymeleaf的最佳方式是什么?它是否合理?

  • 大家好,我对thymeleaf和我的静态html页面有一个问题。更具体地说,我有一个spring mvc web应用程序,我也在使用spring security,在我的登录页面中,我想使用thymeleaf,这样spring security可以与另一端的客户端层通信吗?我不想在我的所有html页面中包括thymeleaf,因为我将使用AngularJs 我尝试将登录名放在templates文件

  • 如何使用Spring启动百里香为编辑表单设置输入文件?我尝试使用 MultpartFile 类,但我只能获取它的值......这是数据传输类: 这是HTML