2016-07-07 353 views
1

我使用thymeleaf和springboot構建框架。並嘗試使用百里香的佈局模板。問題是使用ThymeleafLayoutInterceptor使用佈局模板時。 css和js的網址無法找到。Spring boot thymeleaf佈局[使用URI * .css/*。js找不到HTTP請求的映射]

的代碼和配置如下:

您可以通過鏈接project view

public class ThymeleafLayoutInterceptor extends HandlerInterceptorAdapter { 

private static final String DEFAULT_LAYOUT = "layouts/default"; 

private static final String DEFAULT_VIEW_ATTRIBUTE_NAME = "view"; 

@Override 
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 
    if (modelAndView == null || !modelAndView.hasView()) { 
     return; 
    } 
    String originalViewName = modelAndView.getViewName(); 
    if (isRedirectOrForward(originalViewName)) { 
     return; 
    } 
    modelAndView.setViewName(DEFAULT_LAYOUT); 
    modelAndView.addObject(DEFAULT_VIEW_ATTRIBUTE_NAME,originalViewName); 
} 

private boolean isRedirectOrForward(String viewName) { 
    return viewName.startsWith("redirect:") || viewName.startsWith("forward:"); 
} 

}

@Configuration

公共類WebMvcConfig擴展WebMvcConfigurationSupport {看項目佈局

@Override 
protected void addInterceptors(InterceptorRegistry registry) { 
    super.addInterceptors(registry); 
    registry.addInterceptor(new ThymeleafLayoutInterceptor()); 
} 

@Bean 
public ServletContextTemplateResolver templateResolver(){ 
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(); 
    resolver.setPrefix("/templates/views/"); 
    resolver.setSuffix(".html"); 
    resolver.setTemplateMode("HTML5"); 
    resolver.setOrder(1); 
    return resolver; 
} 

@Bean 
public SpringTemplateEngine templateEngine(){ 
    Set<IDialect> dialects = new HashSet<>(); 
    dialects.add(new LayoutDialect()); 
    SpringTemplateEngine engine = new SpringTemplateEngine(); 
    engine.setTemplateResolver(templateResolver()); 
    engine.setAdditionalDialects(dialects); 
    return engine; 
} 

@Bean 
public ThymeleafViewResolver thymeleafViewResolver(){ 
    ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 
    resolver.setTemplateEngine(templateEngine()); 
    resolver.setViewNames(new String[]{"*","springBootMvc/js/*","springBootMvc/css/*"}); 
    return resolver; 
} 

}

default.html中

<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
 

 
<head> 
 
    <link th:href="@{/dataTable/media/css/jquery.dataTables.css}" rel="stylesheet" type="text/css"/> 
 
    <link href="css/boot.css" rel="stylesheet" type="text/css"/> 
 
    <link th:href="@{/bootstrap/css/bootstrap.min.css}" rel="stylesheet" type="text/css"/> 
 

 
    <script th:src="@{/dataTable/media/js/jquery.js}" type="text/javascript" charset="utf8"/> 
 
    <script th:src="@{/dataTable/media/js/jquery.dataTables.js}" type="text/javascript" charset="utf8"></script> 
 
    <script th:src="@{/bootstrap/js/bootstrap.min.js}" type="text/javascript" charset="utf8"/> 
 
    <script th:src="@{/js/boot.js}" type="text/javascript" charset="utf8"/> 
 
</head> 
 
<body> 
 
<div th:raplace="fragments/header :: header"> 
 
    Header1 
 
</div> 
 
<div th:replace="${view} :: content"> 
 
    Content 
 
</div> 
 
<div th:replace="fragments/footer :: footer"> 
 
    Footer 
 
</div> 
 
</body> 
 
</html>

的.html

application.yaml

server: 
 
    context-path: /springBootMvc 
 
    port: 8082 
 

 
spring: 
 
    profiles: 
 
    active: test 
 
    messages: 
 
    basename: i18n 
 
    devtools: 
 
    restart: 
 
     exclude: static/** 
 
     additional-paths: src/main/ 
 
    thymeleaf: 
 
    prefix: /templates/views/ 
 
    suffix: .html

然後發生了一個問題,我無法得到* .js和* .css的網址。 錯誤堆棧如下:

2016-07-07 09:22:08.427 WARN 9572 --- [nio-8082-exec-2] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/css/boot.css] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.452 WARN 9572 --- [nio-8082-exec-4] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/dataTable/media/js/jquery.js] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.454 WARN 9572 --- [nio-8082-exec-5] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/dataTable/media/css/jquery.dataTables.css] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.457 WARN 9572 --- [nio-8082-exec-6] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/bootstrap/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.461 WARN 9572 --- [nio-8082-exec-7] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/dataTable/media/js/jquery.dataTables.js] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.475 WARN 9572 --- [nio-8082-exec-3] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/bootstrap/js/bootstrap.min.js] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.740 WARN 9572 --- [nio-8082-exec-8] o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/springBootMvc/js/boot.js] in DispatcherServlet with name 'dispatcherServlet' 
 
2016-07-07 09:22:08.745 ERROR 9572 --- [nio-8082-exec-2] o.a.c.c.C.[Tomcat].[localhost]   : Exception Processing ErrorPage[errorCode=0, location=/error]

如果我刪除類WebMvcConfig,那些* .css和* .js文件可能被發現。但佈局模板不再適用。 screen with js, css but no header and footer

回答

0

最後,我通過閱讀大量源代碼解決了這個問題。我發現在我們的配置中缺少SimpleUrlHandlerMapping。我用代碼配置它。它可能有點複雜。如果有人有一個更簡單的方法,歡迎與我聯繫!

@Bean 
 
    public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { 
 
     SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); 
 
     Map<String, Object> map = new LinkedHashMap<>(); 
 
     ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler(); 
 
     List<Resource> locations = new ArrayList<>(); 
 
     locations.add(new ServletContextResource(getServletContext(), "/")); 
 
     locations.add(new ClassPathResource("META-INF/resources")); 
 
     locations.add(new ClassPathResource("resources/")); 
 
     locations.add(new ClassPathResource("static/")); 
 
     locations.add(new ClassPathResource("public/")); 
 
     resourceHttpRequestHandler.setLocations(locations); 
 
     resourceHttpRequestHandler.setApplicationContext(getApplicationContext()); 
 

 
     List<ResourceResolver> resourceResolvers = new ArrayList<>(); 
 
     PathResourceResolver resourceResolver = new PathResourceResolver(); 
 
     resourceResolver.setAllowedLocations(new ServletContextResource(getServletContext(), "/"), new ClassPathResource("META-INF/resources"), new ClassPathResource("resources/"), new ClassPathResource("static/"), new ClassPathResource("public/")); 
 
     resourceResolvers.add(resourceResolver); 
 

 
     resourceHttpRequestHandler.setResourceResolvers(resourceResolvers); 
 
     map.put("/**", resourceHttpRequestHandler); 
 
     simpleUrlHandlerMapping.setUrlMap(map); 
 
     ResourceUrlProvider resourceUrlProvider = new ResourceUrlProvider(); 
 
     Map<String, ResourceHttpRequestHandler> handlerMap = new LinkedHashMap<>(); 
 
     handlerMap.put("/**", resourceHttpRequestHandler); 
 
     resourceUrlProvider.setHandlerMap(handlerMap); 
 
     ResourceUrlProviderExposingInterceptor interceptor = new ResourceUrlProviderExposingInterceptor(resourceUrlProvider); 
 
     simpleUrlHandlerMapping.setInterceptors(new Object[]{interceptor}); 
 
     return simpleUrlHandlerMapping; 
 
    }

0

默認春天啓動的資源應該放在 「的src/main /資源」,例如js和css文件應該放在/靜態文件夾 「的src/main /資源/ static/css「同樣適用於模板文件夾,如果你不需要任何特殊的東西,幾乎不需要任何配置。 嘗試運行您的應用程序並檢查日誌記錄,彈出日誌記錄所有映射,您可以檢查默認映射。

當我回家時,我會附上我的項目層次結構。

5

在擴展WebMvcConfigurerAdapter的類中,我使用了@EnableWebMvc註釋,關於相同的佈局示例。

我刪除了這個註解,佈局工作原理相同,並且加載了css。

我希望這會有所幫助。

相關問題