之前学习Spring-boot的时候,跟着csdn中的案例写了demo,结果报错了,就是以上的错误,现在把代码贴出来说下解决办法。
首先是Controller代码:
package com.Sheffy.demo.Controller;
import com.Sheffy.demo.Entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/{id}")
public String getUser(@PathVariable int id,Model model){
model.addAttribute("user",new User(id,"ysh",13,"fj"));
return "/user/detail";
}
@RequestMapping(value = "/listUser")
public String listUser(Model model){
List<User> listUser=new ArrayList<User>();
for(int i=0;i<10;i++){
listUser.add(new User(i,"ysh",18+i,"fj"));
}
model.addAttribute("users",listUser);
return "/list";
}
}
然后是代码结构:
html文件是放在templates的目录下的
properties文件中没有任何配置,之后运行,localhost:8080/user/listUser就报错了。
接下来说下解决办法,找了很久都没有找到,最后是同事帮忙解决的。
1、在properties文件中加入
# 模板配置
# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false
# 这个是配置模板路径的,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
# 这个可以不配置,检查模板位置
spring.thymeleaf.check-template-location=true
# 下面3个不做解释了,可以不配置
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 模板的模式
spring.thymeleaf.mode=HTML5
这个配置文件很奇怪,没加之前报错,加了运行正常之后,把配置文件注释掉还是运行正常。。
我的问题的解决办法就是这样。