新聞中心
SpringMVC中怎么發(fā)送GET、POST請求,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)建站成立10余年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、國際域名空間、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,創(chuàng)新互聯(lián)建站通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。
我們知道發(fā)起 GET 請求和 POST 請求,只需要在表單的 form 標(biāo)簽中,設(shè)置 method ="get" 就是GET請求。
設(shè)置 form 標(biāo)簽的method="post"。就會發(fā)起 POST 請求。而 PUT 請求和 DELETE 請求。
要有 post 請求的 form 標(biāo)簽
在 form 表單中,添加一個額外的隱藏域 _method ="PUT" 或 _method = "DELETE"
在web.xml中配置一個Filter過濾器org.springframework.web.filter.HiddenHttpMethodFilter(注意,這個Filter一定要在處理亂碼的Filter后面 )
簡單代碼實(shí)現(xiàn)
1. RestController.java
package com.spring.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Restful風(fēng)格的 Controller
* Created by YongXin Xue on 2020/05/12 9:55
*/
@Controller
public class RestController {
///// restful風(fēng)格中請求方式GET、POST、PUT、DELETE分別表示查、增、改、刪。
/**
* @PathVariable 路徑參數(shù)獲取
* value = "/queryUserById/{id}" 中的 {id} 表示路徑參數(shù) ,表示一個可變的值。 {id} 里面的 id 是變量的名稱
*
* @PathVariable("id") Integer id 表示把指定 id 的路徑參數(shù)值,賦給方法參數(shù)id。
* [ 也可以設(shè)置多個值 ] 如下 : Integer id; String name;
*/
@RequestMapping(value = "/queryUserById/{id}/{name}", method = RequestMethod.GET)
public ModelAndView queryUserById(@PathVariable("id") Integer id,
@PathVariable("name") String name){
System.out.println(" 查詢一條用戶記錄信息!! " + id + name);
// redirect 到 index.jsp 頁面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/queryUserList", method = RequestMethod.GET)
public ModelAndView queryUserList() {
System.out.println(" 查詢?nèi)坑脩粲涗浶畔?! ");
// redirect 到 index.jsp 頁面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public ModelAndView saveUser() {
System.out.println(" 保存用戶記錄信息!! ");
// redirect 到 index.jsp 頁面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/updateUser/1", method = RequestMethod.PUT)
public ModelAndView updateUser() {
System.out.println(" 更新用戶記錄信息!! ");
// redirect 到 index.jsp 頁面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/deleteUserById/1", method = RequestMethod.DELETE)
public ModelAndView deleteUserById() {
System.out.println(" 刪除用戶記錄信息!! ");
// redirect 到 index.jsp 頁面
return new ModelAndView("redirect:/index.jsp");
2. spring-mcv.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instancxmlns:context=" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
3. web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
4. index.jsp
<%-- Created by YongXin Xue on 2020/05/12 9:52 --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Restful ADN CRUD
查詢一條用戶記錄
查詢?nèi)坑脩粲涗?/a>
如何發(fā)送 PUT請求,和 DELETE 請求
1. 首先要有一個 post 請求的表單
2. 2. 要有一個隱藏域
3. 在 web.xml 中配置 Restful 的 Filter 過濾器用來識別 PUT 和 DELETE 請求
--%>
看完上述內(nèi)容,你們掌握SpringMVC中怎么發(fā)送GET、POST請求的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:SpringMVC中怎么發(fā)送GET、POST請求
轉(zhuǎn)載注明:http://fisionsoft.com.cn/article/pihdie.html