当前位置: 首页 > news >正文

SiteMesh参考

SiteMesh参考

作者:kongxx (kongxx@gmail.com)

安装

  • 首先从sitemesh下载安装包,这里使用的是2.2.1版本。
  • 创建一个Web应用程序,这里我创建一个名为myapp的Web应用程序;
  • 复制sitemesh-2.2.1.jar文件到{myapp}/WEB-INF/lib目录下;
  • 编辑{myapp}/WEB-INF/web.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
    <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
   
    <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
   
    <session-config>
    <session-timeout>
        30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>
        index.jsp
    </welcome-file>
    </welcome-file-list>
</web-app>

添加蓝色高亮部分。
  • 在{myapp}/WEB-INF/目录下创建decorators.xml文件,并且输入一下内容
<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>

    <decorator name="panel" page="panel.jsp"/>
    <decorator name="printable" page="printable.jsp"/>
</decorators>
  • 安装完毕。

例子1

  • 在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator
<decorator name="mydecorator1" page="mydecorator1.jsp">
        <pattern>/test1.jsp</pattern>
    </decorator>
  • 在{myapp}/decorators目录下添加mydecorator1.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
    <head>
        <title>My Site - <decorator:title default="Welcome!" /></title>
        <decorator:head />
    </head>
    <body>
        <decorator:body />
        <p>This message is in /decorators/mydecorator1.jsp</p>       
    </body>
</html>
  • 在{myapp}目录下添加test1.jsp文件,内容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test1</title>
    </head>
    <body>
    <b>This is test1</b>
    </body>
</html>

  • 打开浏览器,访问http://localhost:8080/myapp/test1.jsp,将会出现一下内容:

This is test1

This message is in /decorators/mydecorator1.jsp


例子2 (decorator:getProperty tag)

有时候,我们期望修改页面中某个有固定标记的片段,例如我们的jsp中有一个标记<mytag>...</mytag>,此时可以用如下方法实现:
  • 在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator
<decorator name="mydecorator2" page="mydecorator2.jsp">
        <pattern>/test2.jsp</pattern>
    </decorator>
  • 在{myapp}/decorators目录下添加mydecorator2.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>

<html>
    <head>
        <title>My Site - <decorator:title default="Welcome!" /></title>
        <decorator:head />
    </head>

    <body>
        <decorator:body />

        <decorator:getProperty property="page.content1"/>
        <decorator:getProperty property="page.content2"/>
       
        <!-- do nothing -->
        <decorator:getProperty property="page.content3"/>
       
        <p>This message is in /decorators/mydecorator2.jsp</p>
    </body>
</html>
  • 在{myapp}目录下添加test2.jsp文件,内容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test2</title>
    </head>
   
    <body>
    <b>This is test2</b>
    <b>Use &lt;decorator:getProperty&gt; tag</b>
   
    <content tag="content1"><p>This is content1</p></content>
    <content tag="content2"><p>This is content2</p></content>
    <content tag="content4"><p>This is content4, it shouldn't be display</p></content>
    </body>
</html>
  • 打开浏览器,访问http://localhost:8080/myapp/test2.jsp,将会出现一下内容:

This is test2

Use <decorator:getProperty> tag

This is content1

This is content2

This message is in /decorators/mydecorator2.jsp

例子3 (page:applyDecorator tag)

  • 在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator
    <decorator name="mydecorator3" page="mydecorator3.jsp">
        <pattern>/test3.jsp</pattern>
    </decorator>
    
    <decorator name="mydecorator31" page="mydecorator31.jsp">
    </decorator>
  • 在{myapp}/decorators目录下添加mydecorator3.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<html>
    <head>
        <title>My Site - <decorator:title default="Welcome!" /></title>
        <decorator:head />
    </head>

    <body>
        <decorator:body />

        <page:applyDecorator name="mydecorator31">
            <content tag="content1"><p>This is content1</p></content>
            <content tag="content2"><p>This is content2</p></content>
        </page:applyDecorator>
    </body>
</html>
在{myapp}/decorators目录下添加mydecorator31.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>

<p><i>begin</i></>
<decorator:getProperty property="page.content1"/>
<decorator:getProperty property="page.content2"/>
<p><i>end</i></>
  • 在{myapp}目录下添加test3.jsp文件,内容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test3</title>
    </head>
   
    <body>
    <b>This is test3</b>
    <b>Use &lt;page:applyDecorator&gt; tag</b>
    </body>
