所以我和一个特别的圣殿骑士合作。它的逻辑和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());
}
}