Spring Boot 学习笔记(四)——Web开发之Thymeleaf
项目结构
代码的部分不说了;
resources:
static下存放静态资源,templates下存放默认的模板配置路径?
Spring Web MVC框架
很迷,好像后续章节有详解。
静态文件
默认下Spring Boot从resources下的static或ServletContext根目录找静态资源:
/static
/public
/resources
/META-INF/resources
/templates
本质上是Spring MVC的ResourceHttpRequestHandler,所以可以继承?实现WebMvcConfigurerAdapter,然后重写addResourceHandlers方法改变静态资源路径。
还有打jar包的时候,src/main/webapp文件夹会被忽略,所以里面的资源文件就木有了。war包则可以。什么webjar?
模板引擎
FreeMarker
Groovy
Thymeleaf(官方推荐)
Mustache
官方不推荐使用jsp,反正我就不用了呗。
以上模板的默认路径都是:src/main/resources/templates
可以在模板引擎的配置属性中查询并修改这个路径。
Thymeleaf模板引擎
用于渲染XML/XHTML/HTML5。优势:不用启动整个web应用就可以显示模板页面。与Spring MVC的web框架集成作为web应用的引擎特别方便。
Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
@Controller @RequestMapping("/learn") public class LearnResourceController { @RequestMapping("") public ModelAndView index(){ List<LearnResouce> learnList =new ArrayList<LearnResouce>(); LearnResouce bean =new LearnResouce("官方参考文档","Spring Boot learnList.add(bean); ModelAndView modelAndView = new ModelAndView("/index");//直接返回模板名字 modelAndView.addObject("learnList", learnList); return modelAndView; }
并且spring-boot-starter-thymeleaf就包含了spring-boot-starter-web。所以pom.xml中的就不用引入web依赖了。
下面这段就很牛逼了,行话叫:Thymeleaf做到了不破坏HTML自身内容的数据逻辑分离。
<tr th:each="learn : ${learnList}"> <td th:text="${learn.author}">嘟嘟MD</td> <td th:text="${learn.title}">SPringBoot干货系列</td> <td><a th:href="${learn.url}" target="_blank">点我</a></td> </tr>
参考文献:[1]: http://tengj.top/2017/03/13/springboot4/
阅读更多- Spring Boot 学习笔记【6】使用FreeMarker与BootStrap开发Web
- Spring boot入门实例 简易登录(精通Spring+4.x++企业应用开发实战 学习笔记二)
- SpringBoot学习笔记 - web开发(Thymeleaf的应用)
- springboot学习笔记(二) Thymeleaf
- SpringBoot--使用Thymeleaf模板开发web项目
- Spring Boot基础7-web应用开发-模板引擎Thymeleaf
- spring boot + Thymeleaf开发web项目 视图解析
- SpringBoot(四) Web开发 --- Thymeleaf、JSP
- springBoot(5):web开发-模板引擎FreeMarker与thymeleaf
- spring boot 学习笔记(二) 构建web支持jsp
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
- Spring Boot 学习笔记(二)—— WEB相关配置
- 【学习笔记】spring boot + zookeeper + dubbo + rabbitMq + mysql + thymeleaf/freemarker + mybaits 代码试验
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- Spring Boot 系列 - web开发 Thymeleaf、FreeMarker模板引擎
- Spring boot 学习笔记 ---分分钟构建一个web程序(一)
- Spring Boot进阶之Web进阶 学习笔记
- Spring Boot学习(四)之web开发渲染页面 -- Velocity
- Spring Boot---(16)Spring Boot使用Thymeleaf开发web页面
- Spring Boot 学习笔记 4 : Thymeleaf