`

使用sitemesh的基本配置

阅读更多

使用sitemesh的基本配置

 

1、官网 : http://www.opensymphony.com/sitemesh/

     下载链接:http://www.opensymphony.com/sitemesh/download.action

 

官网说法:

 

SiteMesh - SiteMesh Installation and Configuration

 

Once SiteMesh has been downloaded (or built), configuration is simple.

 

    * Setup a web-app as usual (or skip all this by using the pre-configured sitemesh-blank.war).

    * Copy sitemesh-2.4.1.jar into [web-app]/WEB-INF/lib.

    * Create the file [web-app]/WEB-INF/decorators.xml that contains the following:

 

          <decorators>

          </decorators>

 

    * (Optional) Create the file [web-app]/WEB-INF/sitemesh.xml that contains the following:

 

      <sitemesh>

          <property name="decorators-file" value="/WEB-INF/decorators.xml" />

          <excludes file="${decorators-file}" />

 

          <page-parsers>

              <parser content-type="text/html"

                  class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />

              <parser content-type="text/html;charset=ISO-8859-1"

                  class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />

          </page-parsers>

 

          <decorator-mappers>

              <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">

                  <param name="config" value="${decorators-file}" />

              </mapper>

          </decorator-mappers>

      </sitemesh>

 

    * Add the following to [web-app]/WEB-INF/web.xml within the <web-app> tag:

 

      <filter>

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

          <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>

      </filter>

 

      <filter-mapping>

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

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

      </filter-mapping>

 

从官网的介绍可知,使用sitemesh要进行3个步骤的配置:

 

1、导入架包:sitemesh-2.4.1.jar

2、配置decorators.xml(必须有),sitemesh.xml(可选,但最好配置)

3、web.xml中注册sitemesh过滤器

 

下面将上面所说的3步的详细配置过程介绍如下:

第一步:导入架包。

进入官网后可以看到如下结构:(选择JAR链接下载的是需要的架包)

Version 2.4.1

下载得到了sitemesh-2.4.1.jar架包后,在你项目中引入。第一步就搞定了。

第二步:配置相关的XML文件。
在你项目的/WebRoot/WEB-INF/目录下新建一个decorators.xml和一个sitemesh.xml的空的XML文件。
在decorators.xml中这样配置:
<?xml version="1.0" encoding="UTF-8"?>
<!--* defaultdir: 包含装饰器页面的目录
    * page : 页面文件名
    * name : 别名
    * role : 角色,用于安全
    * webapp : 可以另外指定此文件存放目录
    * Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。 
 --> 
<decorators defaultdir="/decorators">
 <!-- 用来定义不需要过滤的页面 -->  
 <excludes>
 <!-- 过滤掉load进的页面 --> 
  <pattern>/page/lesquare/specialties/special_java.jsp</pattern>
  <pattern>/page/lesquare/specialties/special_.net.jsp</pattern>
 </excludes>
 <!-- 用来定义装饰器要过滤的页面 -->  
 <decorator name="siteMesh" page="siteMesh.jsp">
  <pattern >/*</pattern>
 </decorator>
</decorators>

在sitemesh.xml可以这样配置:
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />
    <excludes file="${decorators-file}" />

    <page-parsers>
        <parser content-type="text/html"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
        <parser content-type="text/html;charset=UTF-8"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
</sitemesh>

第三步:在web.xml中注册sitemesh过滤器
<!-- sitemesh过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

经过上面的配置你的sitemesh就可以工作了,其实就这么简单。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics