pom.xml
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>
web.xml
<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>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
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.xxx.web.common.FixedConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
FixedConfigDecoratorMapper.java
解决Weblog路径问题
import java.lang.reflect.Field;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper;
import com.opensymphony.module.sitemesh.mapper.ConfigLoader;
/**
* 修复 sitemesh 部署在 weblogic 的路径匹配问题
*/
public class FixedConfigDecoratorMapper extends ConfigDecoratorMapper {
public Decorator getDecorator(HttpServletRequest request, Page page) {
String thisPath = request.getServletPath();
if (thisPath == null) {
String requestURI = request.getRequestURI();
if (request.getPathInfo() != null) {
thisPath = requestURI.substring(0,
requestURI.indexOf(request.getPathInfo()));
} else {
thisPath = requestURI;
}
} else if ("".equals(thisPath)) {
thisPath = request.getPathInfo();
} else if (thisPath.endsWith(".jsp")) {
// weblogic将输入的url转换成实际的JSP
String contextPath = request.getContextPath();
String requestURI = request.getRequestURI();
thisPath = requestURI.substring(contextPath.length());
}
String name = null;
try {
ConfigLoader configLoader = (ConfigLoader) getValueByFieldName(
this, "configLoader");
name = configLoader.getMappedName(thisPath);
} catch (ServletException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Decorator result = getNamedDecorator(request, name);
return ((result == null) ? super.getDecorator(request, page) : result);
}
public static Field getFieldByFieldName(Object obj, String fieldName) {
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
}
}
return null;
}
public static Object getValueByFieldName(Object obj, String fieldName)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field field = getFieldByFieldName(obj, fieldName);
Object value = null;
if (field != null) {
if (field.isAccessible()) {
value = field.get(obj);
} else {
field.setAccessible(true);
value = field.get(obj);
field.setAccessible(false);
}
}
return value;
}
}
decorators.xml 示例
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/WEB-INF/view/layout/">
<excludes>
<pattern>/static/*</pattern>
<pattern>/download/*</pattern>
</excludes>
<decorator name="help" page="help.jsp">
<pattern>/site/help*</pattern>
</decorator>
<decorator name="none" page="none.jsp">
</decorator>
<decorator name="main" page="main.jsp">
<pattern>/</pattern>
</decorator>
</decorators>
这样配置后,请求的url会去找到decorators.xml匹配的具体decorator制定的page
jsp中就可以引入<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
再就可以套用模板了。
分享到:
相关推荐
SiteMesh是一个网页布局和装饰框架以及Web应用程序集成框架,可帮助创建由页面组成的网站,这些页面需要一致的外观,导航和布局方案。 SiteMesh会拦截对通过Web服务器请求的任何静态或动态生成的HTML页面的请求,...
Struts 2.0提供一个Sitemesh插件,允许在Sitemesh模板中使用Struts标记。 要使用Sitemesh需要包含Freemark,Sitemesh和Sitemesh插件库文件。 配置过滤器 如果需要使用Freemark模板文件作为装饰器文件,需要在web....
MiddleGen-Hibernate: 本书使用版本:2.1 ... modtime=1096973436&big_mirror=0 下载文件:middlegen-2.1.zip MiddleGenIDE插件: 本书使用版本:1.2.0 ...(7)Sitemesh: ...下载文件:sitemesh-2.3.zip
Sitemesh简介: SiteMesh是一个Web页面布局修饰框架, 用于构建包含大量页面, 需要一致的外观样式(look/fell), 导航和布局机制的大型网站. sitemesh应用Decorator模式,用filter截取request和response,把页面组件...
用sitemesh页面装饰器,将大名鼎鼎的开源即时通讯服务器openfire中运用的,布局页面抽取出来。MyEclipse中可以直接部署的代码。
sitemesh 应用 Decorator 模式,用 filter 截取 request 和 response,把页面组件 d,content,banner 结合为一个完整的视图。通常我们都是用 include 标签在每个 jsp 页面中来 断的包含各种header , ...
web布局框架 SiteMesh教程及SiteMesh官方文档翻译
SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。Sitemesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大 量页面构成的...
siteMesh demo siteMesh使用文档
sitemesh.jar包 sitemesh.jar 包sitemesh.jar 包sitemesh.jar包
sitemesh-2.2.1.jar
很简单的sitemesh入门教程,希望对大家有所帮助
sitemesh 装饰页面技术.
sitemesh-2.4.1.jar sitemesh-2.4.1.jar sitemesh-2.4.1.jar sitemesh-2.4.1.jar sitemesh-2.4.1.jar sitemesh-2.4.1.jar
spring-boot-sitemesh 源码整合
springMVC与sitemesh的结合,
sitemesh3-demo 配置说明参见http://blog.csdn.net/thc1987/article/details/6913416
公司的OA流項目視圖層都是採用sitemesh技術,感覺還可以,裝潢頁面,技術不是很複雜,頁面結構清晰,佈局明朗,很好的佈局框架。
sitemesh3包括lib,api,demo,配置说明,希望大家喜欢