SpringMVC解析post請(qǐng)求參數(shù)詳解
SpringMVC
一,概述
作用是接受服務(wù)器請(qǐng)求并做出響應(yīng),是spring的后續(xù)產(chǎn)品,使用注解@RestController和@RequestMapping
MVC設(shè)計(jì)模式:
M是model模型,用來封裝數(shù)據(jù)
V是view視圖,用來展示數(shù)據(jù)
C是control控制器,用來控制瀏覽器如何請(qǐng)求,做出數(shù)據(jù)響應(yīng)
好處:提高代碼的復(fù)用性,松耦合
二、原理:
1.前端控制器DispatcherServlet:當(dāng)瀏覽器發(fā)送請(qǐng)求成功后,充當(dāng)調(diào)度者的角色,負(fù)責(zé)調(diào)度每個(gè)組件
2.處理器映射器HandlerMapping:根據(jù)請(qǐng)求的url路徑,找到能處理請(qǐng)求的類名和方法名
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的請(qǐng)求參數(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="請(qǐng)輸入姓名..." name="name" /></td></tr> <tr><td>年齡:</td></tr> <tr><td><input class="a" type="number" placeholder="請(qǐng)輸入年齡..." 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í)不提倡,測(cè)試的時(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;/*字號(hào)*/ } /* 屬性選擇器 */ /*修飾提交按鈕*/ 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做得更加美觀哦,這里只是為了測(cè)試,如果有興趣還可以自己做得更加好看哦~
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)建啟動(dòng)類
一般命名為RunApp,位置必須放在所有資源之上的包里
package cn.tedu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /**這是一個(gè)啟動(dòng)類 * 位置:必須在所有資源之上的包里*/ @SpringBootApplication public class RunApp { public static void main(String[] args) { SpringApplication.run(RunApp.class); } }
4,創(chuàng)建數(shù)據(jù)庫,表
要與Student類相對(duì)應(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層,控制層,用來接受請(qǐng)求和給出響應(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 //注冊(cè)驅(qū)動(dòng) 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.測(cè)試
運(yùn)行啟動(dòng)類,執(zhí)行前端頁面,提交表單數(shù)據(jù),并在數(shù)據(jù)庫中查看數(shù)據(jù)入庫情況
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- springmvc接口接收參數(shù)與請(qǐng)求參數(shù)格式的整理
- 使用SpringMVC 重寫、擴(kuò)展HttpServletRequest請(qǐng)求參數(shù)
- SpringMvc接受請(qǐng)求參數(shù)的幾種情況演示
- 詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問題
- Spring MVC請(qǐng)求參數(shù)與響應(yīng)結(jié)果全局加密和解密詳解
- 快速解決SpringMVC @RequestBody 用map接收請(qǐng)求參數(shù)的問題
- 學(xué)習(xí)SpringMVC——如何獲取請(qǐng)求參數(shù)詳解
- Spring?MVC??接受請(qǐng)求參數(shù)的方法
相關(guān)文章
淺談Java中強(qiáng)制類型轉(zhuǎn)換的問題
下面小編就為大家?guī)硪黄獪\談Java中強(qiáng)制類型轉(zhuǎn)換的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05異常解決SpringBoot項(xiàng)目啟動(dòng)卡住,無任何異常信息問題
這篇文章主要介紹了異常解決SpringBoot項(xiàng)目啟動(dòng)卡住,無任何異常信息問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03RestTemplate如何使用JSON發(fā)送Post請(qǐng)求
這篇文章主要介紹了RestTemplate如何使用JSON發(fā)送Post請(qǐng)求問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09springboot表單提交之validator校驗(yàn)
在前臺(tái)表單驗(yàn)證的時(shí)候,通常會(huì)校驗(yàn)一些數(shù)據(jù)的可行性,比如是否為空,長度,身份證,郵箱等等,這篇文章主要給大家介紹了關(guān)于springboot表單提交之validator校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2021-05-05SpringBoot基于Minio實(shí)現(xiàn)分片上傳、斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
本文主要介紹了SpringBoot基于Minio實(shí)現(xiàn)分片上傳、斷點(diǎn)續(xù)傳的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Spring Boot Admin(監(jiān)控工具)的使用
今天我們將會(huì)講解一個(gè)優(yōu)秀的監(jiān)控工具Spring Boot Admin。 它采用圖形化的界面,讓我們的Spring Boot管理更加簡單,需要的朋友可以參考下2020-02-02SpringBoot項(xiàng)目打成War包部署的方法步驟
這篇文章主要介紹了springboot項(xiàng)目如何打war包流程的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12