jar webapp
自动配置
springBoot帮我们配置了什么
xxxAUtoCofinguraion 向容器中自动配置组件
xxxProperties :自动配置类,转配配置文件中自定义的一些内容
要解决的问题
- 导入静态资源
- 首页
- jsp , 模板引擎
- 转配扩展SpringMVC
- 增删查改
- 拦截器
- 国际化
静态资源
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
ServletContext servletContext = getServletContext();
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (servletContext != null) {
registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION));
}
});
}
第一张配置方式
路径 "classpath:/META-INF/resources/webjars/"中 的webjars是什么?
网址:webjars的官网 在pom文件中配置后 (用maven的方式引入)启动服务器 可以访问到文件
备注:addResourceHandler(registry, “/webjars/**”, “classpath:/META-INF/resources/webjars/”); 表示:springboot已经帮我配置了在 项目下根目录下 META-INF/resources/webjars/ 去寻找文件
访问服务器:http://localhost:8080/webjars/jquery/3.6.0/jquery.js
在springboot中 ,我们可以使用以下方式处理静态资源
- webjars localhost:8080/webjars/
源码:其优先级也是 从前到后
public static class Resources {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" };
- public static /** resources localhost:8080/
Thymeleaf
结论:只要需要使用thymeleaf,只需要导入对应的依赖就可以了!
当前最新的版本
<!--Thymeleaf依赖开始 当前默认版本3.0.12-->
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-java8time -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<!--Thymeleaf依赖结束 当前默认版本3.0.12-->
基本注解
- @PathVariable 获取路径 (变量)参数里面的值
- @RequestHeader 获取请求头里面的参数
- @RequestParam 获取请求参数
- @CookieValue 获取cookie的值
- @RequestBody 获取post请求体
- @RequestAttribute 获取request域中的属性
return “forward: /success” //转发到 /success请求 重定向
- @MatrixVariable 矩形变量
1、语法: /cars/sell; Low=34; brand=byd, audi, yd
2、SpringBoot默认是禁用J矩阵变量的功能 手动开启:原理。对于路径的处理。UrLPathHelper进 行解 。removeSemicolonContent (移除分号内容)支持矩阵变量的
3、矩阵变量必须有url路径变量才能被解析
想使用它,还要开启:
第一种:
> 第二种:
测试矩阵变量: