在匹配的时候,使用的如下的方法
/** Retrieve {@link com.opensymphony.module.sitemesh.Decorator} based on 'pattern' tag. */
public Decorator getDecorator(HttpServletRequest request, Page page) {
String thisPath = request.getServletPath();
// getServletPath() returns null unless the mapping corresponds to a servlet
if (thisPath == null) {
String requestURI = request.getRequestURI();
if (request.getPathInfo() != null) {
// strip the pathInfo from the requestURI
thisPath = requestURI.substring(0, requestURI.indexOf(request.getPathInfo()));
}
else {
thisPath = requestURI;
}
}
String name = null;
try {
name = configLoader.getMappedName(thisPath);
}
catch (ServletException e) {
e.printStackTrace();
}
Decorator result = getNamedDecorator(request, name);
return result == null ? super.getDecorator(request, page) : result;
}
在匹配时,使用的是 request.getServletPath();springmvc中,我们的配置如下:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
即/app/下的所有访问,是由DispatcherServlet来处理的,也就是所所有的/app执行request.getServletPath()获取到的path都是/app,故我们如果需要装饰,那么就需要如下配置:
<pattern>/app</pattern>
这样就会装饰所有/app开头的地址(*可加可不加,加的话,会匹配/app1,/app/app等,可能不符合要求,建议不加。但一定不要配置成 <pattern>/app/*</pattern>,这样就会装饰不到)。而在排除装饰时,使用的方法如下:
factory.isPathExcluded(extractRequestPath(request))
这里的
private String extractRequestPath(HttpServletRequest request) {
String servletPath = request.getServletPath();
String pathInfo = request.getPathInfo();
String query = request.getQueryString();
return (servletPath == null ? "" : servletPath)
+ (pathInfo == null ? "" : pathInfo)
+ (query == null ? "" : ("?" + query));
}
看到了吧,他是servletPath、pathInfo、query三者的组合,所以在配置排除时,我们可以加上pathInfo,如/app下排除装饰:
<excludes>
<pattern>/app/users*</pattern>
</excludes>