SiteMesh实战

SiteMesh: OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰外观的分离。

1、下载siteMesh jar 文件 放到项目中:

https://github.com/sitemesh/sitemesh3/downloads
2、配置web.xml文件

<!--sitemesh配置   start  -->
  	<!-- <filter>
		<filter-name>sitemeshFilter</filter-name>
		<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
	</filter> -->
	<!-- 通过DelegatingFilterProxy使sitemesh成为受Spring管理的filter -->
	<filter>
		<filter-name>sitemeshFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetBeanName</param-name>
			<param-value>sitemeshFilterBean</param-value>
		</init-param>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
  	
  	<filter-mapping>
		<filter-name>sitemeshFilter</filter-name>
		<url-pattern>*.html</url-pattern>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>
	<filter-mapping>
		<filter-name>sitemeshFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>
  	<!-- Sitemesh configuration file location -->
	<context-param>
		<param-name>sitemesh.configfile</param-name>
		<param-value>/WEB-INF/classes/conf/sitemesh.xml</param-value>
	</context-param>
	
	<!--sitemesh配置  end  -->

 3.创建sitemesh.xml文件

 

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/classes/conf/decorators.xml"/>
    <excludes file="${decorators-file}"/> 
    <page-parsers>
        <parser default="true" content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
    </page-parsers>
    <decorator-mappers>
		<!-- for selecter -->
        <mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
			<param name="decorator" value="selecter" />
        </mapper>
		<!-- for print  在 url 后加上 printable=true  去除装饰 -->
        <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
			<param name="decorator" value="printable" />
			<param name="parameter.name" value="printable" />
			<param name="parameter.value" value="true" />
        </mapper>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}"/> 
        </mapper>
    </decorator-mappers>    
</sitemesh>

 

4、创建decorators.xml文件

 

<decorators defaultdir="/decorators">
	<!--excludes 用于不使用装饰器 -->
	<excludes>		
		<pattern>/index.jsp</pattern>
		<pattern>/test*</pattern>
	</excludes>	 
    <decorator name="main" page="decMain.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

 

 5、写装饰器页面如:decMain.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator"  prefix="decorator"%>
<!-- 引入标签 ,使用标签-->

<html>
  <head>       
    <title><decorator:title default="代表标题"></decorator:title></title>	
  </head>  
  <body>
 	页眉
 	<hr/>
 	<decorator:body/> 	
 	页脚
  </body>
</html>

 

此时就可以加上装饰器了。

详细解释请下载此文档。

你可能感兴趣的:(SiteMesh教程)