gpt4 book ai didi

spring-mvc - 有没有办法让 Spring Thymeleaf 处理字符串模板?

转载 作者:行者123 更新时间:2023-12-04 01:36:25 24 4
gpt4 key购买 nike

我想写一些类似的东西:

    @Autowired
private SpringTemplateEngine engine;
....
// Thymeleaf Context
WebContext thymeleafContext = new WebContext(request, response, request.getServletContext(), locale);

// cached html of a thymeleaf template file
String cachedHtml=....

// process the cached html
String html=engine.process(cachedHtml, thymeleafContext);

默认情况下, [process] 方法不能这样做。我可以从文档中了解到我需要一个特殊的模板解析器:

为了执行模板,将使用 process(String, IContext) 方法:
final String result = templateEngine.process("mytemplate", ctx);
“mytemplate”字符串参数是模板名称,它将与 相关联。物理/逻辑位置 模板本身以在模板解析器/s 中配置的方式。

有谁知道如何解决我的问题?

目标是在字符串中缓存 Thymeleaf 模板(文件),然后处理这些字符串而不是文件。

最佳答案

我们最终使用的解决方案包括一个新的 IResourceResolver带定制 Context而不是自定义 TemplateResolver .我们选择这个是因为我们在大多数情况下仍然想使用类路径扫描,但偶尔会有动态内容。

下面显示了我们是如何做到的:

public class StringAndClassLoaderResourceResolver implements IResourceResolver {


public StringAndClassLoaderResourceResolver() {
super();
}


public String getName() {
return getClass().getName().toUpperCase();
}


public InputStream getResourceAsStream(final TemplateProcessingParameters params, final String resourceName) {
Validate.notNull(resourceName, "Resource name cannot be null");
if( StringContext.class.isAssignableFrom( params.getContext().getClass() ) ){
String content = ((StringContext)params.getContext()).getContent();
return IOUtils.toInputStream(content);
}
return ClassLoaderUtils.getClassLoader(ClassLoaderResourceResolver.class).getResourceAsStream(resourceName);
}

public static class StringContext extends Context{

private final String content;

public StringContext(String content) {
this.content = content;
}

public StringContext(String content, Locale locale) {
super(locale);
this.content = content;
}

public StringContext(String content, Locale locale, Map<String, ?> variables) {
super(locale, variables);
this.content = content;
}

public String getContent() {
return content;
}
}

测试用例
public class StringAndClassLoaderResourceResolverTest {

private static SpringTemplateEngine templateEngine;

@BeforeClass
public static void setup(){
TemplateResolver resolver = new TemplateResolver();
resolver.setResourceResolver(new StringAndClassLoaderResourceResolver());
resolver.setPrefix("mail/"); // src/test/resources/mail
resolver.setSuffix(".html");
resolver.setTemplateMode("LEGACYHTML5");
resolver.setCharacterEncoding(CharEncoding.UTF_8);
resolver.setOrder(1);

templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(resolver);
}

@Test
public void testStringResolution() {
String expected = "<div>dave</div>";
String input = "<div th:text=\"${userName}\">Some Username Here!</div>";
IContext context = new StringAndClassLoaderResourceResolver.StringContext(input);
context.getVariables().put("userName", "dave");
String actual = templateEngine.process("redundant", context);
assertEquals(expected, actual);
}

@Test
public void testClasspathResolution(){
IContext context = new Context();
context.getVariables().put("message", "Hello Thymeleaf!");
String actual = templateEngine.process("dummy", context);
String expected = "<h1>Hello Thymeleaf!</h1>";
assertEquals(expected, actual);
}
}

src/main/resources/mail/dummy.html 中的虚拟模板文件
<h1 th:text="${message}">A message will go here!</h1>

注:我们使用 Apache CommonsIO 的 IOUtils 将字符串转换为 InputStream

关于spring-mvc - 有没有办法让 Spring Thymeleaf 处理字符串模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com