2014-04-26 153 views
2

我在使用thymeleaf並仍然提供靜態內容時出現sonfigure Spring Boot問題。我的資源目錄中有兩個目錄:「/ static」和「/ templates」。根據Spring Boot文檔,默認情況下,thymeleaf應該在templates目錄中找到thymeleaf模板。當我嘗試使用模板時,我得到了404。從我的控制器使用Spring Boot與thymeleaf模板一起提供靜態內容

相關代碼:

​​

的index.html是在資源/靜態的test.html是資源/模板。

index.html工作正常,但如果您嘗試在您的瀏覽器中打開/測試,則會拋出404說不能找到thymeleaf模板。

我真的很感激任何幫助。我很難過。

+1

請張貼您的thymeleaf配置。 – Vaelyr

+0

您能否請您發佈您的Spring Boot配置? – geoand

回答

1

任何未經過ThymeleafViewResolver(your/resources/static)的頁面都需要刪除。

/** 
* Configures a {@link ThymeleafViewResolver} 
* 
* @return the configured {@code ThymeleafViewResolver} 
*/ 
@Bean 
public ThymeleafViewResolver thymeleafViewResolver() 
{ 
    String[] excludedViews = new String[]{ 
     "/resources/static/*"}; 

    ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 
    resolver.setTemplateEngine(templateEngine()); 
    resolver.setOrder(1); 
    /* 
    * This is how we get around Thymeleaf view resolvers throwing an error instead of returning 
    * of null and allowing the next view resolver in the {@see 
    * DispatcherServlet#resolveViewName(String, Map<String, Object>, Locale, 
    * HttpServletRequest)} to resolve the view. 
    */ 
    resolver.setExcludedViewNames(excludedViews); 
    return resolver; 
} 
0

您是否嘗試過僅返回模板的字符串名稱而不是ModelAndview? 並且您的ModelAttributes註釋如documentation中所示?

相關問題