sitemesh、springmvc装饰配置

本文介绍在SpringMVC框架下如何通过request.getServletPath()进行路径匹配以实现页面装饰,并讨论了如何配置和排除特定路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在匹配的时候,使用的如下的方法

 /** 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> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值