</html>
注意:相对于例子2,这里已经没有了<content tag="XXX"/>标签。
  • 打开浏览器,访问http://localhost:8080/myapp/test3.jsp,将会出现一下内容:

This is test3

Use <page:applyDecorator> tag

begin

This is content1

This is content2

end

这里,我在mydecorator3.jsp中应用了mydecorator31.jsp的的decorator,并且将原来在test2.jsp中的 <content />标签复制到mydecorator3.jsp中,此时对于<content tag="xxx"/>的标签将会由mydecorator31.jsp了装饰。

例子4 (page:param tag)

  • 在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator
    <decorator name="mydecorator4" page="mydecorator4.jsp">
        <pattern>/test4.jsp</pattern>
    </decorator>
    
    <decorator name="mydecorator41" page="mydecorator41.jsp">
    </decorator>
  • 在{myapp}/decorators目录下添加mydecorator4.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>

<html>
    <head>
        <title>My Site - <decorator:title default="Welcome!" /></title>
        <decorator:head />
    </head>

    <body>
        <decorator:body />
        <page:applyDecorator name="mydecorator41" >
            <content tag="content1"><p>This is content1</p></content>
            <content tag="content2"><p>This is content2</p></content>
            <page:param name="page.content1"><p>This content1 has been replaced</p></page:param>
        </page:applyDecorator>
    </body>
</html>
在{myapp}/decorators目录下添加mydecorator41.jsp文件,内容如下:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>

<p><i>begin</i></>
<decorator:getProperty property="page.content1"/>
<decorator:getProperty property="page.content2"/>
<p><i>end</i></>
  • 在{myapp}目录下添加test4.jsp文件,内容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test4</title>
    </head>
   
    <body>
    <b>This is test4</b>
    <b>Use &lt;page:param&gt; tag</b>
    </body>
</html> 
  • 打开浏览器,访问http://localhost:8080/myapp/test4.jsp,将会出现一下内容:

This is test4

Use <page:param> tag

begin

This content1 has been replaced

This is content2

end

这里,我在mydecorator4.jsp中应用了mydecorator41.jsp的的decorator,并且添加了 <page:param name="page.content1">标签,那么此时页面上将会用<page:param>标签中的内容替换原来在 <decorator:getProperty property="page.content1"/> 中的内容,因此页面将不在 This is content1” 而显示 This content1 has been replaced ”。
 

相关文章:

  • 准备买个智能手机
  • 预加载图片问题 in ASP .NET
  • 软件开发C#中ref和out的使用小结
  • 封装的变化之不断变化的需求
  • ***利用Ms05002溢出找“肉鸡
  • 如何利用局域网来实现VLAN的实例
  • 关于windows 2003 权限划分
  • asp的站内搜索功能
  • html地址栏传值问题
  • 经典东北话集锦2
  • 汪中求讲座免费在线学习 免费下载
  • NAS DAS SAN三种存储方式的比较
  • 给WSS3.0增加用户与IP绑定功能
  • CrystalReport水晶报表
  • 网站SEO方案
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 3.7、@ResponseBody 和 @RestController
  • IP路由与转发
  • Javascript Math对象和Date对象常用方法详解
  • Python学习之路13-记分
  • SpiderData 2019年2月25日 DApp数据排行榜
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 使用putty远程连接linux
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • 组复制官方翻译九、Group Replication Technical Details
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (LeetCode) T14. Longest Common Prefix
  • (剑指Offer)面试题34:丑数
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (轉貼) UML中文FAQ (OO) (UML)
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • .NET Core 中插件式开发实现
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .net打印*三角形
  • [ 英语 ] 马斯克抱水槽“入主”推特总部中那句 Let that sink in 到底是什么梗?
  • [20161101]rman备份与数据文件变化7.txt
  • [Android Studio 权威教程]断点调试和高级调试
  • [Android]创建TabBar
  • [docker]docker网络-直接路由模式
  • [FC][常见Mapper IRQ研究]
  • [IDF]啥?
  • [iOS]-UIKit
  • [MySQL数据库部署及初始化相关]
  • [NOI2014]购票
  • [NOIP2017 提高组] 列队 题解
  • [office] 怎么在Excel2003菜单栏自定义一个选项卡 #其他#微信#知识分享
  • [Oh My C++ Diary]Main函数参数argc,argv如何传入
  • [Real world Haskell] 中文翻译:第一章 快速上手
  • [RK-Linux] 移植Linux-5.10到RK3399(一)| 搭建系统并让系统跑起来
  • [sd_scripts]之train
  • [StartingPoint][Tier1]Crocodile
  • [TestLink]testlink安装