2016-12-25 45 views
0

我想要按照本教程http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html瞭解如何發送對Thymeleaf模板的響應。但我得到這個錯誤:無法找到模板位置:classpath:/模板/(請添加一些模板或檢查您的Thymeleaf配置)如何從彈簧啓動中找到Thymeleaf模板

我把message.html文件放在其他源目錄和src/main/resources下<default package>

所以結構如下:

SomeProject - 其他來源 --src /主/資源 --- <default package> ---- message.html

我想知道爲什麼會出現根據<default package>而不是<template>?這可能是問題嗎?如果是這樣,我該如何改變它?我正在使用netbeans和maven。請有任何想法嗎?這是我的依賴在我的pom.xml:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-devtools</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
    </dependency>    
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
    </dependency> 

在控制器我有

@RequestMapping(value = "message", method = RequestMethod.GET) 
    public String messages(Model model) { 
    model.addAttribute("messages", messageRepository.findAll()); 
    return "message"; 
} 

並在視圖:

<ul th:each="message : ${messages}"> 
    <li th:text="${message.id}">1</li> 
    <li><a href="#" th:text="${message.title}">Title ...</a></li> 
    <li th:text="${message.text}">Text ...</li> 
</ul> 

回答

0

2這邊的事情: 1。如果您正在使用Maven,並且我假設沒有對文件夾名稱進行自定義。然後文件夾名稱應該是src而不是source。 2.文件夾重命名後,將您的模板移動到src/resources中的「模板」文件夾中,這應該可以正常運行。

+0

@HhuShu Bhardwaj我在這個問題上犯了一個錯字。在NetBeans中,文件夾結構顯示如下src/main/resources,它位於NetBeans項目層次結構中其他資源目錄下。我會解決問題中的錯字。謝謝你的幫助。現在,也許你可以進一步反饋? – Gustav

+0

我已經給了2分。您的模板是否位於src/main/resources下的模板目錄中? –

+0

@ Himanshu我在src/main/resources下創建了模板目錄,並將html文件放在那裏,現在問題已解決。感謝您的支持。我會將問題標記爲已回答。 – Gustav

2

Spring Boot包含對thymeleaf模板引擎的自動配置支持,您的模板將從src/main/resources/templates自動獲取。

如果您正在自定義模板位置,請使用Spring Boot下面的thymeleaf屬性配置。

  • spring.thymeleaf.check-template = true#在呈現模板之前檢查模板是否存在。

  • spring.thymeleaf.check-template-location = true#檢查模板位置是否存在。

  • spring.thymeleaf.enabled = true#啓用MVC Thymeleaf視圖分辨率。

  • spring.thymeleaf.prefix = classpath:/ templates /#構建URL時預先查看名稱的前綴。

  • spring.thymeleaf.suffix = .html#在構建URL時被附加到視圖名稱的後綴。

+0

從哪裏我應該找到這個配置文件?我正在使用maven,當我嘗試從依賴列表中更改一些時,它不允許我在那裏更改任何內容。有些代碼甚至被隱藏起來。我應該手動下載我想要更改配置的依賴關係嗎? – Gustav

+0

在你的類路徑中添加一個application.properties文件並放置所有的屬性。 SRC /主/資源/ application.properties – Chandu

0

的默認目錄位置thymeleaf模板是:

src/main/resources/templates 

其他路徑是春天引導標準約定。

Spring Boot thymeleaf directory structure