本章介绍了Maven的相关技术点,对于Maven的作用、特性、如何搭建本地Maven环境、如何配置IDEA Maven环境进行了一 一说明,同时对于thymeleaf模板引擎进行了详细的说明和讲解,提供了SpringBoot集成thymeleaf模板的操作演示,最后对于SpringBoot项目中如何引入静态资源进行了详细的描述和说明。
下载Maven(3.X)
解压到指定目录
配置环境变量
配置“阿里云”镜像地址
测试

注意:需要基于Idea全局配置
选择settings,进入全局配置窗口

选择"Maven",依次选择Maven主目录、配置文件位置、本地仓库目录

Thymeleaf在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以thymeleaf的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
Thymeleaf开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
Thymeleaf提供spring标准方言和一个与SpringMVC完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
在pom.xml文件引入thymeleaf
在application.yml文件中配置thymeleaf
- # thymeleaf
- spring.thymeleaf.prefix=classpath:/templates/
- spring.thymeleaf.check-template-location=true
- spring.thymeleaf.suffix=.html
- spring.thymeleaf.encoding=UTF-8
- spring.thymeleaf.content-type=text/html
- spring.thymeleaf.mode=HTML5
- spring.thymeleaf.cache=false
编写控制层HelloController.java
@Controller public class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request,String id) { request.setAttribute("id", id); return "hello"; } }
新建编辑模板文件
启动服务,测试
编写实体类UserInfo.java
public class UserInfo { private int userId; private String userName; public UserInfo() { } public UserInfo(int userId, String userName) { this.userId = userId; this.userName = userName; } public int getUserId() { return userId; } public String getUserName() { return userName; } }
编写控制层HelloController.java
@Controller public class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request, String id) { //存储编号 request.setAttribute("id", id); //存储单个对象 request.setAttribute("user",new UserInfo(1001,"Jain")); //存储用户对象集合 List
编写模板页面hello.html
启动服务,测试

开始实验
在SpringBoot默认静态资源访问路径下添加名为index.html文件,那么访问localhost:8080,会自动跳转到这个index.html

static目录下创建js文件夹
在index.html引入jQuery.js,并编程实现
static目录下创建css文件夹
.myred{ color:red; font-size: 30px; }
在index.html引入样式
项目的首页index.html
