【问题标题】:Get Raw Html From the controller Spring thymeleaf从控制器 Spring thymeleaf 获取原始 Html
【发布时间】:2018-04-08 07:48:49
【问题描述】:

我有一个控制器,它创建模型属性并传递给视图“partial.html”以生成输出

部分.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home page</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<p>
    <span th:text="'Today is: ' + ${message}"></span>
</p>
</body>
</html>

在控制器方法中

model.addAttribute("message", search);

如何将 Htlm 输出到控制器方法中的字符串? 像这样

String htmlOutput="from partial.html";

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    一旦控件离开查看处理器(JSP/Thymeleaf 等),它就不会返回到控制器。您将能够在 customFilter 中获取原始 html 响应,但不能在 Controller 中。

    【讨论】:

      【解决方案2】:

      如果您使用通常的 Spring MVC 方法,正如 Joanna 所说,您做事的顺序是错误的。 Controller 创建模型并指定视图,然后视图由使用该模型的 Thymeleaf 模板渲染。

      另一方面,如果您尝试自己渲染 Thymeleaf 模板(而不是直接将它们发送到用户的浏览器,可能用于 HTML 电子邮件或将预渲染页面存储在数据库或其他东西中),那么您需要创建自己的 Thymeleaf 模板引擎才能使用。有关详细信息,请参阅文档的“Creating and configuring the Template Engine”部分。您可以创建自己的引擎,然后使用其process 方法获取模板的结果放入变量中以供进一步使用。

      【讨论】:

        【解决方案3】:

        您可以寻找this,直接获取结果HTML,忽略帖子的电子邮件部分。

        然后,您可以创建这个:

        final Context ctx = new Context(locale);
        ctx.setVariable("name", recipientName);
        ctx.setVariable("subscriptionDate", new Date());
        ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
        ctx.setVariable("imageResourceName", imageResourceName); 
        // so that we can reference it from HTML
        final String htmlContent = this.templateEngine.process("html/email-inlineimage.html", ctx);
        

        所以你有渲染百里香模板的htmlContext(带变量)

        【讨论】:

          【解决方案4】:

          假设您有一个 HTML 文件,其中包含两个变量 nametodayDate
          您想要处理它并希望将其存储在字符串/数据库/AWS S3 中。

          <!DOCTYPE html>
          <html xmlns:th="http://www.thymeleaf.org">
              <head>
                  <meta charset="UTF-8">
              </head>
              <body>
                  <p>Hello</p>
                  <p th:text="${name}"></p>
                  <p th:text="${todayDate}"></p>
              </body>
          </html>
          

          您的 HTML 文件位置是 src/main/resources/templates/home.html

          通过使用下面的函数,你可以得到最终处理的 HTML:

          <!DOCTYPE html>
          <html>
              <head>
                  <meta charset="UTF-8">
              </head>
              <body>
                  <p>Hello</p>
                  <p>Manoj</p>
                  <p>30 November 2019</p>
              </body>
          </html>
          
          import org.thymeleaf.context.Context;
          
          @GetMapping("/")
              public void process() {
                  SpringTemplateEngine templateEngine = new SpringTemplateEngine();
                  ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
                  templateResolver.setPrefix("templates/");
                  templateResolver.setCacheable(false);
                  templateResolver.setSuffix(".html");
                  templateResolver.setTemplateMode("HTML");
          
                  // https://github.com/thymeleaf/thymeleaf/issues/606
                  templateResolver.setForceTemplateMode(true);
          
                  templateEngine.setTemplateResolver(templateResolver);
          
                  Context ctx = new Context();
          
                  SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
                  Calendar cal = Calendar.getInstance();
          
                  ctx.setVariable("todayDate", dateFormat.format(cal.getTime()));
                  ctx.setVariable("name", "Manoj");
                  final String result = templateEngine.process("home", ctx);
                  System.out.println("result:" + result);
              }
          

          【讨论】:

            猜你喜欢
            • 2017-10-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-15
            • 2020-12-12
            • 2014-06-10
            • 2017-02-14
            • 1970-01-01
            相关资源
            最近更新 更多