Spring Boot学习记录(五、WEB开发)

使用springboot:

  1. 创建springboot应用,选择需要的场景;
  2. springbooty已经默认将这些场景配置好了,只需在配置文件中指定少量配置就可以运行起来;
  3. 自己编写业务逻辑代码;

 

1.springboot对静态资源的映射规则

@ConfigurationProperties(prefix="spring.resources",ignoreUnknownFileds=false)
public class ResourceProperties implements ResourceLoaderAware{
//可以设置和静态资源有关的参数,缓存时间等

 

 

1.所有/Webjars/**,都去classpath:/META-INF/resources/webjars/找资源;

webjars:以jar包的方式引入静态资源;

http://www.webjars.org/

localhost:8080/webjars/jquery/3.3.1/jquery.js

<!-- 引入jQuery-webjar -->
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.1</version> </dependency>

 

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

"classpath://META-INF/resources/",
"classpath://resources/",
"classpath://static/",
"classpath://public/",
"/":当前项目的根路径

localhost:8080/xxx  (去静态文件夹里面找xxx)

 

3.欢迎页:静态资源文件夹下的所有index.html页面;被"/**"映射;

localhost:8080/  找index页面

 

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

 

2.模板引擎

例如:jsp、 Velocity、Freemaker、Thymeleaf。

springboot推荐使用的Thymeleaf语法更简单,功能更强大;

 

1.引入Thymeleaf

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 

2.Thymeleaf使用&语法

 

Thymeleaf使用文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

 

默认前缀=“classpath:/templates/”  默认后缀=“.html”

    @RequestMapping("/success")
    public String success(){
        return "success";
    }

1.html页面中导入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>成功!~ lol</h1>
<p th:text="${hello}"/>
</body>
</html>

 

3.语法规则

    1) th:text  改变当前元素的文本内容

        th:任意html属性  来替换原生属性的值

    2)  表达式(Thymeleaf使用文档第四章)

  • 简单表达:
    • 变量表达式:${...}
    • 选择变量表达式:*{...}
    • 讯息表达:#{...}
    • 链接URL表达式:@{...}
    • 片段表达:~{...}
  • 文字
    • 文本文字:'one text''Another one!',…
    • 数字文字:0343.012.3,…
    • 布尔文字:truefalse
    • NULL文字:null
    • 文字标记:onesometextmain,…
  • 文本操作:
    • 字符串级联:+
    • 字面替换:|The name is ${name}|
  • 算术运算:
    • 二进制运算符:+-*/%
    • 减号(一元运算符):-
  • 布尔运算:
    • 二进制运算符:andor
    • 布尔否定(一元运算符):!not
  • 比较和平等:
    • 比较国:><>=<= (gtltgele)
    • 平等操作员:==!= (eqne)
  • 条件运算符:
    • 如果-然后:(if) ? (then)
    • 如果-然后-否则:(if) ? (then) : (else)
    • 违约:(value) ?: (defaultvalue)
  • 特殊令牌:
    • 不-行动:_

4.SpringMVC自动配置

http://www.spring-boot.org/doc/pages/spring-boot-features.html#boot-features-json-json-b 第28节

Spring Boot 提供了适用于大多数 Spring MVC 应用的自动配置(auto-configuration)。

自动配置在 Spring 默认功能上添加了以下功能:

  • 引入 ContentNegotiatingViewResolver 和 BeanNameViewResolver bean。
    • 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发、重定向等));
    • ContentNegotiatingViewResolver :组合所有的视图解析器;
    • 如何定制:我们可以自己给容器中t添加一个视图解析器;自动的将其解析进来;
  • 支持服务静态资源,包括对 WebJar 的支持。
  • 自动注册 ConverterGenericConverter 和 Formatter bean。
    • Converter:转换器;public String hello(User user):类型转换使用Converter
    • Formatter:格式化;2017-11-13===Date;
    • 自己添加的格式化转换器,只需要放在容器中即可
  • 支持 HttpMessageConverter
    • HttpMessageConverter:SpringMVC用来转换http请求和响应的
    • HttpMessageConverter是从容器中确定;获取所有的HttpMessageConverter
    • 自己给容器中添加HttpMessageConverter,只需要将自己的组件注册在容器中(@Bean,@Component)
  • 自动注册 MessageCodesResolver。(定义错误代码生成规则)
  • 支持静态 index.html。
  • 支持自定义 Favicon 。
  • 自动使用 ConfigurableWebBindingInitializer bean。
    • 可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器中)

如果您想保留 Spring Boot MVC 的功能,并且需要添加其他 MVC 配置(interceptor、formatter 和视图控制器等),可以添加自己的 WebMvcConfigurerAdapter 类型的 @Configuration 类,但不能带 @EnableWebMvc 注解。如果您想自定义 RequestMappingHandlerMappingRequestMappingHandlerAdapter或者 ExceptionHandlerExceptionResolver 实例,可以声明一个 WebMvcRegistrationsAdapter 实例来提供这些组件。

如果您想完全掌控 Spring MVC,可以添加自定义注解了 @EnableWebMvc 的 @Configuration 配置类。

 

扩展SpringMVC

<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
    <mvc:inpterceptor>
        <mvc:mapping path="/hello"/>
        <bean><bean/>
    </mvc:inpterceptor>
</mvc:interceptors>

  编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc 

 

5.如何修改springboot的默认配置

模式:

  1.springboot在自动配置很多组件的时候,先看容器中有没有用户自己的配置(@Bean,@Component)如果有用用户配置,没有自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

  

posted @ 2020-03-09 00:49  苏道羲  阅读(236)  评论(0编辑  收藏  举报