溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot Web開發(fā)的工作流程

發(fā)布時(shí)間:2021-08-24 20:22:37 來(lái)源:億速云 閱讀:129 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“SpringBoot Web開發(fā)的工作流程”,在日常操作中,相信很多人在SpringBoot Web開發(fā)的工作流程問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”SpringBoot Web開發(fā)的工作流程”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

目錄
  • SpringBoot Web開發(fā)

    • 靜態(tài)資源

    • 定制首頁(yè)

    • thymeleaf模板引擎

      • 1、導(dǎo)入依賴

      • 2、controller書寫

      • 源碼分析

    • Thymeleaf語(yǔ)法

      • 基本語(yǔ)法:

  • MVC配置原理

    SpringBoot Web開發(fā)

    springboot到底幫我們配置了什么?我們能不能修改?能修改那些東西?能不能擴(kuò)展?

    • xxxAutoConfiguration: 向容器中自動(dòng)配置組件

    • xxxProperties:自動(dòng)配置類,裝配配置文件中自定義的一些內(nèi)容

    要解決的問(wèn)題:

    • 導(dǎo)入靜態(tài)資源

    • 首頁(yè)

    • jsp, 模板引擎 Thymeleaf

    • 裝配擴(kuò)展SpringMVC

    • 增刪改查

    • 攔截器

    • 國(guó)際化

    靜態(tài)資源

    SpringBoot Web開發(fā)的工作流程

    總結(jié):

    1、在springboot,我們可以使用以下方式處理靜態(tài)資源

    public,static,resources

    2、優(yōu)先級(jí):resources >static(默認(rèn)) > public

    定制首頁(yè)

    首頁(yè)放在public、resources、template下面都可

    thymeleaf模板引擎

    SpringBoot Web開發(fā)的工作流程

    1、導(dǎo)入依賴
      <!--Thymeleaf-->
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring5</artifactId>
            </dependency>
            <dependency>
                <groupId>org.thymeleaf.extras</groupId>
                <artifactId>thymeleaf-extras-java8time</artifactId>
            </dependency>

    html寫在template文件下里面

    2、controller書寫
    package com.kuang.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    /*
    * 這個(gè)跳轉(zhuǎn)需要模板引擎的支持
    * 在template目錄下的所有頁(yè)面,只能通過(guò)controller來(lái)跳轉(zhuǎn)*/
    @Controller
    public class IndexController {
        @RequestMapping("/test")
        public String test(){
            return "test";
        }
    }
    源碼分析

    SpringBoot Web開發(fā)的工作流程

    html中獲取顯示后臺(tái)controller傳來(lái)的數(shù)據(jù)

    1、在html中引入標(biāo)簽

    xmlns:th="http://www.thymeleaf.org"
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <!--所有的html元素都可以被thymeleaf替換接管   th:元素名-->
    <div th:text="${msg}"></div>
    </body>
    </html>

    2、controller

    package com.kuang.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    /*
    * 這個(gè)跳轉(zhuǎn)需要模板引擎的支持
    * 在template目錄下的所有頁(yè)面,只能通過(guò)controller來(lái)跳轉(zhuǎn)*/
    @Controller
    public class IndexController {
        @RequestMapping("/test")
        public String test(Model model){
            model.addAttribute("msg","雨勢(shì)漸大了");
            return "test";
        }
    }

    Thymeleaf語(yǔ)法

    SpringBoot Web開發(fā)的工作流程

    基本語(yǔ)法:

    SpringBoot Web開發(fā)的工作流程

    SpringBoot Web開發(fā)的工作流程

    遍歷一個(gè)數(shù)據(jù):

    1、controller

    package com.kuang.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import java.util.Arrays;
    /*
    * 這個(gè)跳轉(zhuǎn)需要模板引擎的支持
    * 在template目錄下的所有頁(yè)面,只能通過(guò)controller來(lái)跳轉(zhuǎn)*/
    @Controller
    public class IndexController {
        @RequestMapping("/test")
        public String test(Model model){
            model.addAttribute("msg","雨勢(shì)漸大了");
            model.addAttribute("users", Arrays.asList("下雨了","下大了"));
            return "test";
        }
    }

    2、html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <!--遍歷數(shù)組 ,將后臺(tái)的users中的每一個(gè)元素賦值給user,并以test顯示在頁(yè)面-->
    <h4 th:each="user:${users}" th:text="${user}"></h4>
    </body>
    </html>

    MVC配置原理

    擴(kuò)展視圖解析器

    package com.kuang.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.View;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import java.util.Locale;
    //如果你想自定義一些定制化的功能,只要寫這個(gè)組件,然后將它交給springboot,springboot就會(huì)自動(dòng)幫我們配置
    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer {
        //ViewResolver 實(shí)現(xiàn)了視圖解析器接口的類,我們可以把它看作視圖解析器
        @Bean
        public ViewResolver myViewResolver(){
            return new MyViewResolver();
        }
        //自定義一個(gè)視圖解析器
        public static class MyViewResolver implements ViewResolver{
            @Override
            public View resolveViewName(String s, Locale locale) throws Exception {
                return null;
            }
        }
    }

    @EnableWebMvc //它就是導(dǎo)入了一個(gè)類:DelegatingWebMvcConfiguration: 從容器中獲取所有的webmvcconfig
    注意:
    在自定義的mvc配置類中不能加這個(gè)注解

    到此,關(guān)于“SpringBoot Web開發(fā)的工作流程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI