麻绳先生

做一些记录性的工作

spring-boot和web开发

web开发

如何使用spring boot:

  1. 创建spring boot应用,选中需要的模块;
  2. spring boot已经默认将这些场景配置好,只需要在配置文件中指定少量配置就可与运行;
  3. 编写业务代码;

自动配置原理

webjars和静态资源映射规则

  1. 所有/webjars/**,都去classpath:/META-INF/resources/webjars查找资源;
    webjars:以jar包方式引入静态资源。

  2. “/**”访问当前项目的任何资源,静态资源文件夹

    1
    2
    3
    4
    5
    "classpath:/MEAT-INF/resources/",
    "classpath:/resources/",
    "calsspath:/static/",
    "calsspath:/puclic/",
    "/":当前目录的根目录
  3. 欢迎页:静态资源文件夹下的所有index.html页面,被”/**”映射;

  4. 所有的**/favicon.ico都是在静态资源文件夹下查找;

模板引擎

JSP、

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
</properties>

thymeleaf

导入语法空间

thymeleaf语法规则

  • 片段包含:jsp:include
    • th:insert
    • th:replace
  • 遍历:c:forEach
    • th:each
  • 条件判断c:if
    • th:if
    • th:unless
    • th:switch
    • th:case
  • 声明变量c:set
    • th:object
    • th:with
  • 任意属性修改支持prepend,append
    • th:attr
    • th:attrprepend
    • th:attrappend
  • 修改指定属性默认值
    • th:value
    • th:href
    • th:src
  • 修改标签体内容
    • th:text不转义
    • th:utext转义
  • 声明片段
    • th:fragment

thymeleaf表达式

  • Simple expressions:
    • Variable Expressions: ${…}
    • Selection Variable Expressions: *{…}
    • Message Expressions: #{…}
    • Link URL Expressions: @{…}
    • Fragment Expressions: ~{…}
  • Literals
    • Text literals: ‘one text’ , ‘Another one!’ ,…
    • Number literals: 0 , 34 , 3.0 , 12.3 ,…
    • Boolean literals: true , false
    • Null literal: null
    • Literal tokens: one , sometext , main ,…
  • Text operations:
    • String concatenation: +
    • Literal substitutions: |The name is ${name}|
  • Arithmetic operations:
    • Binary operators: + , - , * , / , %
    • Minus sign (unary operator): -
  • Boolean operations:
    • Binary operators: and , or
    • Boolean negation (unary operator): ! , not
  • Comparisons and equality:
    • Comparators: > , < , >= , <= ( gt , lt , ge , le )
    • Equality operators: == , != ( eq , ne )
  • Conditional operators:
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)
  • Special tokens:
    • No-Operation: _

Spring Boot自动配置Spring MVC

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans;
  • Support for serving static resourcces, including support for Webjars;
  • Automatic registration of Converter, GenericConverter, Formatter beans;
  • Support for HttpMessageConerters;
  • Automatic registration of MessageCodeResolver;
  • Static index.html support;
  • Custom Favicon support;
  • Automatic use of a ConfigurableWebBindingInitializer bean;

修改Spring Boot默认配置

扩展Spring MVC

Tips

国际化

默认的区域信息是通过浏览器请求报文获取的。可以通过链接提交区域信息。
自己的信息解析器需要实现Localeresolver。

  1. 编写国际化配置文件,抽取页面需要配置的国际化信息;
  2. 使用ResourceBundleMessageSource管理国际化资源信息;
  3. 在页面使用fml:message获取国际化内容;
  4. 可以自己编写区域信息解析器,替换Spring Boot默认的解析器ResourceBundleMessageSource;

模板引擎要实时生效,需要禁用模板缓存,页面修改要ctrl+F9重新编译

登陆状态检查需要拦截器

thymeleaf公共页面元素抽取

  1. 抽取公共片段
  2. 引入公共片段
  3. 不同替换效果有th:insert、th:replace、th:include

错误处理的自动配置

可以参照ErrorMVCAutoConfiguration;容器中有如下组件:

  1. DefaultErrorAttributes
  2. BasicErrorController
  3. ErrorPageCustomizer
  4. DefaultErrorViewResolver

注册Servlet、Filter、Linstener

Spring Boot默认是以jar包的方式启动嵌入式的Servlet容器来启动Spring Boot的web应用,没有web.xml文件;
注册三大组件使用ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean。

其他嵌入式Servlet容器

Jetty

适合长连接

Undertow

后置处理器会获取所有容器中的定制器,以此配置Servlet容器,自己定义容器就是将自定义的定制器加到Bean工厂中。

嵌入式Servlet容器启动原理

  1. Spring Boot应用启动运行run方法;
  2. refreshContext(context);Spring Boot刷新IOC容器,创建IOC容器对象,并初始化容器,创建容器中的每一个组件,还判断了是否是web应用;
  3. refresh(context);刷新刚才创建好的ioc容器;
  4. onRefresh();web的IOC容器重写了onRefresh方法;
  5. web IOC容器会创建嵌入式的Servlet容器;createEmbeddedServletContainer();
  6. 获取嵌入式的Servlet容器工厂;TomcatEmbeddedServletContainerFactory创建对象,后置处理器随后获取所有的定制器来定制Servlet容器的相关配置;
  7. 使用容器工厂获取嵌入式的Servlet容器;
  8. 嵌入式的Servlet容器创建对象并启动Servlet容器;先启动嵌入式的Servlet容器,再将IOC容器中剩下的对象获取;

使用外置的Servlet容器

嵌入式Servlet容器优点在于简单、便携,缺点在于默认不支持JSP,优化定制不够复杂;使用定制器、自己编写嵌入式Servlet容器的创建工厂;

  1. 必须创建一个war项目,利用idea创建目录结构;
  2. 将嵌入式的Tomcat指定为provided;
  3. 必须编写一个SpringBootServletInitializer的子类,并调用configure方法;
  4. 启动tomcat;