sitemesh

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,模板技术)