2017-09-20 4 views
0

내 프로젝트 트리는 다음과 같습니다Thymeleaf + Spring-Boot - 정적 리소스에 액세스 할 수없는 이유는 무엇입니까?

enter image description here

내가 지금 템플릿에 액세스 할 수 있지만 같은 CSS, 이미지, JS 등의 정적 리소스를로드 할 수 없습니다.

<link rel="stylesheet" th:href="@{/css/app.css}"/> 

헤더 조각 나는 common.html과 같이 다음과 같습니다 :

은 내가 예를 들어, 내 모든 정적 리소스를 정의하라 common.html 조각이

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

<head th:include="fragments/common :: commonFragment" lang="en"></head> 
// page body 
</html> 

레이아웃에 대한 default.html 파일 :

<!DOCTYPE html> 
<html lang="en" 
     xmlns:th="http://www.thymeleaf.org" 
     xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 

<head th:include="fragments/common :: commonFragment" lang="en"> 
    <meta charset="utf-8"/> 
    <meta name="robots" content="noindex, nofollow"/> 
    <title>Touch</title> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/> 
    <meta http-equiv="content-dataType" content="text/html; charset=utf-8"/> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
    <meta name="apple-mobile-web-app-capable" content="yes"/> 
    <link rel="shortcut icon" th:href="@{/static/images/favicon.ico}" type="image/x-icon" /> 
</head> 

<body> 
<div th:id="defaultFragment" th:fragment="defaultFragment" class="container"> 
    <div id="header" th:replace="fragments/header :: headerFragment" /> 
     <div layout:fragment="content" /> 
</div> 
</body> 
</html> 

또한, 내 application.properties 파일에, 나는 이러한 항목이 있습니다

#SPRING RESOURCE HANDLING 
spring.resources.static-locations=classpath:/resources/ 

#THYMELEAF 
spring.thymeleaf.cache = true 
spring.thymeleaf.check-template = true 
spring.thymeleaf.check-template-location = true 
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.enabled=true 
spring.thymeleaf.encoding=UTF-8 
spring.thymeleaf.mode=HTML5 
spring.thymeleaf.prefix=classpath:templates/ 
spring.thymeleaf.suffix=.html 
spring.thymeleaf.template-resolver-order=1 

을하지만 같은 No mapping found 메시지가 계속.

32190 [http-nio-8081-exec-2] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet' 

여기에 표시되지 않는 항목은 무엇입니까?

답변

1

사용 spring.resources.static - 위치 정적 자원 위치 (직접 공개를 만들기)를 정의하기 위해 당신의 재산에 :

spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/ 
당신이 쉼표로 구분 된 값 taken from the documentation을, 즉 제공 할 수

:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources. 
+0

이것은 첫 번째 접근 방식 이었지만 작동하지 않았습니다. 나는 아주 기본적인 프로젝트 구조를 사용하고있다. Spring Boot는이 문제를 정말로 가지고 있어야한다 ... – gtludwig

+0

위의 속성을 추가 할 때 어떤 오류가 발생합니까? – dimitrisli

+0

spring.resources.static-locations = classpath :/resources/static/css /, classpath :/resources/static/images /, classpath :/resources/static/js /''를'application.properties'에 추가하고 있습니다. '51669 [http-nio-8081-exec-8] WARN osweb.servlet.PageNotFound - DispatcherServlet의 URI가 'dispatcherServlet'인 HTTP 요청 (/central/css/app.css)에 대해 매핑이 없습니다. – gtludwig

0

시도 링크에서 정적 자원 매핑을 추가합니다

<link rel="stylesheet" th:href="@{/css/app.css}" 
href="../../../css/app.css" /> 
+0

정적 매핑'href = "../../../ css/app.css"를 추가하면 스프링 부트가 이것을 처리하도록하는 모든 점을 무효로 할 것입니다. – gtludwig