最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC post請求的處理

 更新時間:2021年07月12日 11:18:28   作者:cgblpx  
今天小編就為大家分享一篇解決SpringMVC接收不到ajaxPOST參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一,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報錯,需要用注解進行類型轉(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框架進行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框架真的進行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)文章

  • 如何使用@AllArgsConstructor和final 代替 @Autowired

    如何使用@AllArgsConstructor和final 代替 @Autowired

    這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot+Redis哨兵模式的實現(xiàn)

    SpringBoot+Redis哨兵模式的實現(xiàn)

    本文主要介紹了SpringBoot+Redis哨兵模式的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • SpringCloud Gateway跨域配置代碼實例

    SpringCloud Gateway跨域配置代碼實例

    這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 淺談mybatis mapper.xml文件中$和#的區(qū)別

    淺談mybatis mapper.xml文件中$和#的區(qū)別

    這篇文章主要介紹了淺談mybatis mapper.xml文件中$和#的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java TCP協(xié)議通信超詳細(xì)講解

    Java TCP協(xié)議通信超詳細(xì)講解

    TCP/IP是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,它會保證數(shù)據(jù)不丟包、不亂序。TCP全名是Transmission Control Protocol,它是位于網(wǎng)絡(luò)OSI模型中的第四層
    2022-09-09
  • Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例

    Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例

    這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計算等相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • SpringBoot @value注解動態(tài)刷新問題小結(jié)

    SpringBoot @value注解動態(tài)刷新問題小結(jié)

    @Value注解 所對應(yīng)的數(shù)據(jù)源來自項目的 Environment 中,我們可以將數(shù)據(jù)庫或其他文件中的數(shù)據(jù),加載到項目的 Environment 中,然后 @Value注解 就可以動態(tài)獲取到配置信息了,這篇文章主要介紹了SpringBoot @value注解動態(tài)刷新,需要的朋友可以參考下
    2023-09-09
  • 配置gateway+nacos動態(tài)路由管理流程

    配置gateway+nacos動態(tài)路由管理流程

    這篇文章主要介紹了配置gateway+nacos動態(tài)路由管理流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot?如何添加webapp文件夾

    springboot?如何添加webapp文件夾

    這篇文章主要介紹了springboot?如何添加webapp文件夾,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Eclipse新建項目不可選擇Java Project問題解決方案

    Eclipse新建項目不可選擇Java Project問題解決方案

    這篇文章主要介紹了Eclipse新建項目不可選擇Java Project問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07

最新評論

绥滨县| 永兴县| 罗江县| 仙游县| 山西省| 宜兴市| 常德市| 田阳县| 新兴县| 伊宁县| 福建省| 怀宁县| 宜兴市| 盐山县| 察隅县| 永仁县| 藁城市| 贺兰县| 宁陕县| 和田县| 泸溪县| 磐石市| 米林县| 济源市| 封丘县| 宁晋县| 石阡县| 博白县| 林甸县| 达孜县| 永安市| 马龙县| 边坝县| 安徽省| 福泉市| 邹城市| 沭阳县| 德庆县| 石嘴山市| 茶陵县| 玛纳斯县|