文章目录
1. 模板引擎
引入thymeleaf依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.1. 模板引擎存放位置
首先找到ThymeleafAutoConfiguration这个类(ctrl+n搜索):
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {
public ThymeleafAutoConfiguration() {
}
看注解@EnableConfigurationProperties进入xxxProperties类:
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
//只要把html页面放在classpath:/templates里面,thymeleaf就能帮我们自动渲染了
private String prefix = "classpath:/templates/";
private String suffix = ".html";
有点像SpringMvc的视图解析器功能:(在SpringMvc中,如果将html页面放在WEB-INF文件夹下面,那么是无法直接通过浏览器输入URL进行访问的,因此就需要使用视图解析器来配置前缀和后缀,从而进行访问html页面)
@Controller
public class HelloWorld {
//注意不能使用@ResponseBody注解,否则会原样返回字符串success
@RequestMapping("/success")
public String success(){
//classpath:/templates/success.html
return "success";
}
}
Spring Boot存放模板页面的路径在src/main/resources/templates下面,模板后缀名为html
1.2. Thymeleaf的使用
(1)导入thymeleaf的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
(2)创建一个Controller
@Controller
public class HelloWorld {
@RequestMapping("/success")
public String success(Map<String,Object> modelMap){
modelMap.put("hello","你好");
return "success";
}
}
(3)使用thymeleaf语法;
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容设置为 -->
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>
1.3. 语法规则
写一个Controller控制器,通过modelMap里面向域中存放对象,然后通过themleaf语法从中取对象
@Controller
public class HelloWorld {
@RequestMapping("/success")
public String success(Map<String,Object> modelMap){
modelMap.put("hello","<h1>你好</h1>");
modelMap.put("users", Arrays.asList("zhangsan","lisi","wangwu"));
return "success";
}
}
success.html模板引擎页面:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容设置为 -->
<div th:text="${hello}">这是显示欢迎信息</div>
<hr/>
<!--不会转义标签体-->
<div th:text="${hello}"></div>
<!--转义标签体-->
<p th:utext="${hello}"></p>
<!--每次遍历都会生成h2标签,然后将文本信息放入这个标签中-->
<hr/>
<h2 th:text="${user}" th:each="user:${users}"></h2>
<!--将遍历的内容放在h4标签中,然后放在span标签中-->
<!--注释不要加在一个没有写完额标签中,报错-->
<hr/>
<h4>
<span th:text="${user}" th:each="user:${users}">[[${user}]]</span>
</h4>
</body>
</html>
2. SpringMVC自动配置
2.1 Spring MVC auto-configuration
2.2 扩展SpringMVC
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;
既保留了所有的自动配置,也能用我们扩展的配置;
//WebMvcConfigurerAdapter扩展SpringMvc的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry){
//浏览器发送/chahua请求,来到resources/templates/success.html页面
registry.addViewController("/chahua").setViewName("success");
}
}
原理:
1)、WebMvcAutoConfiguration是SpringMVC的自动配置类
2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)
3)、容器中所有的WebMvcConfigurer都会一起起作用;
4)、我们的配置类也会被调用;
效果:SpringMVC的自动配置和我们的扩展配置都会起作用;
2.3 全面接管SpringMVC;
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了
我们需要在配置类中添加@EnableWebMvc即可;
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry){
//浏览器发送/chahua请求,来到resources/templates/success.html页面
registry.addViewController("/chahua").setViewName("success");
}
}
3. 如何修改SpringBoot的默认配置
模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置