在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!“在浏览器中。
然后我将百里叶依赖添加到pom.xml中,当在浏览器中输入相同的地址时,重新运行应用程序并获得TemplateInputException。我想这很好,因为现在默认情况下在resources/templates目录中搜索资源。
我发现奇怪的是,当我向控制器方法添加@ReponseBody注释时:
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "helloworld.html";
}
}一切都正常了,我得到的是"Hello world!“在浏览器中。但据我所知,@ResponseBody注释是将返回值作为响应的主体。那么,为什么会奇迹般地导致视图文件能够再次被找到呢?
发布于 2018-08-23 14:33:34
我测试了你的代码,我没有得到"Hello World“作为响应,胸腺叶和html放置在静态下。但是我把字符串打印出来了
带有注释的@ResponseBody的"helloworld.html“
因为您的控制器方法返回一个字符串,在本例中,该字符串是误导性的"helloworld.html“
https://stackoverflow.com/questions/51979184
复制相似问题