持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情
1. 首页定制
在SpringBoot源码中定义了index.html可以默认访问,源码剖析中index.html只能放置在public、static、resources文件夹下,可以直接访问,所以在项目中,一般都会把首页放置在这三个文件夹中;
在templates文件夹下也可以放置index.html页面,但是templates文件夹下的页面只能通过controller层才能访问,大多数注重安全的项目则会将index.html页面放置在template文件夹下
在静态目录下找一个名字为index.html的文件就会被映射到首页。
2. thymeleaf模板引擎
导入thymeleaf的依赖,具体地址为:
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
</dependencies>
thymeleaf的头文件:<xmlns:th="http://www.thymeleaf.org">
thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP, Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用
SpringBoot推荐使用Thymeleaf、Freemarker等现代的模板引擎技术;一旦导入相 关依赖,会自动配置ThymeleafAutoConfiguration、FreeMarkerAutoConfiguration。
3. SpringMVC的相关知识点
MVC是一种设计模式,在这种模式下软件被分为三层,即Model(模型)、View(视图)、Controller(控制器)。Model代表的是数据,View代表的是用户界面,Controller代表的是数据的处理逻辑,它是Model和View这两层的桥梁。
注意: Spring Boot 自动配置好了SpringMVC
以下为日期格式化的书写:
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
}
自己添加的格式化器转换器,我们只需要放在容器中即可
Support for HttpMessageConverters (see below).
-
HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User—Json
-
HttpMessageConverters 是从容器中确定;获取所有的HttpMessageConverter