我使用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