四、Spring Boot与Web开发

一、web自动配置规则

1、WebMvcAutoConfiguration

2、WebMvcProperties

3、ViewResolver自动配置

4、静态资源自动映射

5、Formatter与Converter自动配置

6、HttpMessageConverter自动配置

7、静态首页

8、favicon

9、错误处理 

二、Thymeleaf模板引擎 

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP, Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成 作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够 直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。
Spring Boot推荐使用Thymeleaf、Freemarker等后现代的模板引擎技术;一但导入相 关依赖,会自动配置ThymeleafAutoConfiguration、FreeMarkerAutoConfiguration。 

1、整合Thymeleaf  

      – 1、导入starter-thymeleaf

      – 2、template文件夹下创建模板文件

      – 3、测试页面&取值

      – 4、基本配置 

2、基本语法  

• 表达式: 

      – #{...}:国际化消息

      – ${…}:变量取值

      – *{…}:当前对象/变量取值

      – @{…}:url表达式

      – ~{…}:片段引用

      – 内置对象/共用对象:  

• 判断/遍历: 

       – th:if

      – th:unless

      – th:each

      – th:switch、th:case  

• th:属性 

三、定制web扩展配置 

1、WebMvcConfigurerAdapter

      Spring Boot提供了很多xxxConfigurerAdapter来定制配置

2、定制SpringMVC配置

3、@EnableWebMvc全面接管SpringMVC

4、注册view-controller、interceptor等

5、注册Interceptor 

四、配置嵌入式Servlet容器 

1、ConfigurableEmbeddedServletContainer

2、EmbeddedServletContainerCustomizer

3、注册Servlet、Filter、Listener  

      ServletRegistrationBean

      FilterRegistrationBean

      ServletListenerRegistrationBean 

4、使用其他Servlet容器 

      Jetty(长连接)

       Undertow(不支持JSP) 

五、使用外部Servlet容器 

1、 SpringBootServletInitializer

      – 重写configure

2、SpringApplicationBuilder

     – builder.source(@SpringBootApplication类)

3、启动原理  

     – Servlet3.0标准ServletContainerInitializer扫描所有jar包中METAINF/services/javax.servlet.ServletContainerInitializer文件指定的类并加载

     – 加载spring web包下的SpringServletContainerInitializer

     – 扫描@HandleType(WebApplicationInitializer)

     – 加载SpringBootServletInitializer并运行onStartup方法

     – 加载@SpringBootApplication主类,启动容器等 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值