spring中配置thymeleaf以及遇到的问题
1.在pom.xml文件中添加thymeleaf模板引擎
org.springframework.boot spring-boot-starter-thymeleaf2.在application.properties中进行配置
#thymeleaf
#该路径根据自己项目进行指定
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
3.将相对应的html文件放到对应的文件夹中
4.在controller中写入对应的跳转
@Controller
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/to_login")
public String to_login(){
return "login";
}
}
5.遇到的问题
如果出现上述问题记得检查自己的代码,本人就是因为在第一步中忘记添加依赖导致花费了半个小时找问题。
还有一点就是记得做好文件的管理:
需要将主控制文件放在对应的文件夹下,springboot才能自动扫描,否则需要:
@SpringBootApplication(scanBasePackages = "") //此处添加要扫面的controller
public class MainApplication {
public static void main(String[] args) throws Exception{
SpringApplication.run(MainApplication.class,args);
}
}