1、创建springboot应用,选中我们需要的模块
2、springboot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来
3、自动编写业务代码;
自动配置原理?
xxxxAutoConfiguraion:帮我们给容器中自动配置组件
xxxxProperties:配置类来封装配置文件的内容;
2、spring boot对静态资源的映射规则;
1)所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源;
webjars:以jar包的方式引入静态资源;
2)“/**”访问当前项目的任何资源,(静态资源的文件夹)
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
"/":当前项目的根路径
3)欢迎页;静态资源文件夹下的所有index.html页面;
localhost:8080/ 找index页面
4)所有的**/tavicon。ico都是在静态资源文件下找;
3、模板引擎
jsp、velocity、Freemarker、Thymeleaf;
springboot推荐的thymeleaf;
语法简单,功能强大;
1、引入thymeleaf
2、使用thymeleaf语法
3、语法规则
1)th:text:改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值
4、扩展springMVC
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型,不能标注@EnableWebMvc;
既保留了所有的自动配置,也能用我们扩展的配置;
@Configuration
public class MyMvcConfig extend WebMvcConfigurerAdapter{
@Overide
public void addViewControllers(ViewControllerRegistry registry){
//浏览器发送/demo 请求来到success
registry.addViewController("demo").setViewName("success");
}
}
1、WebMvcAutoConfiguration是SpringMVC的自动配置类
2、在做其他自动配置时会导入;@import(EnableWebMvcConfiguration.class)
3、容器中所有的WebMvcConfigurer都会一起起作用;
4、我们的配置类会被自动配置类调用;
3、全面接管SpringMVC;
springboot对springMVC的自动配置不需要了, 所有都是我们自己配置;所有的自动配置都失效了
我们需要在配置类中添加@EnableWebMvc
5、如何修改springboot的默认配置
模式:
1)springboot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)在SpringBoot中会有很多的xxxConfigurer帮助我们及逆行扩展配置
6、RestfulCRUD
1)默认访问首页
2)国际化
步骤:
1)编写国际化配置文件,抽取页面需要显示的国际化信息
2)springboot自动配置好了管理国际化资源文件的组件;
3)去页面获取国际化的值
原理:国际化(Locale(区域信息对象)),
thymeleaf公共页面元素抽取
1、抽取公共片段
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名
3、默认效果:
insert的公共片段在div标签中 如果使用th:insert等属性进行引入,可以不用写~{}:
行内写法可以加上:[[~{}]];[(~{})];
三种引入公共片段的th属性:
th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中
<footer th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</footer>
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
效果
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
引入片段的时候传入参数:
<nav class="col-md-2 d-none d-md-block bg-light sidebar" id="sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active"
th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}"
href="#" th:href="@{/main.html}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
<polyline points="9 22 9 12 15 12 15 22"></polyline>
</svg>
Dashboard <span class="sr-only">(current)</span>
</a>
</li>
<!--引入侧边栏;传入参数-->
<div th:replace="commons/bar::#sidebar(activeUri='emps')"></div>