2017-09-26 3 views
2

JAR 파일을 통해 모든 Spring 기반 Java 웹 응용 프로그램과 공통 프레임 워크 라이브러리를 사용할 수 있습니다. 나는보기를 위해 Thymeleaf를 사용하고 있습니다.Thymeleaf 템플릿에서 라이브러리 JAR 파일의 스타일 시트에 액세스하려면 어떻게해야합니까?

저는 여러 웹 응용 프로그램에서 사용되는 일반적인 Thymeleaf 템플릿이 있습니다. 템플릿 HTML 파일과 관련 스타일 시트는 모두 라이브러리 JAR 파일에 있습니다. 웹 애플리케이션이 프레임 워크 템플리트를 기반으로보기를 표시하면 템플릿이 Thymeleaf에 의해 발견되고 처리됩니다.

제 문제는 스타일 시트를 불러 와서 브라우저로 다시 전달하는 방법을 알아낼 수 없다는 것입니다. 최종 결과는 스타일이 올바르지 만 HTML입니다.

저는 빌드 도구로 Gradle을 사용하고 있습니다.

app/src/main 
    ../java      - contains the application code 
    ../resources     - contains the application resources 
     ../templates 
      ../app 
       start.html   - Thymeleaf template 
    ../webapp 
     ../resources 
      ../css 
       app.css    - Style sheet for use in the app 

여기에 응용 프로그램과 모두 사용하는 ViewResolver를 콩이야 :

가 여기에 예제 응용 프로그램 내 관련 프로젝트 구조의
common-framework/src/main 
    ../java      - contains the framework code 
    ../resources     - contains the framework resources 
     ../static 
      ../css 
       example.css   - Style sheet for use in templates 
     ../templates 
      ../fwk 
       example.html  - Thymeleaf template 

:

다음은 프레임 워크에 대한 내 관련 프로젝트 구조입니다 위치 템플릿을위한 프레임 워크 :

@Bean 
public ViewResolver viewResolver() 
{ 
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); 
    templateResolver.setTemplateMode("HTML5"); 
    templateResolver.setPrefix("templates/"); 
    templateResolver.setSuffix(".html"); 
    templateResolver.setCharacterEncoding("UTF-8"); 
    templateResolver.setOrder(1); 

    SpringTemplateEngine engine = new SpringTemplateEngine(); 
    engine.setTemplateResolver(templateResolver); 

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
    viewResolver.setTemplateEngine(engine); 
    viewResolver.setCharacterEncoding("UTF-8"); 
    viewResolver.setOrder(1); 

    return viewResolver; 
} 

여기 내 재입니다 (내 WebMvcConfigurerAdapter에서) 소스 핸들러 구성 : 여기

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) 
{ 
    registry.addResourceHandler("/resources/**") 
      .addResourceLocations("/resources/"); 
} 

프레임 워크 Thymeleaf 템플릿입니다 (FWK/example.html) 여기

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 

<head> 
    <meta charset="utf-8" /> 
    <title th:text="${title}"></title> 
    <link rel="stylesheet" type="text/css" th:href="@{/resources/static/css/framework.css}" /> <!-- *** PROBLEM IS HERE *** --> 
</head> 
<body> 
    ... 
</body> 
</html> 

응용 프로그램 Thymeleaf 템플릿입니다 (응용 프로그램/start.html)

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 

<head> 
    <meta charset="utf-8" /> 
    <title th:text="${title}"></title> 
    <link rel="stylesheet" type="text/css" th:href="@{/resources/css/app.css}" /> <!-- *** This works correctly. *** --> 
</head> 
<body> 
    ... 
</body> 
</html> 

다음은 웹 응용 프로그램의 샘플 컨트롤러입니다. 참고 : 이것은 프레임 워크 라이브러리에 의존하는 별도의 프로젝트입니다.

@Controller 
public class ExampleController 
{ 
    @GetMapping(path = "/start") 
    public String start(Model model, HttpServletRequest request) 
    { 
     ... 
     return "app/start"; // Uses an application template 
    } 

    @GetMapping(path = "/example") 
    public String example(Model model) 
    { 
     ... 
     return "fwk/example"; // Uses a framework template 
    } 
} 

웹 응용 프로그램은 Tomcat에서 실행되며 프레임 워크 라이브러리 JAR 파일을 사용할 수 있습니다. 컨트롤러 URL을 테스트 할 때 두 가지 모두 올바르게로드됩니다. 즉, 두 경우 모두 템플리트가 발견되고로드되고 표시됩니다.

그러나 위에 언급 한 것처럼 (/example URL로 트리거 된) 프레임 워크 템플릿은 해당 프레임 워크 CSS 파일이없고 브라우저로 전달되기 때문에 스타일이 다릅니다. 이것을 어떻게 성취합니까?

이 (내 프레임 워크 구조 괜찮습니까? 올바르게 구성 제의 ViewResolver인가? 내 자원이 제대로 구성 취급? 내가 같은 JAR에 스타일 시트를 참조하는 프레임 워크 템플릿에 무엇을 사용해야 href 경로?)

답변

0

에서 example.html 교체하려고 :

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

작성자 : 대답에 대한

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

감사합니다. 불행히도 이것은 효과가 없었습니다. "/app/css/framework.css"로 해석되어 404 오류가 발생했습니다. – dave