<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天用SiteMesh3.0.0 做网站项目,遇到模板总是无法正确匹配的问题,我使用mapping元素定义匹配规则时,发现sitemesh不能正常匹配到模板。</span>
我的sitemesh3.xml是这么定义的
<mapping>
<span style="white-space:pre"> </span><path>/game/*</path>
<span style="white-space:pre"> </span><decorator>/_decorators/mobile-common.theme.ftl</decorator>
</mapping>
<mapping>
<span style="white-space:pre"> </span><path>/game/list/*</path>
<span style="white-space:pre"> </span><decorator>/_decorators/mobile-games.theme.ftl</decorator>
<span style="white-space:pre"> </span><decorator>/_decorators/mobile-common.theme.ftl</decorator>
</mapping>
阅读org.sitemesh.config.xml.XmlConfigurator的代码才发现,原来是sitemesh的代码出了点问题导致无法正常读取path子元素,
修改40-50行的代码即可解决该问题。
3.0.0原始代码
// Decorator mappings
for (Xml mapping : xml.children("mapping")) {
List<Xml> paths = xml.children("path");
if (!paths.isEmpty()) {
for (Xml path : xml.children("mapping")) {
addDecoratorPaths(builder, mapping, path.text());
}
} else {
addDecoratorPaths(builder, mapping, mapping.attribute("path", "/*"));
}
}
修复的代码:
// Decorator mappings zhongqiang:基于对3.0.0版本,修正对mapping的path子元素的支持。
for (Xml mapping : xml.children("mapping")) {
List<Xml> paths = mapping.children("path");
if (!paths.isEmpty()) {
for (Xml path : paths) {
addDecoratorPaths(builder, mapping, path.text());
}
} else {
addDecoratorPaths(builder, mapping, mapping.attribute("path", "/*"));
}
}
(完)
可以参考github。