SiteMesh2-sitemesh.xml的PageDecoratorMapper映射器的用法

继上一章http://www.cnblogs.com/EasonJim/p/7083165.html中使用的例子中,是通过decorators.xml文件通过URL匹配进行转换的。

而下面这种方法是通过sitemesh.xml的PageDecoratorMapper映射器来转换的,而无需匹配URL,只需在meta头指定decorator中的模板即可。

下面是操作步骤:

1、在sitemesh.xml中添加PageDecoratorMapper映射器,并指定meta的名称规则

        <mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
            <param name="property.1" value="meta.decorator" />
            <param name="property.2" value="decorator" />
            <!-- 注意这一行指定<meta/>标签的 name 属性作为PageDecorator的识别符号 -->
            <param name="property.3" value="meta.theme" />
        </mapper>

用法:

①<meta name="decorator" content="basic-theme" />

②<meta name="them" content="basic-theme" />

③<param name="property.2" value="decorator" />这个用法在下方将有说明。

注意:content指定的就是在decorators.xml文件中docorator的模板名称。

2、在decorators.xml的模板配置还是使用原来的

<decorators defaultdir="/decorators">
    <decorator name="basic-theme" page="basic-theme.jsp">
        <pattern>/data/*</pattern>
    </decorator>
</decorators>

3、在根目录新建index.jsp页面,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="decorator" content="basic-theme" />
<title>应该是内容页面</title>
</head>
<body>
    <h1>Weekdays</h1>
    <p>5:00pm - 10:00pm</p>
    <p>Weekends</p>
    <p>5:00pm - 10:00pm</p>
</body>
</html>

4、测试效果如下:

5、<param name="property.2" value="decorator" />的用法

①新建test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html decorator="basic-theme">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
    <h1>Test</h1>
</body>
</html>

②直接访问test.jsp页面,得到的效果就是使用了模板页面的,效果如下:

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/sitemesh/test2

 

参考:

http://wiki.sitemesh.org/wiki/display/sitemesh/Decorating+Beyond+URL+Patterns

https://my.oschina.net/yangning/blog/110487

https://web.archive.org/web/20071008180624/http://www.opensymphony.com/sitemesh/api/com/opensymphony/module/sitemesh/mapper/PageDecoratorMapper.html

转载于:https://www.cnblogs.com/EasonJim/p/7086916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值