[英]Thymeleaf + static resource + @ResponseBody
在Spring Boot中,我得到了有史以來最簡單的控制器,返回了我的視圖名稱:
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "helloworld.html";
}
}
我的helloworld.html
文件位於resources/static
目錄中:
<html>
<body>
Hello world!
</body>
</html>
而且效果很好-鍵入localhost:8080/hello
“ Hello world!”。 在瀏覽器中。
然后在瀏覽器中鍵入相同的地址時,將Thymeleaf依賴項添加到pom.xml
,重新運行應用程序並獲取TemplateInputException
。 我猜這很好,因為現在默認情況下會在resources/templates
目錄中搜索resources/templates
。
我發現很奇怪,是在我向控制器方法添加@ReponseBody
注釋時:
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "helloworld.html";
}
}
一切恢復正常,我得到“ Hello world!” 在瀏覽器中。 但據我所知, @ResponseBody
批注是將返回的值作為響應的正文。 那么,為什么奇跡般地導致可以再次找到視圖文件呢?
我測試了您的代碼,但沒有將百里香和html放在靜態下作為響應“ Hello World”。 但是我得到了打印的字符串
帶注釋的@ResponseBody的“ helloworld.html”
當您的控制器方法返回一個String時,在您的情況下該字符串是誤導性的“ helloworld.html”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.