我讀了Thymeleaf + Spring 3 Tutorial,並試圖建立一個模塊化的應用程序。我的目標是能夠在類路徑中從Jars加載thymeleaf模板。模塊化模板解析器在Thymeleaf
爲此,我已經配置在未來的XML一個SpringTemplateEngine
:
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<util:set>
<ref bean="servletContextTemplateResolver" />
<ref bean="classpathPluginTemplateResolver" />
</util:set>
</property>
</bean>
<bean id="classpathPluginTemplateResolver"
class="org.mael.codex.arcana.web.thymeleaf.templates.ClasspathPluginTemplateResolver">
<property name="prefix" value="/META-INF/codexarcana/plugin/views" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
而且ClasspathPluginTemplateResolver代碼:
public class ClasspathPluginTemplateResolver extends TemplateResolver {
public ClasspathPluginTemplateResolver() {
super();
super.setResourceResolver(new ClasspathTemplateResolver());
}
@Override
public void setResourceResolver(IResourceResolver resourceResolver) {
throw new ConfigurationException("Cannot set a resource resolver on "
+ this.getClass().getName() + ". If "
+ "you want to set your own resource resolver, use "
+ TemplateResolver.class.getName() + "instead");
}
}
而且ClasspathTemplateResolver:
public class ClasspathTemplateResolver implements IResourceResolver {
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public InputStream getResourceAsStream(
TemplateProcessingParameters templateProcessingParameters,
String resourceName) {
if (resourceName == null || resourceName.isEmpty()) {
throw new IllegalArgumentException("Resource name cannot be null");
}
try {
return new ClassPathResource(resourceName).getInputStream();
} catch (IOException e) {
throw new RuntimeException("Cannot open resource '" + resourceName
+ "'", e);
}
}
}
如何避免由ClasspathPluginTemplateResolver解析的模板名稱衝突?
例子:
讓我們假設我有2個插件這個目錄結構:
codexarcana-poll-plugin.jar
----/META-INF/codexarcana/plugin/views/wizard.html
和
codexarcana-gravatar-plugin.jar
----/META-INF/codexarcana/plugin/views/wizard.html
在這種情況下,我相信ClasspathTemplateResolver會返回一個錯誤的觀點或會有解決這種事情的衝突。
從源頭答案嗎?那很棒!格拉西亞斯! – ElderMael