簡介
使用SpringBoot;
1)、創建SpringBoot應用,選中我們需要的模塊;
2)、SpringBoot已經默認將這些場景配置好了,只需要在配置文件中指定少量配置就可以運行起來 3)、自己編寫業務代碼;
1.擴展Spring MVC
編寫一個配置類(@Configuration),是WebMvcConfigurerAdapter類型;不能標註@EnableWebMvc;
既保留了所有的自動配置,也能用我們擴展的配置;
//使用WebMvcConfigurerAdapter可以來擴展SpringMVC的功能
@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) { // super.addViewControllers(registry);
//瀏覽器發送 /atguigu 請求來到 success
registry.addViewController("/atguigu").setViewName("success"); }
}
原理:
1)、WebMvcAutoConfiguration是SpringMVC的自動配置類
2)、在做其他自動配置時會導入;@Import(EnableWebMvcConfiguration.class)
@Configuration public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
//從容器中獲取所有的WebMvcConfigurer
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
//一個參考實現;將所有的WebMvcConfigurer相關配置都來一起調用;
@Override
// public void addViewControllers(ViewControllerRegistry registry) { // for (WebMvcConfigurer delegate : this.delegates) {
// delegate.addViewControllers(registry);
// }
}
}
}
3)、容器中所有的WebMvcConfigurer都會一起起作用;
4)、我們的配置類也會被調用;
效果:SpringMVC的自動配置和我們的擴展配置都會起作用;
2.全面接管SpringMVC
SpringBoot對SpringMVC的自動配置不需要了,所有都是我們自己配置;所有的SpringMVC的自動配置都失效了 我們需要在配置類中添加@EnableWebMvc即可;
//使用WebMvcConfigurerAdapter可以來擴展SpringMVC的功能
@EnableWebMvc
@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) { // super.addViewControllers(registry);
//瀏覽器發送 /atguigu 請求來到 success
registry.addViewController("/atguigu").setViewName("success"); }
}
原理:
爲什麼@EnableWebMvc自動配置就失效了;
1)@EnableWebMvc的核心
@Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc {
2)、
@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
3)、
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中沒有這個組件的時候,這個自動配置類才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
4)、@EnableWebMvc將WebMvcConfigurationSupport組件導入進來; 5)、導入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;
3.模板引擎
SpringBoot推薦的Thymeleaf;
語法更簡單,功能更強大;
1.引入thymeleaf
<properties>
<java.version>1.8</java.version>
<!--<thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version>-->
<thymeleaf-layout-dialect.version> 2.1.1 </thymeleaf-layout-dialect.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、Thymeleaf使用
只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染; 使用:
1、導入thymeleaf的名稱空間
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2、使用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>
3、語法規則
1)、th:text;改變當前元素裏面的文本內容;
th:任意html屬性;來替換原生屬性的值
2)、表達式
面向百度學習
4、如何修改SpringBoot的默認配置
模式:
1)、SpringBoot在自動配置很多組件的時候,先看容器中有沒有用戶自己配置的(@Bean、@Component)如 果有就用用戶配置的,如果沒有,才自動配置;如果有些組件可以有多個(ViewResolver)將用戶配置的和自己默 認的組合起來;
2)、在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴展配置
3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進行定製配置