日常积累,待更
使用SpringBoot时推荐使用thymeleaf+html
访问页面时报下面的异常:
org.thymeleaf.exceptions.TemplateInputException: Error resolving
template “index”, template might not exist or might not be accessible
by any of the configured Template Resolvers
1,导入pom.xml添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2,application.properties中加入配置
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
注意第一行最后的斜杠不能掉,此例中suffix后缀名为html,并不是jsp。
3,将@RestController换成@Controller,使用@RestController如果想直接通过return字符串,页面名跳界面时,返回的是具体字符串而不会进行页面跳转。
@Controller
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/hello")
public String hello() {
System.out.println("-----------");
return "index";
}
}