2014-09-23 1 views
5

봄 MVC 및 Thymeleaf를 사용하여 프로젝트를하고 있습니다. 나는이 폴더 구조가있는 경우 내 CSS 파일을 참조하는 방법에 대해 질문이 있습니다 :봄용 MVC에 thymeleaf가있는 .css 파일을 참조하십시오.

src 
    main 
    webapp 
    resources 
     myCssFolder 
     myCssFile.css 
    web-inf 
     spring 
     views 
     myViewFolder 
      index.html 

내 구성 클래스는 다음과 같이이다 :

@Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/css/**").addResourceLocations("/css/**"); 
     registry.addResourceHandler("/img/**").addResourceLocations("/img/**"); 
     registry.addResourceHandler("/js/**").addResourceLocations("/js/**"); 
     registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**"); 
     registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**"); 
    } 

그리고 이런 내 인덱스 파일에 href 전화 :

href="resources/css/bootstrap.min.css" 

그러나 내 페이지에는 엉망인 일부 요소가 있습니다. 예를 들어 CSS가 작동하지 않습니다.

+0

그것은 좋은 것입니다 구조 및 리소스에 대한 서버에 실제로 생성 된 HTTP 요청 – geoand

+0

해결책을 찾았습니까? –

답변

7

CSS 파일을 참조 할 때 th:href 속성을 사용해야합니다. 다음은 thymeleaf 자습서의 샘플입니다. thymeleaf가 th:href 값을 평가할 수없는 경우 기본값은 href입니다.

<head> 
    <title>Good Thymes Virtual Grocery</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link rel="stylesheet" type="text/css" media="all" 
     href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" /> 
</head> 
+0

그것은 작동하지 않았고, 내 리소스는 괜찮습니까? – stackUser2000

+0

@Narresh가 당신에게 어떻게 사용했는지 보여주는 예가 없습니다 : href – ndrone

+1

구성이 올바른 것처럼 보입니다. 그러나 css 파일은'webapp/css /'폴더에 있어야하고'th : href = "@ {/ css/bootstrap.min.css}"를 사용하여 참조해야합니다. –

2

나는 그 단계가 나를 도왔다.

  1. 나는 /resources/css/myCSS.css 디렉토리가 있습니다. 그래서 /css/myCSS.css 같은 루트로 CSS를 넣어 제거했다 디렉토리/자원
  2. 나는이 같은 MyCSS 링크 : 당신이 나타났다 경우

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

관련 문제