Java大联盟
致力于最高效的Java学习
今天继续带来SpringBoot专题教程:静态化。
thymeleaf模板
SpringBoot中默认使用thymeleaf模板引擎整合html。
SpringBoot不建议使用jsp。
html路径默认为
src/main/resources/templates/*.html,默认是从templates文件夹里找html资源。
1.pom.xml中配置thymeleaf的依赖:
2. resources下创建templates文件夹,并新建tem.html:
注意:IDEA新建html,默认是不会补齐后面这个,我们必须手动补齐,否则页面会报错:
控制台会报错:
3. Controller:
4. 打开浏览器,输入http://localhost:8080/tem/thymleaf
5. 更改默认路径:
(1)resources文件夹中新建application.properties:
(2)重新运行:
freemarker模板
1. pom.xml:
2. *.ftl文件默认也是在
src/main/resources/templates/*.ftl中,前缀默认在templates文件夹中寻找资源,新建hello.ftl:
3. Controller:
4. 打开浏览器,输入http://localhost:8080/templates/freemarker
5. 更改默认路径application.properties:
6. 存在的问题:
网上有资料说freemarker的访问路径是可以更改的,实际经过测试是不能更改的。
配置了spring.freemarker.prefix=classpath:/tem/ 就会报错。
springboot中整合jsp
1.创建maven web project,项目结构如图所示:
2. pom.xml:
3.application.properties:
4.Controller:
5. 项目结构:
6. 打开浏览器,输入http://localhost:8080/hello
访问成功。
源码:
github
SpringBoot使用thymleaf和freemarker:
https://github.com/southwind9801/springboot_01.git
SpringBoot整合JSP:
https://github.com/southwind9801/springboot_jsp.git
专业 热爱 专注
致力于最高效的Java学习
Java大联盟
领取专属 10元无门槛券
私享最新 技术干货