欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章帶你了解SpringBoot Web開發(fā)

 更新時(shí)間:2021年08月24日 16:50:28   作者:你好y  
這篇文章主要介紹了使用Spring Boot搭建Java web項(xiàng)目及開發(fā)過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

SpringBoot Web開發(fā)

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

  • xxxAutoConfiguration: 向容器中自動配置組件
  • xxxProperties:自動配置類,裝配配置文件中自定義的一些內(nèi)容

要解決的問題:

  • 導(dǎo)入靜態(tài)資源
  • 首頁
  • jsp, 模板引擎 Thymeleaf
  • 裝配擴(kuò)展SpringMVC
  • 增刪改查
  • 攔截器
  • 國際化

靜態(tài)資源

image-20210822114403747

總結(jié):

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

public,static,resources

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

定制首頁

首頁放在public、resources、template下面都可

thymeleaf模板引擎

image-20210822151215883

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目錄下的所有頁面,只能通過controller來跳轉(zhuǎn)*/
@Controller
public class IndexController {
    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}

源碼分析

image-20210822153106980

html中獲取顯示后臺controller傳來的數(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目錄下的所有頁面,只能通過controller來跳轉(zhuǎn)*/
@Controller
public class IndexController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","雨勢漸大了");
        return "test";
    }
}

Thymeleaf語法

image-20210822154222783

基本語法:

image-20210822154551592

image-20210822154642654

遍歷一個(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目錄下的所有頁面,只能通過controller來跳轉(zhuǎn)*/
@Controller
public class IndexController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","雨勢漸大了");
        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ù)組 ,將后臺的users中的每一個(gè)元素賦值給user,并以test顯示在頁面-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
</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就會自動幫我們配置
@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è)注解

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • spring boot 如何指定profile啟動

    spring boot 如何指定profile啟動

    這篇文章主要介紹了spring boot 如何指定profile啟動的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解

    Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解

    這篇文章主要介紹了Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解的相關(guān)資料,主要是利用Collections.sort()方法對泛型為String的List 進(jìn)行排序,需要的朋友可以參考下
    2017-08-08
  • Eureka源碼核心類預(yù)備知識

    Eureka源碼核心類預(yù)備知識

    這篇文章主要為大家介紹了Eureka源碼核心類預(yù)備知識詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Eclipse安裝Aptana插件(注意對應(yīng)版本問題)

    Eclipse安裝Aptana插件(注意對應(yīng)版本問題)

    這篇文章主要為大家詳細(xì)介紹了Eclipse安裝Aptana插件的相關(guān)資料,特別注意對應(yīng)版本問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Java中Date日期類的使用方法示例詳解

    Java中Date日期類的使用方法示例詳解

    這篇文章主要介紹了Java中Date日期類的使用方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • SpringCloud配置客戶端ConfigClient接入服務(wù)端

    SpringCloud配置客戶端ConfigClient接入服務(wù)端

    這篇文章主要為大家介紹了SpringCloud配置客戶端ConfigClient接入服務(wù)端,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • JDBC如何獲取數(shù)據(jù)庫連接

    JDBC如何獲取數(shù)據(jù)庫連接

    這篇文章主要為大家詳細(xì)為大家詳細(xì)介紹了JDBC如何獲取數(shù)據(jù)庫連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • java @Value(

    java @Value(

    這篇文章主要介紹了java @Value("${}")獲取不到配置文件中值的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    大家都知道,java作為一門內(nèi)容豐富的編程語言,其中涉及的范圍是十分廣闊的,下面這篇文章主要給大家介紹了關(guān)于java中構(gòu)造方法及this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java基礎(chǔ)泛型詳情

    Java基礎(chǔ)泛型詳情

    這篇文章主要介紹了Java基礎(chǔ)泛型詳情,泛型是JDK5中引入的特性,它提供了編譯時(shí)類型安全檢測機(jī)制,該機(jī)制允許在編譯時(shí)檢測到非法的類型,下面文章的詳細(xì)介紹,需要的朋友可以參考一下
    2022-04-04

最新評論