SpringBoot模板引擎
模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。 SpringBoot 推荐使用模板引擎来渲染html,如果你不是历史遗留项目,一定不要使用JSP,常用的模板引擎很多,有freemark,thymeleaf等,其实都大同小异其中springboot 强烈推荐的是用thymeleaf
pom.xml文件中添加thymeleaf的支持,并且删除JSP的支持
<!--<!–JavaServer Pages Standard Tag Library,JSP标准标签库–>--> <!--<dependency>--> <!--<groupId>javax.servlet</groupId>--> <!--<artifactId>jstl</artifactId>--> <!--</dependency>--> <!--<!–内置tocat对Jsp支持的依赖,用于编译Jsp–>--> <!--<dependency>--> <!--<groupId>org.apache.tomcat.embed</groupId>--> <!--<artifactId>tomcat-embed-jasper</artifactId>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>在resources目录里面新建一个templates目录,在目录里面新建testThymeleaf.html文件(springboot约定大约配置,Springboot默认的模板配置路径为:src/main/resources/templates)
<!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>enjoy</title> </head> <body> <h1 th:text="${name}"/> </body> </html>在浏览器上输入:localhost:8080/tpl/testThymeleaf,可以看到页面。
