SpringMVC post請求的處理
一,SpringMVC解析POST提交的數(shù)據(jù)
–1,需求:解析form表單提交的大量數(shù)據(jù)


–2, 準(zhǔn)備html頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>通過post提交數(shù)據(jù)</title>
<!-- 在HTML中嵌入css代碼 -->
<style>
/* 輸入框的高度寬度 */
input[type='text']{
width: 280px;
height: 30px;
}
/* 整體右移 */
form{
margin-left:50px ;
}
</style>
</head>
<body>
<!-- method屬性用來指定數(shù)據(jù)的提交方式,action屬性用來指定數(shù)據(jù)要提交到哪里,
name屬性用來指定數(shù)據(jù)提交時的屬性名 name=jack
value屬性用來指定要提交的具體值
-->
<form method="post" action="http://localhost:8080/stu/add">
<table>
<tr>
<td>
<h2>學(xué)生信息管理系統(tǒng)MIS</h2>
</td>
</tr>
<tr>
<td>姓名: </td>
</tr>
<tr>
<td>
<input type="text" placeholder="請輸入姓名..." name="name"/>
</td>
</tr>
<tr>
<td>年齡: </td>
</tr>
<tr>
<td>
<input type="text" placeholder="請輸入年齡..." name="age"/>
</td>
</tr>
<tr>
<td>
性別:(單選框)
<input type="radio" name="sex" value="1" checked="checked"/>男
<input type="radio" name="sex" value="0"/>女
</td>
</tr>
<tr>
<td>
愛好:(多選)
<input type="checkbox" name="hobby" value="ppq" checked="checked"/>乒乓球
<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">???lt;/option>
<option value="3">研究生</option>
</select>
</td>
</tr>
<tr>
<td>
入學(xué)日期:
<input type="date" name="intime"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="保存" style="color: white;background-color: #0000FF;border-color: #0000FF;"/>
<input type="reset" value="取消" style="color: white;background-color: palevioletred;border-color: palevioletred;"/>
</td>
</tr>
</table>
</form>
</body>
</html>
–3,準(zhǔn)備Student類
package cn.tedu.pojo;
import java.util.Arrays;
import java.util.Date;
//充當(dāng)了MVC的M層,用來封裝數(shù)據(jù)(類里的屬性名 要和 頁面上name屬性的值 一致,不然沒法封裝)
public class Student {
private String name;
private Integer age;
private Integer sex;
private String[] hobby;//多選
private Integer edu;
//注意:HTML網(wǎng)頁上輸入的日期是String類型的,無法轉(zhuǎn)成Date類型,HTML上會有400報錯,需要用注解進(jìn)行類型轉(zhuǎn)換
@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 +
'}';
}
}
–4,準(zhǔn)備RunApp類
package cn.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//注意存放位置:要在所有資源所在的文件夾的外面
@SpringBootApplication
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
–5,準(zhǔn)備StuController類
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//作為MVC的C層,用來接受請求給出響應(yīng)
@RestController
@RequestMapping("stu")
public class StuController {
//解析post方式提交的數(shù)據(jù)
@RequestMapping("add")
public void add(Student s){
System.out.println(s);
}
}
–6,測試

二,改造成Ajax提交post請求的數(shù)據(jù)
–1,修改網(wǎng)頁的 保存按鈕
<input type="button" value="保存" onclick="fun();" style="color: white;background-color: #0000FF;border-color: #0000FF;"/>
–2,修改網(wǎng)頁的 form標(biāo)簽
<form method="post" action="#" id="f1">
–3,通過ajax提交數(shù)據(jù)
<!--1. 使用jQuery提供的ajax,導(dǎo)入jQuery提供的函數(shù)庫 -->
<script src="jquery-1.8.3.min.js"></script>
<!--2. 開始寫ajax的代碼 -->
<script>
function fun(){
$.ajax({
url: "http://localhost:8080/stu/add" , //指定要請求的路徑
data: $("#f1").serialize() , //請求時要攜帶的參數(shù)
success: function(data){ //成功時會回調(diào)的函數(shù)
alert(data);
}
});
}
</script>
–4,修改Controller的代碼,添加了返回值和跨域問題的注解
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.CrossOrigin;
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;
//作為MVC的C層,用來接受請求給出響應(yīng)
@RestController
@RequestMapping("stu")
**@CrossOrigin//放行所有的請求,解決跨域問題**
public class StuController {
//解析post方式提交的數(shù)據(jù)
@RequestMapping("add")
public Student add(Student s) throws Exception {
System.out.println(s);
**return s;**
}
}
–5,測試

