`
aijuans8
  • 浏览: 178409 次
社区版块
存档分类
最新评论

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”。
分享到:
评论

相关推荐

    ssm项目基础搭建及sitemesh标签

    刚上手ssm及maven时一直会问基础配置,以下可做参考,sitemesh标签的使用

    SiteMesh2.3很全的一个资料

    SiteMesh2.3(含带所需lib并且还有相关文档,实例作参考),天涯精心总结,吐血推荐!

    一个Struts2+Spring2.5+SiteMesh的小DEMO

    一个Struts2+Spring2.5+SiteMesh的小DEMO,仅供大家做个参考!我也刚学会。欢迎大家多交流!

    jsp中sitemesh修改tagRule技术分享

    主要介绍了jsp中sitemesh修改tagRule技术以及详细代码分析,有需要的朋友跟着小编一起学习参考下吧。

    基于maven3开发babasport源码(更新到38讲)

    \babasportrefactoring\babasportbluetripe为参考struts官方例子后重构的代码,同步更新到38讲; \babasportrefactoring\babasportcompact 为清理了非必要测试的子model2,Spring3.05 + Strtus2 + JPA(Hibernate) + ...

    Grails 中文参考手册

    6.2.4 使用Sitemesh布局 6.3 标签库 6.3.1 简单标签 6.3.2 逻辑标签 6.3.3 迭代标签 6.3.4 标签命名空间 6.4 URL映射 6.4.1 映射到控制器和操作 6.4.2 嵌入式变量 6.4.3 映射到视图 6.4.4 映射到响应代码 6.4.5 映射...

    基于SSM+MySQL的电子商城系统设计与实现.zip

    布局框架:SiteMesh 3.0.1 分布式应用程序协调服务:ZooKeeper 3.3.1 分布式服务框架:Dubbo 2.5.3 接口测试框架:Swagger2 2.6.1 工具类:Apache Commons、Jackson 2.2、fastjson 1.2.20 详细介绍参考:...

    Apache Shiro+SpringMVC+Hibernate Search企业信息管理系统

    7、SiteMesh 2.4 8、JQuery 1.9 9、Twitter Bootstrap 2.3.1 10、....其他技术就不讲了,具体参考项目 项目访问: 1)后台后台访问地址:http://localhost:8989/jeesite3/ 用户名:thinkgem(原作者名字)密码:...

    开发者突击·Java Web主流框架整合开发(J2EE+Struts+Hibernate+Spring)源码

    在《开发者突击:Java Web主流框架整合开发(J2EE+Struts+Hibernate+Spring)》架构讲解的过程中,穿插介绍了JDBC、Log4j、Sitemesh、JUnit技术。 随书附赠光盘内容包括《开发者突击:Java Web主流框架整合开发(J2EE+...

    Java高手真经_应用框架卷_Java_Web核心框架.part1.rar

     表现层框架struts 1:讲解struts 1的入门配置、核心组件、标签库、国际化、数据校验、sitemesh集成、数据库开发技术,并分别实现与hibernate、ibatis持久层框架的集成开发。..  表现层框架struts 2:讲解struts 2...

    Java高手真经_应用框架卷_Java_Web核心框架.part2.rar

     表现层框架struts 1:讲解struts 1的入门配置、核心组件、标签库、国际化、数据校验、sitemesh集成、数据库开发技术,并分别实现与hibernate、ibatis持久层框架的集成开发。..  表现层框架struts 2:讲解struts 2...

    Java高手真经_应用框架卷_Java_Web核心框架.part3.rar

     表现层框架struts 1:讲解struts 1的入门配置、核心组件、标签库、国际化、数据校验、sitemesh集成、数据库开发技术,并分别实现与hibernate、ibatis持久层框架的集成开发。..  表现层框架struts 2:讲解struts 2...

    Java高手真经 应用框架卷 源码

    《Java高手真经(应用框架卷):Java Web核心框架》适合作为Java相关培训机构的教材,也可作为Java自学人员的参考手册。书首先分析了Java Web应用的分层设计方法,并进行应用框架的选型,然后讲解各种Java Web应用框架...

Global site tag (gtag.js) - Google Analytics