SpringBoot Web開發全教程來啦,一招學會


SpringBoot Web開發

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

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

要解決的問題:

  • 導入靜態資源
  • 首頁
  • jsp, 模板引擎 Thymeleaf
  • 裝配擴展SpringMVC
  • 增刪改查
  • 攔截器
  • 國際化

靜態資源

image-20210822114403747

總結:

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

public,static,resources

2、優先級:resources >static(默認) > public

定制首頁

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

thymeleaf模板引擎

image-20210822151215883

1、導入依賴

1
2
3
4
5
6
7
8
9
<!--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書寫

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 這個跳轉需要模板引擎的支持
* 在template目錄下的所有頁面,只能通過controller來跳轉*/
@Controller
public class IndexController {
     @RequestMapping ( "/test" )
     public String test(){
         return "test" ;
     }
}

源碼分析

image-20210822153106980

html中獲取顯示后台controller傳來的數據

1、在html中引入標簽

1
xmlns:th="https://suowo.cn"
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
< html lang = "en" xmlns:th = "https://suowo.cn" >
< head >
     < meta charset = "UTF-8" >
     < title >Title</ title >
</ head >
< body >
<!--所有的html元素都可以被thymeleaf替換接管   th:元素名-->
< div th:text = "${msg}" ></ div >
</ body >
</ html >

2、controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 這個跳轉需要模板引擎的支持
* 在template目錄下的所有頁面,只能通過controller來跳轉*/
@Controller
public class IndexController {
     @RequestMapping ( "/test" )
     public String test(Model model){
         model.addAttribute( "msg" , "雨勢漸大了" );
         return "test" ;
     }
}

Thymeleaf語法

image-20210822154222783

基本語法:

image-20210822154551592

image-20210822154642654

遍歷一個數據:

1、controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
/*
* 這個跳轉需要模板引擎的支持
* 在template目錄下的所有頁面,只能通過controller來跳轉*/
@Controller
public class IndexController {
     @RequestMapping ( "/test" )
     public String test(Model model){
         model.addAttribute( "msg" , "雨勢漸大了" );
         model.addAttribute( "users" , Arrays.asList( "下雨了" , "下大了" ));
         return "test" ;
     }
}

2、html

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
< html lang = "en" xmlns:th = "https://suowo.cn" >
< head >
     < meta charset = "UTF-8" >
     < title >Title</ title >
</ head >
< body >
<!--遍歷數組 ,將后台的users中的每一個元素賦值給user,並以test顯示在頁面-->
< h3 th:each = "user:${users}" th:text = "${user}" ></ h3 >
</ body >
</ html >

MVC配置原理

擴展視圖解析器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
//如果你想自定義一些定制化的功能,只要寫這個組件,然后將它交給springboot,springboot就會自動幫我們配置短網址
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
     //ViewResolver 實現了視圖解析器接口的類,我們可以把它看作視圖解析器
     @Bean
     public ViewResolver myViewResolver(){
         return new MyViewResolver();
     }
     //自定義一個視圖解析器
     public static class MyViewResolver implements ViewResolver{
         @Override
         public View resolveViewName(String s, Locale locale) throws Exception {
             return null ;
         }
     }
}

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM