1、虽然现在很多开发都采用了前后端完全分离的模式,即后端只提供数据接口,前端用ajax调用;但这种模式不利于seo,有些场景使用模板引擎会更方便。
2、SpringBoot与模板引擎Thymeleaf
引入jar包
<
artifactId
>spring-boot-starter-thymeleaf</
artifactId
>
配置
spring:
thymeleaf:
prefix: classpath:/templates/ //
指定模板所在的目录,默认 就是
classpath:/templates/
check-template-location: true //检查模板路径是否存在,默认true
cache: false //
是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5
3、SpringBoot工程下的目录static和template
默认static下放静态页面。template下放动态页面
4、静态页面
比如在static下放login.html,可通过http://localhost:80/login.html访问到;
@Controller
public class HelloController {
@RequestMapping("/Hi")
public String sayHello() {
return "hello.html";
}
}
5、动态页面
需要先请求服务器,访问后台应用程序,然后再转向页面(需要配置静态模板)
静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html,注意看两者return代码也有区别,动态没有html后缀。
@Controller
public class HelloController {
@RequestMappin