天天看点

SiteMesh3 介绍和使用

    Sitemesh是由一个基于Web页面布局、装饰及与现存Web应用整合的框架。它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一 致的导航条、一致的banner、一致的版权等。它不仅能处理动态的内容,如JSP、PHP、ASP、CGI等产生的内容,还能处理静态的内容,比如 HTML的内容,使得它的内容也符合你的页面结构的要求。甚至它能像include那样将HTML文件作为一个面板的形式嵌入到别的文件中去。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他

SiteMesh3 介绍和使用

要添加的文件:sitemesh-3.0-alpha-2.jar,struts-sitemesh-plugin-2.3.16.3.jar,sitemesh3.xml,最后要在web.xml文件中配置一下sitemesh,配置如下 :

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>SiteMesh3TestProject</display-name>

  <filter>

    <filter-name>sitemesh</filter-name>

    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>

  </filter>

  <filter-mapping>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

</web-app>

然后,就是新建你的母版页了(Template.jsp),如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title> <sitemesh:write property='title'/></title>

     <sitemesh:write property='head'/>

  </head>

  <body>

      <sitemesh:write property='body'/>

  </body>

</html>

接着,再修改sitemesh.xml文件,这里我修改的是我自己的母版页,个人对应自己的地址:

<sitemesh>

  <mapping path="/*" decorator="/Template.jsp"/>

</sitemesh>