- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- SpringMVC中怎么發(fā)送GET、POST請求
SpringMVC中怎么發(fā)送GET、POST請求,相信很多沒(méi)有經(jīng)驗的人對此束手無(wú)策,為此本文總結了問(wèn)題出現的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
我們知道發(fā)起 GET 請求和 POST 請求,只需要在表單的 form 標簽中,設置 method ="get" 就是GET請求。
設置 form 標簽的method="post"。就會(huì )發(fā)起 POST 請求。而 PUT 請求和 DELETE 請求。
要有 post 請求的 form 標簽
在 form 表單中,添加一個(gè)額外的隱藏域 _method ="PUT" 或 _method = "DELETE"
在web.xml中配置一個(gè)Filter過(guò)濾器org.springframework.web.filter.HiddenHttpMethodFilter(注意,這個(gè)Filter一定要在處理亂碼的Filter后面 )
簡(jiǎn)單代碼實(shí)現
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 路徑參數獲取
* value = "/queryUserById/{id}" 中的 {id} 表示路徑參數 ,表示一個(gè)可變的值。 {id} 里面的 id 是變量的名稱(chēng)
*
* @PathVariable("id") Integer id 表示把指定 id 的路徑參數值,賦給方法參數id。
* [ 也可以設置多個(gè)值 ] 如下 : 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(" 查詢(xún)一條用戶(hù)記錄信息!! " + id + name);
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/queryUserList", method = RequestMethod.GET)
public ModelAndView queryUserList() {
System.out.println(" 查詢(xún)全部用戶(hù)記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public ModelAndView saveUser() {
System.out.println(" 保存用戶(hù)記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/updateUser/1", method = RequestMethod.PUT)
public ModelAndView updateUser() {
System.out.println(" 更新用戶(hù)記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/deleteUserById/1", method = RequestMethod.DELETE)
public ModelAndView deleteUserById() {
System.out.println(" 刪除用戶(hù)記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("redirect:/index.jsp");
2. spring-mcv.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instancxmlns:context="
xsi:schemaLocation="http://www.springframework.org/schema/beans
<context:非農數據https://www.gendan5.com/nonfarmpayroll.html
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包掃描 -->
<context:component-scan base-package="com.spring.mvc"/>
</beans>
3. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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">
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 SpringMVC 的容器配置文件路徑 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 中配置 Restful 的 Filter 過(guò)濾器用來(lái)識別 PUT 和 DELETE 請求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4. index.jsp
<%-- Created by YongXin Xue on 2020/05/12 9:52 --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Restful 風(fēng)格</title>
</head>
<body>
<h3>Restful ADN CRUD</h3>
<h4><a href="${pageContext.request.contextPath}/queryUserById/290/lance">查詢(xún)一條用戶(hù)記錄</a></h4>
<h4><a href="${pageContext.request.contextPath}/queryUserList">查詢(xún)全部用戶(hù)記錄</a></h4>
<form action="${pageContext.request.contextPath}/saveUser" method="post">
<input type="submit" value="保存用戶(hù)">
</form>
如何發(fā)送 PUT請求,和 DELETE 請求
1. 首先要有一個(gè) post 請求的表單
2. 2. 要有一個(gè)隱藏域
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_method" value="DELETE">
3. 在 web.xml 中配置 Restful 的 Filter 過(guò)濾器用來(lái)識別 PUT 和 DELETE 請求
--%>
<form action="${pageContext.request.contextPath}/updateUser/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="更新用戶(hù)">
</form>
<form action="${pageContext.request.contextPath}/deleteUserById/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="刪除用戶(hù)">
</form>
</body>
</html>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站