Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从控制器Spring-eleaf获取原始Html_Spring_Spring Boot_Thymeleaf - Fatal编程技术网

从控制器Spring-eleaf获取原始Html

从控制器Spring-eleaf获取原始Html,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我有一个创建模型属性并传递到视图“partial.html”以生成输出的控制器 partial.html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Home page</title> <meta charset="UTF-8"/> <meta name="viewport" content="

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

partial.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>
如何将Htlm输出到控制器方法内的字符串? 像这样

String htmlOutput="from partial.html";

一旦控件转到视图处理器(JSP/Thymeleaf等),它将不会返回到控制器。您将能够在customFilter中获得原始html响应,但不能在控制器中获得。

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

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

您可以直接查找结果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);

因此,您有一个呈现的thymeleaf模板的htmlContext(带vars)

假设您有一个带有两个变量名的HTML文件,todayDate,您希望处理它,并希望将其存储在字符串、数据库或aws s3中

<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> your HTML file location is src/main/resources/templates/home.html By using the below function you can get the final processed HTML as: <!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("HTML5"); 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); } 你好

您的HTML文件位置是src/main/resources/templates/home.HTML 通过使用以下函数,您可以获得最终处理的HTML,如下所示: 你好

马诺伊

2019年11月30日

导入org.thymeleaf.context.context; @GetMapping(“/”) 公共程序(){ SpringTemplateEngine templateEngine=新的SpringTemplateEngine(); ClassLoaderTemplateResolver templateResolver=新ClassLoaderTemplateResolver(); setPrefix(“templates/”); templateResolver.setCacheable(false); templateResolver.setSuffix(“.html”); setTemplateMode(“HTML5”); templateEngine.setTemplateResolver(templateResolver); Context ctx=新上下文(); SimpleDataFormat dateFormat=新的SimpleDataFormat(“dd MMMM yyyy”); Calendar cal=Calendar.getInstance(); setVariable(“todayDate”,dateFormat.format(cal.getTime()); ctx.setVariable(“名称”、“Manoj”); 最终字符串结果=templateEngine.process(“home”,ctx); System.out.println(“结果:+result”); } <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> your HTML file location is src/main/resources/templates/home.html By using the below function you can get the final processed HTML as: <!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("HTML5"); 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); }