SpringMVC解析post請求參數(shù)詳解
SpringMVC
一,概述
作用是接受服務(wù)器請求并做出響應(yīng),是spring的后續(xù)產(chǎn)品,使用注解@RestController和@RequestMapping
MVC設(shè)計(jì)模式:
M是model模型,用來封裝數(shù)據(jù)
V是view視圖,用來展示數(shù)據(jù)
C是control控制器,用來控制瀏覽器如何請求,做出數(shù)據(jù)響應(yīng)
好處:提高代碼的復(fù)用性,松耦合
二、原理:
1.前端控制器DispatcherServlet:當(dāng)瀏覽器發(fā)送請求成功后,充當(dāng)調(diào)度者的角色,負(fù)責(zé)調(diào)度每個(gè)組件
2.處理器映射器HandlerMapping:根據(jù)請求的url路徑,找到能處理請求的類名和方法名
Url:http://localhost:8080/abc 在HelloControl類中找到abc()
3.處理器適配器HandlerAdaptor:正式處理業(yè)務(wù),并返回結(jié)果交給DispatcherServlet
4.視圖解析器ViewResolver:找到正確的能展示數(shù)據(jù)的視圖,準(zhǔn)備展示數(shù)據(jù)
5.視圖渲染view:展示數(shù)據(jù)
1.創(chuàng)建form表單
表單form默認(rèn)提交方式是get,將提交的數(shù)據(jù)展示在網(wǎng)址上,而post提交方式隱藏了數(shù)據(jù)在網(wǎng)址上,因此更加的安全,這里使用springMVC來處理post的請求參數(shù)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>學(xué)生管理系統(tǒng)</title> <link rel="stylesheet" href="../css/form.css"/> </head> <body> <!-- 利用表單,向服務(wù)器發(fā)送數(shù)據(jù), 默認(rèn)是get提交,通過method屬性修改提交方式 action屬性,指定提交的位置--> <form method="post" action="http://localhost:8080/stu/add"> <table width="500px" height="300px"> <tr><td><h2>學(xué)生信息管理系統(tǒng)MIS</h2></td></tr> <tr><td>姓名:</td></tr> <tr><td><input class="a" type="text" placeholder="請輸入姓名..." name="name" /></td></tr> <tr><td>年齡:</td></tr> <tr><td><input class="a" type="number" placeholder="請輸入年齡..." name="age"/></td></tr> <tr><td>性別:(單選框) <input type="radio" name="sex" value="1" />男 <input type="radio" name="sex" value="0" />女 </td></tr> <tr><td>愛好:(多選) <input type="checkbox" name="hobby" value="ppq" />乒乓球 <input type="checkbox" name="hobby" value="ps" />爬山 <input type="checkbox" name="hobby" value="cg" />唱歌 </td></tr> <tr><td>學(xué)歷:(下拉框) <select name="edu"> <option value="1">本科</option> <option value="2">碩士</option> <option value="3">博士</option> <option value="4">???lt;/option> </select> </td></tr> <tr><td>入學(xué)日期:</td></tr> <tr><td><input type="date" name="intime" /></td></tr> <tr><td> <input type="submit" value="保存"/ > <input type="reset" value="取消" /> </td></tr> </table> </form> </body> </html>
css代碼
css的三種引入方式
1.行內(nèi)樣式:通過style屬性引入css樣式
例如:<h1 style="width: 20px; height: 10px; color: #FF0000;">行內(nèi)樣式</h1>
一般實(shí)際寫頁面時(shí)不提倡,測試的時(shí)候可以使用
2,內(nèi)部樣式表
通過<style></style>標(biāo)簽,寫在head標(biāo)簽中
例如:<style> .b{ width: 200px; height: 100px; background-color: #FF69B4; } </style>
3,外部樣式表
創(chuàng)建.css文件,將css樣式寫入其中,然后在html文件中引入,使用link標(biāo)簽
例如:href是css文件路徑
<link rel="stylesheet" href="../css/form.css"/>`
我這里使用了外部樣式表的方式,使css代碼和html代碼分離,使結(jié)構(gòu)更加清晰
/* 輸入框 */
/* 類選擇器 */
.a{
width: 300px;/*寬度*/
height: 40px;/*高度*/
padding: 5px;/*內(nèi)邊距*/
font-size: 15px;/*字號*/
}
/* 屬性選擇器 */
/*修飾提交按鈕*/
input[type="submit"]{
width: 60px;
height: 30px;
background-color: blue;
color: #fff;
font-size: 15px;
border-color: blue;
}
input[type="reset"]{
width: 60px;
height: 30px;
background-color:hotpink;
color: #fff;
font-size: 15px;
border-color: hotpink;
}
body{
font-size: 20px;
}

頁面還可以用css做得更加美觀哦,這里只是為了測試,如果有興趣還可以自己做得更加好看哦~
2.準(zhǔn)備Student類
package cn.tedu.pojo;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Arrays;
import java.util.Date;
//@RequestMapping("find")
//是Model層,用來封裝數(shù)據(jù),就是一個(gè)pojo(封裝的屬性+get/set)
public class Student {
//屬性(成員變量):變量類型 變量名
//提交數(shù)據(jù)的類型 頁面上name屬性的值
// public Student find(){
private String name;
private Integer age;//避免一些異常,能用引用類型最好使用引用類型
private Integer sex;
private String[] hobby;
private Integer edu;
//瀏覽器上提交的日期默認(rèn)是2021/8/12默認(rèn)是String類型
//報(bào)錯(cuò)400,需要把String的日期轉(zhuǎn)成Date日期,使用注解 @DateTimeFormat
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date intime;
// }
// 獲取get set toString
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getEdu() {
return edu;
}
public void setEdu(Integer edu) {
this.edu = edu;
}
public Date getIntime() {
return intime;
}
public void setIntime(Date intime) {
this.intime = intime;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
}
3.創(chuàng)建啟動類
一般命名為RunApp,位置必須放在所有資源之上的包里

package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**這是一個(gè)啟動類
* 位置:必須在所有資源之上的包里*/
@SpringBootApplication
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
4,創(chuàng)建數(shù)據(jù)庫,表
要與Student類相對應(yīng),愛好這一字段是數(shù)組類型,而MySQL中沒有數(shù)組類型,因此使用varchar
注意字符集使用utf-8

使用JDBC把得到的數(shù)據(jù)入庫
5.創(chuàng)建StudentController類
首先要在pom.xml中導(dǎo)入jar包(工具包)
<!-- 添加jdbc的jar包依賴-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
</dependencies>
下面是將數(shù)據(jù)入庫的代碼
package cn.tedu.controller;
//是controller層,控制層,用來接受請求和給出響應(yīng)
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;
@RestController
@RequestMapping("stu")
public class StudentController {
@RequestMapping("add")
public Object add(Student s) throws Exception {
//實(shí)現(xiàn)入庫insert--jdbc
//注冊驅(qū)動
Class.forName("com.mysql.jdbc.Driver");
//獲取連接
String url = "jdbc:mysql://localhost:3306/cgb2106";
Connection conn = DriverManager.getConnection(url, "root", "123456");
//SQL骨架
String sql = "insert into tb_student values(null,?,?,?,?,?,?)";
//獲取傳輸器
PreparedStatement ps = conn.prepareStatement(sql);
//給SQL設(shè)置值
ps.setObject(1, s.getName());
ps.setObject(2, s.getAge());
ps.setObject(3, s.getSex());
//s.getHobby())得到一個(gè)數(shù)組,不能直接入數(shù)據(jù)庫,需要變成串
ps.setObject(4, Arrays.toString(s.getHobby()));
ps.setObject(5, s.getEdu());
ps.setObject(6, s.getIntime());
//執(zhí)行SQL
ps.executeUpdate();//執(zhí)行增刪改的SQL
System.out.println("數(shù)據(jù)插入成功");
return s;
}
}
6.測試
運(yùn)行啟動類,執(zhí)行前端頁面,提交表單數(shù)據(jù),并在數(shù)據(jù)庫中查看數(shù)據(jù)入庫情況




總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- springmvc接口接收參數(shù)與請求參數(shù)格式的整理
- 使用SpringMVC 重寫、擴(kuò)展HttpServletRequest請求參數(shù)
- SpringMvc接受請求參數(shù)的幾種情況演示
- 詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題
- Spring MVC請求參數(shù)與響應(yīng)結(jié)果全局加密和解密詳解
- 快速解決SpringMVC @RequestBody 用map接收請求參數(shù)的問題
- 學(xué)習(xí)SpringMVC——如何獲取請求參數(shù)詳解
- Spring?MVC??接受請求參數(shù)的方法
相關(guān)文章
Java 實(shí)現(xiàn)加密數(shù)據(jù)庫連接的步驟
這篇文章主要介紹了Java 實(shí)現(xiàn)加密數(shù)據(jù)庫連接的步驟,幫助大家更好的理解和使用Java處理數(shù)據(jù)庫,感興趣的朋友可以了解下2020-11-11
springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧2025-03-03
Spring boot JPA實(shí)現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例
這篇文章主要介紹了Spring boot JPA實(shí)現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
java中如何實(shí)現(xiàn)對類的對象進(jìn)行排序
在本篇文章里小編給各位整理一篇關(guān)于java中如何實(shí)現(xiàn)對類的對象進(jìn)行排序知識點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02
SpringBoot3 Spring WebFlux簡介(推薦)
SpringWebFlux是Spring Framework 5中引入的響應(yīng)式Web框架,用于支持非阻塞異步通信和響應(yīng)式流處理,與傳統(tǒng)的SpringMVC相比,WebFlux提供了完全異步非阻塞的編程模型,適用高并發(fā)、微服務(wù)架構(gòu)和實(shí)時(shí)數(shù)據(jù)流,本文介紹SpringBoot3 Spring WebFlux簡介,感興趣的朋友一起看看吧2024-10-10

