温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringMVC中怎么发送GET、POST请求

发布时间:2021-07-24 15:47:59 来源:亿速云 阅读:451 作者:Leah 栏目:编程语言

SpringMVC中怎么发送GET、POST请求,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

我们知道发起 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-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 过滤器用来识别 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>

      <h3>Restful ADN CRUD</h3>

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

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

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

看完上述内容,你们掌握SpringMVC中怎么发送GET、POST请求的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI