SiteMesh参考
来源:互联网 发布:美漫 安卓 知乎 编辑:程序博客网 时间:2024/05/22 07:50
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文件
<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文件,并且输入一下内容
<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
<pattern>/test1.jsp</pattern>
</decorator>
- 在{myapp}/decorators目录下添加mydecorator1.jsp文件,内容如下:
<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 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
<pattern>/test2.jsp</pattern>
</decorator>
- 在{myapp}/decorators目录下添加mydecorator2.jsp文件,内容如下:
<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 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 <decorator:getProperty> 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
<pattern>/test3.jsp</pattern>
</decorator>
<decorator name="mydecorator31" page="mydecorator31.jsp">
</decorator>
- 在{myapp}/decorators目录下添加mydecorator3.jsp文件,内容如下:
<%@ 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>
<%@ 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 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 <page:applyDecorator> tag</b>
</body>
</html>
- 打开浏览器,访问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
<pattern>/test4.jsp</pattern>
</decorator>
<decorator name="mydecorator41" page="mydecorator41.jsp">
</decorator>
- 在{myapp}/decorators目录下添加mydecorator4.jsp文件,内容如下:
<%@ 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>
<%@ 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 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 <page:param> 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
- SiteMesh参考
- sitemesh
- SiteMesh
- Sitemesh
- Sitemesh
- SiteMesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- SiteMesh
- SiteMesh
- sitemesh
- SiteMesh
- SiteMesh
- servlet笔记
- 俄罗斯教科书是这样描述中国的
- 用Eclipse远程调试部署在Tomcat下的Web应用程序
- IT 学习SQL 数据库网、ASP.NET、XML、的网址,,
- 中国一不小心反成了美国“山姆大叔”的大债主!
- SiteMesh参考
- 修改Struts2默认的模板配置
- 今天了解了一下“鲁棒性”的由来
- java string特性
- ant心得
- 虫子发挥一下想象 ,背一下韩国基本字母。嘿嘿 。
- 了解实际开发中 Hashtable 的特性原理
- 2007年5月12日,地坛书市
- 设计模式笔记(第一章 概述)