ITPub博客

首页 > 应用开发 > Java > SpringMVC中如何发送GET请求、POST请求、PUT请求、DELETE请求。

SpringMVC中如何发送GET请求、POST请求、PUT请求、DELETE请求。

Java 作者:专注的阿熊 时间:2020-05-13 17:13:22 0 删除 编辑

我们知道发起 GET 请求和 POST 请求,只需要在表单的 form 标签中,设置 method ="get" 就是GET请求。

设置 form 标签的method="post"。就会发起 POST 请求。而 PUT 请求和 DELETE 请求。

要有 post 请求的 form 标签

在 form 表单中,添加一个额外的隐藏域 _method ="PUT" 或 _method = "DELETE"

在web.xml中配置一个Filter过滤器org.springframework.web.filter.HiddenHttpMethodFilter(注意,这个Filter一定要在处理乱码的Filter后面 )

简单代码实现

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风格的 Controller

 * Created by YongXin Xue on 2020/05/12 9:55

 */

@Controller

public class RestController {

 ///// restful风格中请求方式GET、POST、PUT、DELETE分别表示查、增、改、删。

 /**

 * @PathVariable 路径参数获取

* value = "/queryUserById/{id}" 中的 {id} 表示路径参数 ,表示一个可变的值。 {id} 里面的 id 是变量的名称

     *

     * @PathVariable("id") Integer id 表示把指定 id 的路径参数值,赋给方法参数id。

     *  [ 也可以设置多个值 ] 如下 : 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(" 查询全部用户记录信息!! ");

        // 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

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc xmlns: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 过滤器用来识别 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 风格</title>

  </head>

  <body>

      <h2>Restful ADN CRUD</h2>

      <h3><a href="${pageContext.request.contextPath}/queryUserById/290/lance">查询一条用户记录</a></h3>

      <h3><a href="${pageContext.request.contextPath}/queryUserList">查询全部用户记录</a></h3>

 

      <form action="${pageContext.request.contextPath}/saveUser" method="post">

          <input type="submit" value="保存用户">

      </form>

  如何发送 PUT请求,和 DELETE 请求

1.  首先要有一个 post 请求的表单

2.  2. 要有一个隐藏域

                    <input type="hidden" name="_method" value="PUT">

                    <input type="hidden" name="_method" value="DELETE">

                3. 在 web.xml 中配置 Restful 的 Filter 过滤器用来识别 PUT 和 DELETE 请求

       --%>

      <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>


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2691897/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: replace方法
请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276753