SpringMVC中怎么發(fā)送GET、POST請(qǐng)求,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)建站成立10余年來(lái),這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、國(guó)際域名空間、網(wǎng)絡(luò)營(yíng)銷(xiāo)、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗(yàn)好、性價(jià)比高、打開(kāi)快等等,這些對(duì)于網(wǎng)站建設(shè)都非常重要,創(chuàng)新互聯(lián)建站通過(guò)對(duì)建站技術(shù)性的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。
我們知道發(fā)起 GET 請(qǐng)求和 POST 請(qǐng)求,只需要在表單的 form 標(biāo)簽中,設(shè)置 method ="get" 就是GET請(qǐng)求。
設(shè)置 form 標(biāo)簽的method="post"。就會(huì)發(fā)起 POST 請(qǐng)求。而 PUT 請(qǐng)求和 DELETE 請(qǐng)求。
要有 post 請(qǐng)求的 form 標(biāo)簽
在 form 表單中,添加一個(gè)額外的隱藏域 _method ="PUT" 或 _method = "DELETE"
在web.xml中配置一個(gè)Filter過(guò)濾器org.springframework.web.filter.HiddenHttpMethodFilter(注意,這個(gè)Filter一定要在處理亂碼的Filter后面 )
簡(jiǎn)單代碼實(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)格中請(qǐng)求方式GET、POST、PUT、DELETE分別表示查、增、改、刪。
/**
* @PathVariable 路徑參數(shù)獲取
* value = "/queryUserById/{id}" 中的 {id} 表示路徑參數(shù) ,表示一個(gè)可變的值。 {id} 里面的 id 是變量的名稱
*
* @PathVariable("id") Integer id 表示把指定 id 的路徑參數(shù)值,賦給方法參數(shù)id。
* [ 也可以設(shè)置多個(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(" 查詢一條用戶記錄信息!! " + id + name);
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/queryUserList", method = RequestMethod.GET)
public ModelAndView queryUserList() {
System.out.println(" 查詢?nèi)坑脩粲涗浶畔?! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public ModelAndView saveUser() {
System.out.println(" 保存用戶記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("redirect:/index.jsp");
@RequestMapping(value = "/updateUser/1", method = RequestMethod.PUT)
public ModelAndView updateUser() {
System.out.println(" 更新用戶記錄信息!! ");
// redirect 到 index.jsp 頁(yè)面
return new ModelAndView("/index.jsp");
@RequestMapping(value = "/deleteUserById/1", method = RequestMethod.DELETE)
public ModelAndView deleteUserById() {
System.out.println(" 刪除用戶記錄信息!! ");
// 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:非農(nóng)數(shù)據(jù)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)識(shí)別 PUT 和 DELETE 請(qǐng)求 -->
<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">查詢一條用戶記錄</a></h4>
<h4><a href="${pageContext.request.contextPath}/queryUserList">查詢?nèi)坑脩粲涗?lt;/a></h4>
<form action="${pageContext.request.contextPath}/saveUser" method="post">
<input type="submit" value="保存用戶">
</form>
如何發(fā)送 PUT請(qǐng)求,和 DELETE 請(qǐng)求
1. 首先要有一個(gè) post 請(qǐng)求的表單
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)識(shí)別 PUT 和 DELETE 請(qǐng)求
--%>
<form action="${pageContext.request.contextPath}/updateUser/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="更新用戶">
</form>
<form action="${pageContext.request.contextPath}/deleteUserById/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="刪除用戶">
</form>
</body>
</html>
看完上述內(nèi)容,你們掌握SpringMVC中怎么發(fā)送GET、POST請(qǐng)求的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享名稱:SpringMVC中怎么發(fā)送GET、POST請(qǐng)求
文章鏈接:http://muchs.cn/article4/pihdie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站設(shè)計(jì)、ChatGPT、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)