1 回答
TA贡献1783条经验 获得超4个赞
所以我让它与一个特殊的 TemplateResolver 一起工作。它的逻辑类似于 Spring 的WebJarsResourceResolver,并且使用WebJarAssetLocator.
public class WebJarTemplateResolver extends ClassLoaderTemplateResolver {
private final static String WEBJARS_PREFIX = "/webjars/";
private final static int WEBJARS_PREFIX_LENGTH = WEBJARS_PREFIX.length();
private final WebJarAssetLocator webJarAssetLocator = new WebJarAssetLocator();
@Override
protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, String resourceName, String characterEncoding, Map<String, Object> templateResolutionAttributes) {
resourceName = findWebJarResourcePath(template);
if (resourceName == null) {
return null;
}
return super.computeTemplateResource(configuration, ownerTemplate, template, resourceName, characterEncoding, templateResolutionAttributes);
}
@Nullable
protected String findWebJarResourcePath(String templateName) {
if (!templateName.startsWith(WEBJARS_PREFIX)) {
return null;
}
int startOffset = WEBJARS_PREFIX_LENGTH;
int endOffset = templateName.indexOf('/', startOffset);
if (endOffset == -1) {
return null;
}
String webjar = templateName.substring(startOffset, endOffset);
String partialPath = templateName.substring(endOffset + 1);
return this.webJarAssetLocator.getFullPathExact(webjar, partialPath);
}
}
这通过以下配置集成到应用程序中:
@Configuration
public class ThymeleafConfiguration {
private SpringTemplateEngine templateEngine;
public ThymeleafConfiguration(SpringTemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}
@PostConstruct
public void enableWebjarTemplates() {
templateEngine.addTemplateResolver(new WebJarTemplateResolver());
}
}
添加回答
举报