Spring Boot03 Web
要解决的问题
- 导入静态资源
- 设置首页
- jsp,模板引擎 thymeleaf
- 装配拓展 SpringMVC
- 增删改查
- 拦截器
- 国际化
导入静态资源
从spring boot 的 web配置文件开始看
打开 WebMvcAotoConfiguration
这个类 找到 **addResourceHandlers ( ResourceHandlerRegistry registry )
**这个方法
添加资源控制器
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
//重点看这个webjars
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
什么是 webjars
上面的代码第10行 先判断是否为webjars
目录下的静态资源
否则就是通过WebMvcAotoConfiguration
这个类的EnableConfigurationProperties
这个注解,在注解的属性中找到一个resourceProperties
的类 点进去
可以看到 他默认支持的几个路径
添加资源控制器 的 代码第15行 getStaticPathPattern()
这个方法 直接获取静态资源的位置 点进去之后
在**WebProperties
**类中 他返回了一个属性 静态资源的父路径
这个属性的初始值 是/**
也就是说 在 resourceProperties
类中的 CLASSPATH_RESOURCE_LOCATIONS
这个属性中的 每个路径下的 /**都可以放静态资源
也就是这些路径
classpath:/META-INF/resource/
, classpath:/resources/
, classpath:/static/
, classpath:/public/
总结
-
在spring boot 可以使用以下方式处理静态资源
- webjars localhost:8080/webjars/***
- classpath: {public,static,resources,/**} localhost:8080/…
-
优先级
META-INF/resource/
>classpath:/resources/
>classpath:/static/
>classpath:/public/
首页的定制
源码
还是 WebMvcAotoConfiguration
这个类
最后得到的是 location+"index.html"
,意味着,默认就是 index.html
,只要创建 文件名 为 index.html
的文件 放入 静态资源目录就好了
放入public static resources
的优先级不同 resource
>public
>static
thymeleaf模板引擎
模板引擎的作用就是用来写一个页面模板,可以实现后台数据在页面获取
使用 thymeleaf ,只需要导入依赖,注意spring boot 或者 需要导入插件的版本
th标签库引入
<!-- 在html 标签中 加入如下属性 -->
xmlns:th="http://www.thymeleaf.org"
常用的语法
thymeleaf模板实现 页面中代码 的复用
可以让一个页面 引用另一个 页面中的 标签节点内容,从而避免大量重复代码,比如重复的 top,left,bottom
- 给被引用的页面的标签节点 进行如下操作
th:freagment=" 引用名( 参数) "
定义一个要使用的模板块
<head th:fragment="homeHead(title)">
<meta charset="UTF-8">
<link rel="stylesheet" th:href="@{/css/my.css}">
<!--...-->
<!--title 可传入参数-->
<title th:text="${title}"></title>
<script>
//...
</script>
</head>
- 给引用的页面的相同的标签节点 进行如下操作
th:replace=“${}”
使用自定义的freagment
标签节点 代替这个标签
<head th:replace="~{admin/_fragments :: homeHead(管理首页)}">
</head>
这样可以避免 很多 link 或 script的重复
装配扩展MVC
在 springBoot 中 有一种 Configuration
的类 就是各个组件的配置类 可以继承这个类 对这个类进行拓展
用 视图解析器 (ViewResolver )做测试
自定义一个MVC的类配置类
-
建一个config包 在包下建一个类
MyMvcConfig
-
加上
@Configuration
这个注解 实现WebMvcConfiguration
接口 -
在这个类里面写 内部类 一个视图解析器的类 继承
ViewResolver
接口 -
在
MyMvcConfig
里面 写一个@bean
的方法getMyViewResolver()
获取视图解析器 -
这样 视图解析器 就用的是自己的配置的 而不是默认的
如果要自定义其他的一些定制化的配置,需要写这个组件的实现类,在类中实现自己的需求 就好了
可以配置的属性都在 WebMvcConfigurer
接口中
再自定义配置一个 addViewControllers(ViewControllerRegistry registry)
(添加视图控制器 )
- 在
MyMvcConfig
里面 重写接口的addViewController
方法,这个方法可以设置请求跳转的页面
第一个参数是 获取到的请求 ,第二个参数是 需要跳转到的页面
/*
@阿猫阿狗
2020/6/27 18:29
周六
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
/**
* 这个方法可以更改请求
* 再自定义配置一个 addViewController(添加视图控制器 )
* * 在 MyMvcConfig 里面 重写接口的 addViewController 方法
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// home请求 去找 index页面
registry.addViewController("home").setViewName("index");
}
//用视图解析器做引子
//这是 视图解析器
@Bean
public ViewResolver getMyViewResolver(){
return new MyViewResolver();
}
public static class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}