Spring Web的UrlPathHelper的主要方法

基於Spring Web開發的應用,下面這幾個方法比較經常使用,標記一下html

/**
 * 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,此種方式有兩種:web

第一種:spring

application.yml中app

springide

  resourcesspa

    static-locations:htm

      - file:///資源

這種方式,是很是簡單的,也比較高效,但若是訪問的資源來自模版,則還要檢查4次開發

第二種:部署

自定義一個環境配置,spring.static.location=${project.root}/xxx

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations(staticLocation)
            .setCachePeriod(0);
}

這種方式,只檢查一個location,感到要省時一些,但多了一個自定義配置,而且解決了刷新問題,這個是關鍵

相關文章
相關標籤/搜索