【问题标题】:How to use custom properties with Sitemesh 3如何在 Sitemesh 3 中使用自定义属性
【发布时间】:2013-03-04 16:41:43
【问题描述】:

给定一个由 Sitemesh 3 渲染的简单模板 JSP:

<%@include file="../jsp_inc/taglibs.jsp" %>
<!DOCTYPE html>
<head>
    <link rel="stylesheet" type="text/css" href='<c:url value="/css/global.css" />' >
</head>
<body>
<h1>[HEADING]</h1>
<div>
    <sitemesh:write property='body'/>
</div>
</body>

该模板按预期工作,将 JSP 元素的内容插入到模板中。

正如您可能从上面所期望的那样,我希望能够将 JSP 中设置的值(例如 h1 元素)插入到我的模板中的适当元素中。

我试过了:

<sitemesh:getProperty property="page.heading"></sitemesh:getProperty>

在模板/装饰器中:

<content tag="heading"></content>

在 JSP 中,根据另一个关于 SO 的问题,但我认为这可能指的是 Sitemesh 2。我正在使用 Sitemesh 3。

有什么建议吗?

【问题讨论】:

标签: jsp templates sitemesh


【解决方案1】:

不确定您是否仍在使用 Sitemesh 3,但我正在检查它并在浏览源代码后发现您已经配置了构建器。

我正在使用Java based configuration 并创建了我自己的子类,它添加了一个支持 Sitemesh 2 样式内容块的标签处理规则包:

public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addTagRuleBundle(new Sm2TagRuleBundle());
    }
}

通过sitemesh3.xml中的XML配置:

<sitemesh>
    <content-processor>
        <tag-rule-bundle class="org.sitemesh.content.tagrules.html.Sm2TagRuleBundle" />
    </content-processor>
    <!-- Your mappings go here -->
    <mapping ... />
</sitemesh>

这样做可以让您使用如下内容标签:

<content tag="heading"></content>

在装饰器/模板页面中,使用sitemesh:write 标记代替不再存在的sitemesh:getProperty

<sitemesh:write property="page.heading"/>

【讨论】:

  • 感谢@Phuong LeCong。这对我有用。我想知道为什么这个答案没有被投票/接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 2022-07-12
  • 2013-10-07
相关资源
最近更新 更多