zoukankan      html  css  js  c++  java
  • springboot-02 SpringBoot Web开发

    SpringBoot Web开发

    jar :webapp!

    核心: 自动装配

    要解决的问题

    • 导入静态资源

    • 首页面

    • jsp、模板引擎、Thymeleaf

    • 装配扩展springMVC

    • 增删改查练习

    • 拦截器

    • 国际化!(了解)

    静态资源

    WebMvcAutoConfiguration配置类中

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!this.resourceProperties.isAddMappings()) {
            logger.debug("Default resource handling disabled");
            return;
        }
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        if (!registry.hasMappingForPattern("/webjars/**")) {
            customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                                                 .addResourceLocations("classpath:/META-INF/resources/webjars/")
                                                 .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
                                                 .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                                                 .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }
    }

    什么是webjars?

    https://www.webjars.org/

    静态资源映射默认会响应 项目根路径下的所有请求

    /**
     * Path pattern used for static resources.
     */
    private String staticPathPattern = "/**";

    分别映射到类路径下的这些 静态资源文件中

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/",
        "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

    静态资源文件优先级

    从大到小 resource > static(默认) > public

    当前springboot版本为 2.1.7

    首页定制

    默认在类路径下的文件创建index.html都可以访问。

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
          "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

    template文件中的资源必须要通过 controller才可以访问,template类似于WEB-INF文件

    借助于 Thymeleaf依赖。从控制器转发到 指定的tempalte文件页面。

    导入Thymeleaf依赖

    <!--Thymeleaf 默认指定 2.x版本。已经不适用-->
    springboot2.2.x本版中  默认Thymeleaf本版为 3.x
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
    </dependency>

    通过控制器 返回 index字符串。 模板自动拼接 前后缀。映射到 tempalte/index.html中

    // 使用 controller 响应跳转 index.html
    // 必须借助于 模板引擎的支持!
    @GetMapping("/toindex")
    public String indexPage() {
    
        return "index";
    }

    页面上使用 Thymeleagf 语法

    在html页面导入 头部信息

     

     <html lang="en" xmlns:th="http://www.thymeleaf.org">

    通过 使用thymeleaf语法获取 作用域的各个值

    所有的html元素都可以被thymeleaf接管 th:元素名

    <h1 th:text="${msg}">首页 thymeleaf</h1>

    简单赋值、循环。

    <!--th:text 赋值-->
    <h1 th:text="${msg}">首页 thymeleaf</h1>
    <!--th:utext  内容原样输出-->
    <h1 th:utext="${msg}">首页 thymeleaf</h1>
    
    <!--遍历 方法一-->
    <h3 th:each="str: ${list}" th:text="${str}"></h3>
    <!--方法二-->
    <h3 th:each="str: ${list}">[[${str}]]</h3>

  • 相关阅读:
    2014-9-思杨的缺点
    2014-8-9月杂记
    2014-7-31 思杨在老家想我们
    生日。金鼎轩吃饭;亿旺中影看《后会无期》。
    2014-7-5~6 秦皇岛之旅,带思杨看大海
    2014-6-23 去未来幼儿园玩————————
    2014-6-15 思杨的成长是全家人的责任。
    2014-6-14 思杨跳抓钱舞---疾驰的扭扭车和思杨司机
    2014-6-7 带思杨去西三旗儿童乐园玩-----思杨晕车了
    springboot学习之一
  • 原文地址:https://www.cnblogs.com/mt-blog/p/13339962.html
Copyright © 2011-2022 走看看