3、模板引擎
市面上最常见:JSP、Velocity、Freemarker、Thymeleaf

SpringBoot推荐的Thymeleaf;
语法更简单,功能更强大;
1、引入thymeleaf;
在官方文档 III. Using Spring Boot —> 13.5. Starters
找到:
spring-boot-starter-thymeleaf
Starter for building MVC web applications using Thymeleaf views
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2.1.6
</dependency>
切换thymeleaf版本
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
在github中可以搜索到最新的版本
这有个问题: 为什么SpringBoot要放弃jsp ?
springboot支持jsp,只是不推荐使用jsp,
官方文档:
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
运行使用嵌入式servlet容器的Spring Boot应用程序(并打包为可执行存档)时,JSP支持存在一些限制。
使用Jetty和Tomcat,如果使用war包装,它应该可以工作。可执行的战争在启动时将起作用java -jar,并且也可以部署到任何标准容器。使用可执行jar时不支持JSP。
Undertow不支持JSP。
springboot 是内嵌web容器的,推荐打成jar包不是war包
如果想使用jsp也可以啊,自建WEB-INF/web.xml 然后像往常一样使用jsp
打成war包使用外部容器即可,这就相当于失去了一些springboot的特性了
至于从技术层面的开发使用上 用 JSP当模板 和 用其他模板引擎支持的文件没有任何本质的区别。差别仅存在于开发书写效率和程序解析运行效率。比如 jsp 和 freemarker 的解析执行效率都是很高的,而thymeleaf的书写效率高并且与前端模板语法很相似,学习成本很低。应该是考虑前端开发人员感受
本文探讨了SpringBoot推荐使用Thymeleaf作为模板引擎的原因,指出Thymeleaf语法简单且功能强大。SpringBoot虽然支持JSP,但官方建议使用jar包而非war包形式,因为内嵌的web容器不鼓励使用jsp,这可能会失去一些SpringBoot的特性。Thymeleaf的书写效率高,对于前端开发者来说学习成本较低,而JSP和Freemarker则在解析执行效率上有优势。
1246

被折叠的 条评论
为什么被折叠?



