SiteMesh在项目中的配置

时间:2022-07-28 16:14:47
SiteMesh在项目中的配置
首先在web.xml里面增加siteMesh的配置:
<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>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
凡是提交的.do和.jsp的请求,都要经过siteMesh的处理,哈哈,当然html的就不用处理啦
另外在WEB-INF下面还有一个配置sitemesh.xml:
<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" />
</page-parsers>
<decorator-mappers>
<mapper
class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla [" value="ns" />
<param name="match.Opera" value="opera" />
<param name="match.Lynx" value="lynx" />
</mapper>
<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.RobotDecoratorMapper">
<param name="decorator" value="robot" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
其实这个配置倒是一般是默认的。嘿嘿。不过我当时也研究了一下。不过主要的还是这个文件是在SITEMESH的JAR包中自动装载进来的,它决定了另外一个配置文件的位置和名字,就是decorators.xml:
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="default" page="default.jsp" />
<decorator name="defaulths" page="default_hs.jsp"/>
<decorator name="main" page="main.jsp" />
<decorator name="mainhs" page="main_hs.jsp"/>
</decorators> 这里配置了4个渲染的模板,同时也给这四个模板定义了名字。看看模板的内容,比如这个main.jsp:
<%@ include file="/common/taglibs.jsp"%>
<html>
<head>
<title>
<decorator:title default="DV110.com" />
</title>
<%@ include file="/common/meta.jsp"%>
<decorator:head />
</head>
<body id="homepage">
<div id="header">
<jsp:include page="/common/header.jsp" />
</div>
<div align="center" style="height:500px;">
<table border="0" cellpadding="0" cellspacing="0" width="779" align="center">
<tr>
<td valign="top" >
<%@ include file="/common/messages.jsp"%>
<h1 align="center">
<decorator:getProperty property="page.heading" />
</h1>
<decorator:body />
</td>
</tr>
</table>
</div>
<div id="footer">
<jsp:include page="/common/footer.jsp" />
</div>
</body>
</html>
以上是系统知道要用SiteMesh来处理后,要使用的模板。
以下是系统的页面,比如设备更新页面,会在页面中有个<meta name="decorator" content="default${dName}">
其中content=""这里面的名字就是我们配置的模板的名字。
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<html>
<head>
<title>设备信息更新</title>
<meta name="decorator" content="default${dName}">
</head>
<body>
<html:form action="deviceVIS.do?method=updateDevice" method="post">
<html:hidden property="id" />
<table width="516" border="0" align="center">
<tr>
<td width="84">
设备名称
</td>
<td width="288">
<html:text property="name" />
</td>
<td width="122">
 
</td>
</tr>
<tr>
<td>
安装地址
</td>
<td>
<html:text property="installLocation" />
</td>
<td>
 
</td>
</tr>
<td>
 
</td>
<td>
<input type="submit" name="Submit" value="保存" />
<input type="reset" name="cancel" value="重置" />
</td>
<td>
 
</td>
</tr>
</table>
</html:form>
</body>
</html>