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" %>

再就可以套用模板了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值