Java 百里香叶+;静态资源&x2B@应答器
在Spring Boot中,我得到了有史以来最简单的控制器,返回了我的视图名称:Java 百里香叶+;静态资源&x2B@应答器,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,在Spring Boot中,我得到了有史以来最简单的控制器,返回了我的视图名称: @Controller public class HelloController { @RequestMapping("/hello") public String hello() { return "helloworld.html"; } } 我的helloworld.html文件位于resources/static目录中: <html> <body&
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "helloworld.html";
}
}
我的helloworld.html
文件位于resources/static
目录中:
<html>
<body>
Hello world!
</body>
</html>
一切都恢复正常,我在浏览器中看到“Hello world!”。但我知道,
@ResponseBody
注释将返回值作为响应的主体。那么,为什么它奇迹般地导致视图文件再次被找到呢?我测试了您的代码,但我没有得到“helloworld”作为响应,而thymeleaf和html则放在static下。但是我被打印了字符串
“helloworld.html”,带有注释@ResponseBody
控制器方法返回一个字符串,在您的例子中,该字符串是误导性的“helloworld.html”可能您没有重建项目?因为它绝对应该返回
helloworld.html
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "helloworld.html";
}
}