三,總結(jié)SpringMVC
–1,原理
–2,常用的注解
@RestController:接受用戶的請求,并響應(yīng)json數(shù)據(jù) @RequestMapping: 和請求路徑匹配 @PathVariable:獲取restful里的參數(shù)值 @CrossOrigin: 解決跨域問題(IP不同或者端口不同)
–3,解析參數(shù)
SpringMVC 可以處理一個參數(shù),也可以處理多個參數(shù)。如果需要也可以把多個參數(shù),封裝成一個java對象??梢杂肎ET方式提交數(shù)據(jù),在Controller層直接用方法的 參數(shù)列表 匹配解析就可以了可以用Restful方式提交數(shù)據(jù),在Controller層,使用@PathVariable注解獲取地址欄里的值,直接用方法的 參數(shù)列表 匹配解析就可以了可以用POST方式提交數(shù)據(jù),在Controller層直接用java對象接受請求參數(shù)就可以
–4,返回json串
以前的版本,使用@ResponseBody把數(shù)據(jù)變成json串,給瀏覽器返回新的版本,使用@RestController把數(shù)據(jù)變成json串,給瀏覽器返回
四,Spring
–1,概念
SpringMVC框架用來,接收請求和做出響應(yīng) Spring框架用來管理Bean。核心的組件BeanFactory用來存儲Bean。 ApplicationContext是Spring容器。 IOC是控制反轉(zhuǎn),是指把new的過程交給Spring。 DI是依賴注入是指,spring框架可以把對象間的依賴關(guān)系,自動裝配。 AOP面向切面編程,是一種思想。
–2,Spring IOC
需求:創(chuàng)建一個類,交給spring框架進(jìn)行ioc(new)

創(chuàng)建Hello類
package cn.tedu.spring;
public class Hello {
public void show(){
System.out.println("hello springioc~");
}
}
配置類的信息創(chuàng)建spring的核心配置文件:選中resources-右鍵-new-xml configuration-spring config-起個配置文件的名字-回車
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--這個文件是spring框架的核心配置文件,主要用來配置各種bean-->
<!--使用bean標(biāo)簽,用來指定類的信息
class屬性用來指定類的全路徑(包名.類名)
id屬性用來作為bean的唯一標(biāo)記
IOC:由spring框架創(chuàng)建Hello的對象
Map==={"hello",new Hello()}
-->
<bean class="cn.tedu.spring.Hello" id="hello"></bean>
</beans>
測試(從spring容器中獲取對象)
package cn.tedu.spring;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//測試 spring框架真的進(jìn)行ioc ???
public class TestIOC {
//單元測試::用來測試程序的。@Test + void + 沒有參數(shù) + public
@Test
public void ioc(){
//1,加載spring的核心配置文件
ClassPathXmlApplicationContext spring =
new ClassPathXmlApplicationContext(
"spring-config.xml");
//2,從spring容器中獲取bean對象--ioc
Object o = spring.getBean("hello");
Object o2 = spring.getBean("hello");
System.out.println(o == o2);//true
}
}
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Spring中的@EnableScheduling定時任務(wù)注解
這篇文章主要介紹了Spring中的@EnableScheduling注解,@EnableScheduling是 Spring Framework 提供的一個注解,用于啟用 Spring 的定時任務(wù)功能,通過使用這個注解,可以在 Spring 應(yīng)用程序中創(chuàng)建定時任務(wù),需要的朋友可以參考下2024-01-01
Spring Security基于JWT登錄認(rèn)證的項目實踐
JWT被用來在身份提供者和服務(wù)提供者間傳遞被認(rèn)證的用戶身份信息,本文主要介紹了Spring Security基于JWT登錄認(rèn)證的項目實踐,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
springboot如何實現(xiàn)異步響應(yīng)請求(前端請求超時的問題解決)
這篇文章主要給大家介紹了關(guān)于springboot如何實現(xiàn)異步響應(yīng)請求(前端請求超時的問題解決)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-01-01
解決idea services窗口不見的一種特殊情況(小白采坑系列)
這篇文章主要介紹了解決idea services窗口不見的一種特殊情況(小白采坑系列),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java中Date數(shù)據(jù)類型的數(shù)值轉(zhuǎn)換方式
這篇文章主要介紹了Java中Date數(shù)據(jù)類型的數(shù)值轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java訪問者模式實現(xiàn)優(yōu)雅的對象結(jié)構(gòu)處理
Java訪問者模式是一種行為型設(shè)計模式,它通過將數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)操作分離,實現(xiàn)對復(fù)雜對象結(jié)構(gòu)的處理。它將數(shù)據(jù)結(jié)構(gòu)中的每個元素都轉(zhuǎn)換為訪問者能夠識別的形式,從而使得數(shù)據(jù)操作可以在不影響數(shù)據(jù)結(jié)構(gòu)的前提下進(jìn)行擴(kuò)展和變化2023-04-04
Java日常練習(xí)題,每天進(jìn)步一點點(34)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
maven的pom.xml中repositories和distributionManagement使用
這篇文章主要介紹了maven的pom.xml中repositories和distributionManagement使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java日常練習(xí)題,每天進(jìn)步一點點(54)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08

