2016-08-29 7 views
1

Spark Java 프레임 워크에 익숙하며 Freemarker 템플릿 엔진을 사용하여 HTML 파일을 렌더링하려고합니다. 하지만 내 CSS 및 JS 파일을 렌더링 할 수 없습니다.Spark Java Template Engine을 사용하여 css 및 js 파일을 렌더링 할 수 없습니다.

public static void main(String[] args) { 

     BasicConfigurator.configure(); 

     staticFileLocation("/css"); 
     staticFileLocation("/Content/Images"); 
     staticFileLocation("/fonts"); 
     staticFileLocation("/js"); 

     final Configuration configuration = new Configuration(); 
     configuration.setClassForTemplateLoading(Spark.class, "/"); 

     get("/searchview", (request, response) -> { 

      StringWriter writer = new StringWriter(); 

      try { 
       Template searchTemplate = configuration.getTemplate("templates/search.ftl"); 

       searchTemplate.process(null, writer); 
      } catch (Exception e) { 
       halt(500); 
      } 

      return writer; 
     }); 
} 

을 내가 URL을 입력 할 때 현재 : 로컬 호스트 : 4567/searchview을 내 HTML 페이지가 렌더링됩니다 다음과 같이

./pom.xml 
./src/main/java/Spark.java 
./src/main/resources/css/bootstrap.css 
./src/main/resources/js/bootstrap.min.js 
./src/main/resources/js/app.js 
./src/main/resources/templates/search.ftl 

주요 방법은 다음과 같이

내 프로젝트 구조입니다 하지만 CSS와 JS는 그렇지 않습니다. 내 콘솔에서

말한다 그것을 : 정보 spark.http.matching.MatcherFilter - 요청 된 경로 [/css/bootstrap.min.css] 내가 놓친 게 무엇

스파크

에 매핑되지?

답변

0

이후의 staticFileLocation 호출은 이전에 설정 한 정적 파일 위치보다 우선합니다. src/main/resources 아래에 public이라는 폴더를 만든 다음 css, fonts, js 및 기타 정적 폴더를 그 아래에 배치합니다. 마지막으로 정적 파일 위치를 staticFileLocation("/public")으로 설정하십시오.