使用Thymelaf运行SpringBoot应用程序时,如何解决此404错误



我有一个Spring Boot应用程序,它的pom文件中有thymelaf依赖项,resources/templates目录中有一个index.html(据我所知,这是thymelav查找html文件的地方(。

我的控制器看起来像这样:

@Controller
@SessionAttributes("game")
public class Controller {
@RequestMapping(value = "/start", method = RequestMethod.GET)
public String index( @ModelAttribute("model") Model model) {
return "index";
}

我已经验证了来自浏览器的请求会进入索引方法,但随后我得到了以下错误:

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Dec 10 20:45:21 CET 2020
There was an unexpected error (type=Not Found, status=404).
No message available

我还尝试修改application.properties中的thymelaf设置,但没有成功:

spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.suffix=.html

我尝试了mvn清理、构建和安装,结果一直是404。任何帮助都将不胜感激。

您确定已连接到控制器吗?当我尝试你的代码时,我得到了一个错误500,因为我没有向模板传递任何对象。试试这个,看看你是否在浏览器或IDE控制台中得到了什么:

@RequestMapping(value = "/start", method = RequestMethod.GET)
public String index(Model model) {
System.out.println("hello");
return "index";
}

更新到最新版本的SpringBoot修复了此问题。我猜我是在进口最新版本的thymelaf,它与过时的SpringBoot不兼容。

相关内容

最新更新