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


–2, 準(zhǔn)備html頁(yè)面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>通過(guò)post提交數(shù)據(jù)</title>
<!-- 在HTML中嵌入css代碼 -->
<style>
/* 輸入框的高度寬度 */
input[type='text']{
width: 280px;
height: 30px;
}
/* 整體右移 */
form{
margin-left:50px ;
}
</style>
</head>
<body>
<!-- method屬性用來(lái)指定數(shù)據(jù)的提交方式,action屬性用來(lái)指定數(shù)據(jù)要提交到哪里,
name屬性用來(lái)指定數(shù)據(jù)提交時(shí)的屬性名 name=jack
value屬性用來(lái)指定要提交的具體值
-->
<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="請(qǐng)輸入姓名..." name="name"/>
</td>
</tr>
<tr>
<td>年齡: </td>
</tr>
<tr>
<td>
<input type="text" placeholder="請(qǐng)輸入年齡..." 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>
愛(ài)好:(多選)
<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">專科</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層,用來(lái)封裝數(shù)據(jù)(類里的屬性名 要和 頁(yè)面上name屬性的值 一致,不然沒(méi)法封裝)
public class Student {
private String name;
private Integer age;
private Integer sex;
private String[] hobby;//多選
private Integer edu;
//注意:HTML網(wǎng)頁(yè)上輸入的日期是String類型的,無(wú)法轉(zhuǎn)成Date類型,HTML上會(huì)有400報(bào)錯(cuò),需要用注解進(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層,用來(lái)接受請(qǐng)求給出響應(yīng)
@RestController
@RequestMapping("stu")
public class StuController {
//解析post方式提交的數(shù)據(jù)
@RequestMapping("add")
public void add(Student s){
System.out.println(s);
}
}
–6,測(cè)試

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

三,總結(jié)SpringMVC
–1,原理
–2,常用的注解
@RestController:接受用戶的請(qǐng)求,并響應(yīng)json數(shù)據(jù) @RequestMapping: 和請(qǐng)求路徑匹配 @PathVariable:獲取restful里的參數(shù)值 @CrossOrigin: 解決跨域問(wèn)題(IP不同或者端口不同)
–3,解析參數(shù)
SpringMVC 可以處理一個(gè)參數(shù),也可以處理多個(gè)參數(shù)。如果需要也可以把多個(gè)參數(shù),封裝成一個(gè)java對(duì)象。可以用GET方式提交數(shù)據(jù),在Controller層直接用方法的 參數(shù)列表 匹配解析就可以了可以用Restful方式提交數(shù)據(jù),在Controller層,使用@PathVariable注解獲取地址欄里的值,直接用方法的 參數(shù)列表 匹配解析就可以了可以用POST方式提交數(shù)據(jù),在Controller層直接用java對(duì)象接受請(qǐng)求參數(shù)就可以
–4,返回json串
以前的版本,使用@ResponseBody把數(shù)據(jù)變成json串,給瀏覽器返回新的版本,使用@RestController把數(shù)據(jù)變成json串,給瀏覽器返回
四,Spring
–1,概念
SpringMVC框架用來(lái),接收請(qǐng)求和做出響應(yīng) Spring框架用來(lái)管理Bean。核心的組件BeanFactory用來(lái)存儲(chǔ)Bean。 ApplicationContext是Spring容器。 IOC是控制反轉(zhuǎn),是指把new的過(guò)程交給Spring。 DI是依賴注入是指,spring框架可以把對(duì)象間的依賴關(guān)系,自動(dòng)裝配。 AOP面向切面編程,是一種思想。
–2,Spring IOC
需求:創(chuàng)建一個(gè)類,交給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-起個(gè)配置文件的名字-回車
<?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">
<!--這個(gè)文件是spring框架的核心配置文件,主要用來(lái)配置各種bean-->
<!--使用bean標(biāo)簽,用來(lái)指定類的信息
class屬性用來(lái)指定類的全路徑(包名.類名)
id屬性用來(lái)作為bean的唯一標(biāo)記
IOC:由spring框架創(chuàng)建Hello的對(duì)象
Map==={"hello",new Hello()}
-->
<bean class="cn.tedu.spring.Hello" id="hello"></bean>
</beans>
測(cè)試(從spring容器中獲取對(duì)象)
package cn.tedu.spring;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//測(cè)試 spring框架真的進(jìn)行ioc ???
public class TestIOC {
//單元測(cè)試::用來(lái)測(cè)試程序的。@Test + void + 沒(méi)有參數(shù) + public
@Test
public void ioc(){
//1,加載spring的核心配置文件
ClassPathXmlApplicationContext spring =
new ClassPathXmlApplicationContext(
"spring-config.xml");
//2,從spring容器中獲取bean對(duì)象--ioc
Object o = spring.getBean("hello");
Object o2 = spring.getBean("hello");
System.out.println(o == o2);//true
}
}
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
如何使用@AllArgsConstructor和final 代替 @Autowired
這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot+Redis哨兵模式的實(shí)現(xiàn)
本文主要介紹了SpringBoot+Redis哨兵模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
SpringCloud Gateway跨域配置代碼實(shí)例
這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
淺談mybatis mapper.xml文件中$和#的區(qū)別
這篇文章主要介紹了淺談mybatis mapper.xml文件中$和#的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例
這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
SpringBoot @value注解動(dòng)態(tài)刷新問(wèn)題小結(jié)
@Value注解 所對(duì)應(yīng)的數(shù)據(jù)源來(lái)自項(xiàng)目的 Environment 中,我們可以將數(shù)據(jù)庫(kù)或其他文件中的數(shù)據(jù),加載到項(xiàng)目的 Environment 中,然后 @Value注解 就可以動(dòng)態(tài)獲取到配置信息了,這篇文章主要介紹了SpringBoot @value注解動(dòng)態(tài)刷新,需要的朋友可以參考下2023-09-09
配置gateway+nacos動(dòng)態(tài)路由管理流程
這篇文章主要介紹了配置gateway+nacos動(dòng)態(tài)路由管理流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Eclipse新建項(xiàng)目不可選擇Java Project問(wèn)題解決方案
這篇文章主要介紹了Eclipse新建項(xiàng)目不可選擇Java Project問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

