1 SpringMVC
1.1 SpringMVC框架介紹
Spring MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring Web Flow里面。Spring 框架提供了構(gòu)建 Web 應(yīng)用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構(gòu),從而在使用Spring進(jìn)行WEB開發(fā)時,可以選擇使用Spring的Spring MVC框架或集成其他MVC開發(fā)框架,如Struts1(現(xiàn)在一般不用),Struts 2(一般老項目使用)等等。
總結(jié)來說就是Spring內(nèi)部整合SpringMVC(web的包)
1.2 SpringMVC入門案例
1.2.1 創(chuàng)建項目
1.2.2 添加依賴項
- 添加Lombok/DevTools/Thymeleaf/SpringWeb
1.2.3 檢查pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jt</groupId>
<artifactId>springboot_demo_3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo_3</name>
<description>springboot_demo_3</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--熱部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--測試包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--SpringMVCjar包文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf導(dǎo)入模版工具類-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<!--負(fù)責(zé)項目打包部署-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2.4 編輯YML配置文件
#配置服務(wù)端口
server:
port: 8090
#配置模版工具類
spring:
thymeleaf:
#設(shè)置頁面前綴
prefix: classpath:/templates/
#設(shè)置頁面后綴
suffix: .html
#是否使用緩存
cache: false
1.2.5 在templates中添加index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringMVC入門案例</title>
</head>
<body>
<h1>Hello SpringMVC</h1>
</body>
</html>
1.2.6 默認(rèn)頁面跳轉(zhuǎn)機(jī)制
說明: SpringMVC項目啟動時默認(rèn)設(shè)置一個歡迎頁面 并且名稱必須為index
頁面效果 如圖所示
1.3 @RequestMapping注解測試
說明: 使用@RequestMapping注解攔截用戶請求 實現(xiàn)業(yè)務(wù)調(diào)用
1.3.1 編輯HelloController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //1.將該類交給Spring容器管理 2.同時開啟Spring mvc機(jī)制
public class HelloController {
/**
* 需求: http://localhost:8090/hello 訪問hello.html
* 實現(xiàn)步驟:
* 1.攔截用戶請求 @RequestMapping("/hello")
* 2.String 類型的返回值 表示返回頁面名稱
* 3.根據(jù)YML配置文件中的內(nèi)容 動態(tài)的拼接前綴和后綴 形成頁面唯一路徑
*/
//該方法以后使用的主流的方法
@RequestMapping("/hello")
public String hello() {
//動態(tài)的拼接前綴+后綴
//classpath:/templates/hello.html
return "hello";
}
}
1.3.2 頁面請求效果
http://localhost:8090/hello
1.4 實現(xiàn)數(shù)據(jù)傳遞
1.4.1 導(dǎo)入頭標(biāo)前
<!DOCTYPE html>
<!--導(dǎo)入模板標(biāo)簽!-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
1.4.2 編輯UserController
package com.jt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
/**
* mvc底層數(shù)據(jù)傳輸原則
* url: http://localhost:8090/user
* ModelAndView:
* 1.model 封裝數(shù)據(jù)的
* 2.View 封裝視圖頁面的
* handler處理器真正的執(zhí)行時 才會調(diào)用方法
*/
@RequestMapping("/user")
public ModelAndView toUser(){
ModelAndView modelAndView = new ModelAndView();
//封裝數(shù)據(jù)
modelAndView.addObject("id", 1001);
modelAndView.addObject("name", "安琪拉");
//封裝頁面數(shù)據(jù)
modelAndView.setViewName("user");
return modelAndView;
}
}
1.4.3 頁面取值
<!DOCTYPE html>
<!--導(dǎo)入模板標(biāo)簽!!!!!-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<h1>用戶測試代碼</h1>
<!--從服務(wù)器中獲取數(shù)據(jù) 表達(dá)式 ${從服務(wù)器中的key}-->
<h3 th:text="${id}"></h3>
<h3 th:text="${name}"></h3>
</body>
</html>
1.4.4 頁面請求效果
1.4 SpringMVC原理說明
1.4.1 Servlet作用
servlet是瀏覽器與服務(wù)器(tomcat) 進(jìn)行交互的一種機(jī)制
- 核心對象:
- Request 包含了用戶的所有的請求相關(guān)信息(參數(shù)…協(xié)議…地址…)
- Response 包含了服務(wù)器相關(guān)的信息(服務(wù)器地址,返回的數(shù)據(jù))
1.4.2 重要組件
- 前端控制器
- DispatcherServlet(內(nèi)部核心機(jī)制) 接收用戶所有請求
- 處理器映射器
- HandlerMapping 查找用戶的請求與業(yè)務(wù)處理的映射
- 處理器適配器
- HandlerAdapter 在眾多處理器中挑選合適的處理器去執(zhí)行業(yè)務(wù)
- 視圖解析器
- ViewResolver 實現(xiàn)頁面的路徑的拼接
1.4.3 SpringMVC調(diào)用流程圖
1.4.4 SpringMVC調(diào)用步驟
- 當(dāng)用戶發(fā)起請求時,被SpringMVC框架中的前端控制器攔截.
- 由于前端控制器,并不清楚哪個方法與請求對應(yīng),所以查詢處理器映射器.
- 當(dāng)tomcat服務(wù)器啟動,則處理器映射器會加載所有的@RequestMapping注解,將其中的路徑與方法進(jìn)行綁定, Map</請求路徑,包名.類名.方法名(參數(shù))>,將查找到的方法信息回傳給前端控制器 進(jìn)行后續(xù)調(diào)用.
- 秉承著松耦合的思想,前端控制器將查詢得到的方法, 請求處理器適配器(mvc針對不同的配置文件有專門的處理器(運行程序的機(jī)制))挑選合適的處理器去執(zhí)行(程序內(nèi)置的規(guī)則 無需人為干預(yù))
- 當(dāng)挑選合適的處理器之后,程序開始真正的執(zhí)行業(yè)務(wù)方法. Controller-Service-Mapper(Dao),執(zhí)行業(yè)務(wù). 當(dāng)業(yè)務(wù)執(zhí)行成功之后.返回統(tǒng)一的ModelAndView對象.
- 其中包含2部分?jǐn)?shù)據(jù)
- Model(服務(wù)器數(shù)據(jù))
- View(頁面邏輯名稱)
- 其中包含2部分?jǐn)?shù)據(jù)
- 當(dāng)前端控制器獲取ModelAndView對象之后,交給視圖解析器 解析View對象的邏輯名稱. 動態(tài)的拼接前綴 + 頁面邏輯名稱 + 后綴. 最終形成了用戶展現(xiàn)頁面的全路徑.
- 將Model數(shù)據(jù)填充到頁面中的過程,叫做視圖渲染. 渲染之后,將數(shù)據(jù)交給前端控制器處理.
- 將得到的完整頁面 響應(yīng)給用戶進(jìn)行展現(xiàn).
1.5 簡單參數(shù)傳遞
1.5.1 服務(wù)器向頁面?zhèn)髦?/h4>
//簡化數(shù)據(jù)傳遞
@RequestMapping("/user")
public String toUser2(Model model) {
//將數(shù)據(jù)通過model進(jìn)行傳遞
model.addAttribute("id", 1003);
model.addAttribute("name", "SpringMVC");
return "user";
}
1.5.2 頁面取值
<!DOCTYPE html>
<!--導(dǎo)入模板標(biāo)簽!!!!!-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<h1>用戶測試代碼</h1>
<!--從服務(wù)器中獲取數(shù)據(jù) 表達(dá)式 ${從服務(wù)器中的key}-->
<h3 th:text="${id}"></h3>
<h3 th:text="${name}"></h3>
</body>
</html>
1.5.3 頁面請求效果
//簡化數(shù)據(jù)傳遞
@RequestMapping("/user")
public String toUser2(Model model) {
//將數(shù)據(jù)通過model進(jìn)行傳遞
model.addAttribute("id", 1003);
model.addAttribute("name", "SpringMVC");
return "user";
}
<!DOCTYPE html>
<!--導(dǎo)入模板標(biāo)簽!!!!!-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<h1>用戶測試代碼</h1>
<!--從服務(wù)器中獲取數(shù)據(jù) 表達(dá)式 ${從服務(wù)器中的key}-->
<h3 th:text="${id}"></h3>
<h3 th:text="${name}"></h3>
</body>
</html>
1.6 頁面向服務(wù)器傳遞數(shù)據(jù)
1.6.1 編輯提交數(shù)據(jù)的頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<form action="/addUser" method="POST">
<table border="1px" cellspacing="0" align="center" width="350px" style="margin-top: 50px">
<tr align="center">
<td colspan="2"><h1>表格數(shù)據(jù)提交</h1></td>
</tr>
<tr>
<td>ID:</td>
<!--
id:標(biāo)簽的唯一標(biāo)識 不能重復(fù)
name: 數(shù)據(jù)傳遞的必備要素 不能省略
-->
<td><input id="id" name="id" type="text"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><input id="name" name="name" type="text"/></td>
</tr>
</table>
</form>
</body>
</html>
- 頁面預(yù)覽
1.6.2 編輯提交成功的頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>提交成功!!!!</h1>
</body>
</html>
1.6.3 Request 對象接收參數(shù)
當(dāng)用戶點擊提交按鈕時,將數(shù)據(jù)進(jìn)行傳遞. 所以必須編輯Controller的方法進(jìn)行接收.利用request對象進(jìn)行參數(shù)的接收.
/**
* 請求路徑: http://localhost:8090/addUser
* 請求參數(shù): id: 100 name: 張三
* request/response對象說明 只要用戶調(diào)用就會自動的賦值
* servlet缺點: 接收的參數(shù)都是String類型
*/
@RequestMapping("/addUser")
public String addUser(HttpServletRequest request){
//利用工具API進(jìn)行類型轉(zhuǎn)化
Integer id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
System.out.println("參數(shù):"+id+":"+name);
return "success";
}
- 運行結(jié)果
1.6.4 利用SpringMVC為屬性賦值
/**
* SpringMVC賦值:
* 內(nèi)部根據(jù)request.getParameter("id") 方式獲取數(shù)據(jù).
*/
@RequestMapping("/addUser")
public String addUser2(Integer id, String name) {
System.out.println("參數(shù)獲取:" + id + ":" + name);
return "success";
}
- 運行結(jié)果
2 SpringMVC 高級用法
2.1 @RequestParam
2.1.1 需求說明
有時用戶的數(shù)據(jù)可能為null,如果后端服務(wù)器數(shù)據(jù)有特殊的要求
- 要求:
- 數(shù)據(jù)為必填項
- 如果沒有填寫數(shù)據(jù),可以為其設(shè)定默認(rèn)值.
- 通過@RequestParam注解實現(xiàn).
2.1.2 編輯UserController
說明: 圖中演示了@RequestParam的注解用法
/**
* 請求參數(shù): id: 100 name: 張三
*
* @RequestParam 參數(shù)說明:
* 1.name/value 接收參數(shù)的名稱
* 2.required 默認(rèn)值true 該數(shù)據(jù)項為必填項
* 3.defaultValue 設(shè)定數(shù)據(jù)默認(rèn)值 如果參數(shù)為null則設(shè)定默認(rèn)值
* required與defaultValue 是互斥的
*/
@RequestMapping("/addUser")
public String addUserParam(
@RequestParam(value = "id", required = true, defaultValue = "1001") Integer id,
@RequestParam(value = "name", required = true, defaultValue = "張三") String name) {
System.out.println("參數(shù)獲取:" + id + ":" + name);
return "success";
}
- 運行結(jié)果
2.2 同名提交問題
2.2.1 業(yè)務(wù)描述
SpringMVC中對于頁面要求應(yīng)該保證name屬性盡可能唯一
但是如果遇到復(fù)選框操作時 重名問題將不能避免,使用如下操作優(yōu)化
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<form action="/addUser" method="POST">
<table border="1px" cellspacing="0" align="center" width="350px" style="margin-top: 50px">
<tr align="center">
<td colspan="2"><h1>表格數(shù)據(jù)提交</h1></td>
</tr>
<tr>
<td>ID:</td>
<!--
id:標(biāo)簽的唯一標(biāo)識 不能重復(fù)
name: 數(shù)據(jù)傳遞的必備要素. 不能省略
-->
<td><input id="id" name="id" type="text"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><input id="name" name="name" type="text"/></td>
</tr>
<tr>
<td>愛好:</td>
<td>
<input name="hobbys" type="checkbox" value="敲代碼"/>敲代碼
<input name="hobbys" type="checkbox" value="敲鍵盤"/>敲鍵盤
<input name="hobbys" type="checkbox" value="敲主機(jī)"/>敲主機(jī)
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit">提交</button>
</td>
</tr>
</table>
</form>
</body>
</html>
- 頁面預(yù)覽
2.2.2 數(shù)據(jù)接收
/**
* 同名提交測試
* url參數(shù): id: name: hobbys: 敲代碼 hobbys: 敲鍵盤 hobbys: 敲主機(jī)
* 參數(shù)提交的形式:springMVC自動的將參數(shù)進(jìn)行了","號拼接 敲鍵盤,敲主機(jī)
* SpringMVC優(yōu)化:
* 1.可以根據(jù),號 自動的將字符串進(jìn)行拆分
* 2.如果數(shù)據(jù)類型不是String類型,則可以自動的轉(zhuǎn)化
* 總結(jié): 如果以后遇到了同名提交問題.則使用數(shù)組或者可變參數(shù)類型接收
* String... hobbys 可變參數(shù)類型 實質(zhì)就是數(shù)組
*/
@RequestMapping("/addUser")
public String addHobbys(Integer id, String name, String hobbys) {
System.out.println("參數(shù)獲取:" + id + ":" + name + ":" + hobbys);
return "success";
}
@RequestMapping("/addUser")
public String addHobbys(Integer id, String name, String[] hobbys) {
System.out.println("參數(shù)獲取:" + id + ":" + name + ":" + Arrays.toString(hobbys));
return "success";
}
- 運行結(jié)果
2.3 對象的方式接收參數(shù)
2.3.1 需求說明
如果有大量的頁面的提交數(shù)據(jù),如果采用單獨的參數(shù)接收,必然導(dǎo)致Controller方法結(jié)構(gòu)混亂,不便于理解.所以采用對象的方式進(jìn)行封裝
2.3.2 封裝User對象
import lombok.Data;
import lombok.experimental.Accessors;
/**
* POJO實體對象中"必須"使用包裝類型
* 規(guī)則說明:
* 1.基本類型有默認(rèn)值 包裝類型默認(rèn)值為null
* 2.基本類型中沒有多余的方法 對后續(xù)代碼取值有問題
*/
@Data//get/set/toString....
@Accessors(chain = true)//幾乎不用構(gòu)造方法賦值
public class User {
//頁面name屬性 id/name/hobbys
private Integer id;
private String name;
private String[] hobbys;
}
2.3.3 編輯UserController
實現(xiàn)以對象的方式接收參數(shù)
/**
* 使用對象的方式接收數(shù)據(jù)
* URL地址: /addUser
* url參數(shù): id: name: hobbys: 敲代碼 hobbys: 敲鍵盤 hobbys: 敲主機(jī)
* 對象賦值的原理:
* 要求: POJO對象中必須有g(shù)et/set方法
* 當(dāng)用戶提交數(shù)據(jù)之后,利用對象的set方法為屬性賦值.
*/
@RequestMapping("/addUser")
public String addUser(User user) {
System.out.println(user);
return "success";
}
- 運行結(jié)果
2.4 為對象的引用賦值
2.4.1 業(yè)務(wù)需求
有時可能會遇到 name屬性重復(fù)的問題. 由于業(yè)務(wù)需要不得不寫一個重復(fù)的名稱.那么這時采用對象的引入賦值.
2.4.2 封裝Dog對象
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class Dog {
private Integer id;
private String name;
}
2.4.3 對象引用
說明: 為了實現(xiàn)數(shù)據(jù)封裝,必須將對象進(jìn)行嵌套(引用)
import lombok.Data;
import lombok.experimental.Accessors;
/**
* POJO實體對象中"必須"使用包裝類型
* 規(guī)則說明:
* 1.基本類型有默認(rèn)值 包裝類型默認(rèn)值為null
* 2.基本類型中沒有多余的方法 對后續(xù)代碼取值有問題
*/
@Data//get/set/toString....
@Accessors(chain = true)//幾乎不用構(gòu)造方法賦值
public class User {
//頁面name屬性 id/name/hobbys
private Integer id;
private String name;
private String[] hobbys;
private Dog dog;//通過對象的引入賦值.
}
2.4.4 編輯頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringMVC測試案例</title>
</head>
<body>
<form action="/addUser" method="POST">
<table border="1px" cellspacing="0" align="center" width="350px" style="margin-top: 50px">
<tr align="center">
<td colspan="2"><h1>表格數(shù)據(jù)提交</h1></td>
</tr>
<tr>
<td>ID:</td>
<!--
id:標(biāo)簽的唯一標(biāo)識 不能重復(fù)
name: 數(shù)據(jù)傳遞的必備要素. 不能省略
-->
<td><input id="id" name="id" type="text"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><input id="name" name="name" type="text"/></td>
</tr>
<tr>
<td>寵物ID</td>
<td><input name="dog.id" type="text"/></td>
</tr>
<tr>
<td>寵物名稱</td>
<td><input name="dog.name" type="text"/></td>
</tr>
<tr>
<td>愛好:</td>
<td>
<input name="hobbys" type="checkbox" value="敲代碼"/>敲代碼
<input name="hobbys" type="checkbox" value="敲鍵盤"/>敲鍵盤
<input name="hobbys" type="checkbox" value="敲主機(jī)"/>敲主機(jī)
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit">提交</button>
</td>
</tr>
</table>
</form>
</body>
</html>
- 頁面預(yù)覽
- 通過對象.的方式封裝所屬關(guān)系
文章來源:http://www.zghlxwxcb.cn/news/detail-824313.html
2.4.5 編輯Controller
/**
* 在內(nèi)部封裝引用對象. 使用User接收全部數(shù)據(jù).
*/
@RequestMapping("/addUser")
public String addUserDog(User user) {
System.out.println(user);
return "success";
}
- 運行結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-824313.html
到了這里,關(guān)于DAY09_SpringBoot—整合SpringMVC&SpringMVC參數(shù)取值用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!