使用springboot做Web開發

一.Web開發
使用springboot;
1.創建springboot應用,選中我們需要的模塊
2.springboot已經默認將這些場景配置好了,只需要在配置文件中指定少量配置就可以運行起來
3.自己編寫業務代碼;
自動配置原理?
這個場景springboot幫我們配置了什麼?能不能修改哪些配置?能不能擴展?xxxx
xxxxAutoconfiguration:幫我們給容器自動配置屬性
xxxxproperties:配置類來封裝配置文件的內容

所有/webjars/**,都去classpath:/META-INF/resources/webjars/找資源:
webjars:以jar包的方式引入靜態資源
http://www.webjars.org/

http://localhost:8080/webjars/jquery/3.4.1/jquery.js

org.webjars
jquery
3.4.1

3.模板引擎
JSP.Velocity.Freemarker.Thymeleaf;

Springboot推薦的thymeleaf
語法簡單,功能強大
1.引入thymeleaf
2.語法規則

3.SpringMVC自動配置
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
27.1.1 Spring MVC auto-configuration
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
The auto-configuration adds the following features on top of Spring’s defaults:
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
#自動配置了ViewResolver(視圖解析器:根據方法返回值得到視圖對象(View),視圖對象決定如何渲染(轉發?重定向?))
#ContentNegotiatingViewResolver :組合所有的視圖解析器的;
#如何定製:我們可以自己給容器添加一個視圖解析器;自動的將其組合進來
Support for serving static resources, including support for WebJars (see below)靜態資源文件夾路徑資源webjars
Automatic registration of Converter, GenericConverter, Formatter beans.
Support for HttpMessageConverters (see below).
Automatic registration of MessageCodesResolver (see below).
Static index.html support.靜態首頁訪問
Custom Favicon support (see below).facicon.ico
自動註冊了of Converter ,GennericConverter ,Formatter beans;
Converter:轉換器; public String hello(User user);
Automatic use of a ConfigurableWebBindingInitializer bean (see below).
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

全面接管spring MVC
springboot對springmvc的自動配置不需要了,所有都是我們自己配置;所有的springMVC的自動配置都失效了
我們需要在配置類中添加@EnableWebMvc即可;
去頁面獲取國際化的值:

效果:根據瀏覽器語言的信息設置切換了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章