使用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即可;
去页面获取国际化的值:

效果:根据浏览器语言的信息设置切换了

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