基于Spring Web开发的应用,下面这几个方法比较常用,标记一下
/** * Return the mapping lookup path for the given request, within the current * servlet mapping if applicable, else within the web application. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the lookup path * @see #getPathWithinApplication * @see #getPathWithinServletMapping */ public String getLookupPathForRequest(HttpServletRequest request)
/** * Return the path within the servlet mapping for the given request, * i.e. the part of the request's URL beyond the part that called the servlet, * or "" if the whole URL has been used to identify the servlet. * <p>Detects include request URL if called within a RequestDispatcher include. * <p>E.g.: servlet mapping = "/*"; request URI = "/test/a" -> "/test/a". * <p>E.g.: servlet mapping = "/"; request URI = "/test/a" -> "/test/a". * <p>E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a". * <p>E.g.: servlet mapping = "/test"; request URI = "/test" -> "". * <p>E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "". * @param request current HTTP request * @return the path within the servlet mapping, or "" */ public String getPathWithinServletMapping(HttpServletRequest request)
/** * Return the path within the web application for the given request. * <p>Detects include request URL if called within a RequestDispatcher include. * @param request current HTTP request * @return the path within the web application */ public String getPathWithinApplication(HttpServletRequest request)
使用spring boot+Freemarker,模版名称后缀是.html,请求的路径中.html,可能包括静态资源和模版资源,spring boot会优先检查静态资源是否存在,如果存在,就不会再查询模版资源了,但spring boot中缺省的静态资源检查路径有5个,检查太多了,并且,由于静态资源缺省定在main/resources下,这里的资源必须经过编译发布部署,开发阶段,静态资源改了,刷新比较麻烦,此处直接更改静态资源到绝对路径:file:///media/dd,此种方式有两种:
第一种:
application.yml中
spring
resources
static-locations:
- file:///
这种方式,是非常简单的,也比较高效,但如果访问的资源来自模版,则还要检查4次
第二种:
自定义一个环境配置,spring.static.location=${project.root}/xxx
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations(staticLocation) .setCachePeriod(0); }
这种方式,只检查一个location,感到要省时一些,但多了一个自定义配置,并且解决了刷新问题,这个是关键