私はフレームワークを構築するためにthymeleafとspringbootを使用します。そして、thymeleafのレイアウトテンプレートを使用しようとします。問題は、ThymeleafLayoutInterceptorを使用してレイアウトテンプレートを使用する場合です。 cssとjsのURLをもう見つけられませんでした。Springブート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のURLを取得できませんでした。 エラー・スタックは以下の通りです:
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