繁体   English   中英

Thymeleaf +静态资源+ @ResponseBody

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM