SpringMvc学习笔记(二)-RequestMapping专题

关于映射路径的定义方式(多路径,通配符,占位符传递数据),控制传递的参数,请求方式(get,post,delete,put,patch)

源码获取github

1.项目结构

2.读取自定义核心配置文件的名称和位置

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_3_1.xsd"
         version="3.1">

   <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <!--自定义的核心配置文件的名字和位置-->
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

3.映射文件的@RequestMapping路径方式

多路径访问,一个路径访问,使用通配符,占位符传递数据

PathDemoController.java

package com.hs.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 映射路径的定义方式
 */
@Controller//(value = "hsController")   //这就是给controller取的名字,默认是类名首字母小写
@RequestMapping("/cy42")    //这个是设置本类总的路径,比如你访问这个类的test01,网址栏就要写localhost:8080/mvc/cy42/test01
public class PathDemoController {

   @RequestMapping(path = {"/test01","/test02"})
   public ModelAndView test01() {
      System.out.println("多路径访问,用数组这样设置");
      return null;
   }

   @RequestMapping(path = "/test03")
   public ModelAndView test03() {
      System.out.println("一个路径");
      return null;
   }

   @RequestMapping("/test04")
   public ModelAndView test04() {
      System.out.println("只有一个路径属性的值,可以省略");
      return null;
   }

   //1.在映射路径中可以适应通配符 *
   //A.谁描述的清楚就找谁   eg:/test01 找/test01
   //B.通配符使用的很少
   @RequestMapping("/*")
   public ModelAndView test05() {
      System.out.println("可以使用通配符");
      return null;
   }

   @RequestMapping("/**")
   public ModelAndView test06() {
      System.out.println("任意层次");
      return null;
   }

   //使用占位符,一般只是传递主键和简单的字符串
   // 完成REST风格的路径,优先级比通配符高
   @RequestMapping("/{user_id}")   //user_id接收输入的路径值
   public ModelAndView test06(@PathVariable("user_id") String id) {    //使用注解,把user_id接收到的值赋给id
      System.out.println("使用占位符,"+id);
      System.out.println(id);
      return null;
   }
}

在浏览器网址栏输入localhost:8080/mvc/cy42/xx访问这些路径,占位符访问就是localhost:8080/mvc/xxx,然后user_id就接收到了你的xxx值

4.在网址栏传递参数(params属性)

ParamsDemoController.java

package com.hs.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**控制传递的参数
 * 如果没有传递参数会报400的错误
 */
@Controller
@RequestMapping("/vip")
public class ParamsDemoController {

   @RequestMapping(path = "/param01",params = "hs=tomcat")
   public ModelAndView test01() {
      System.out.println("你必须要传递一下形式的数据里满足params里的数据,eg:param01?hs=tomcat&id=100");
      return null;
   }

   @RequestMapping(path = "/param02",params = {"hs!=tomcat","method=add"})
   public ModelAndView test02() {
      System.out.println("!=,hs!=tomcat是指,只要不输入hs=tomcat,其他的都满足");
      return null;
   }
}

5.请求方式web.xml

  1. 在HTML页面,请求方式有几种?

    • GET: GET在地址栏能看到数据

    • POST: 在地址栏不能看到数据

    • 有且只有这两种请求!!

    但是SpirngMvc支持有8种请求方式

    官方文档:

HTTP Method Conversion

A key principle of REST is the use of the Uniform Interface. This means that all resources (URLs) can be manipulated using the same four HTTP methods: GET, PUT, POST, and DELETE. For each method, the HTTP specification defines the exact semantics. For instance, a GET should always be a safe operation, meaning that is has no side effects, and a PUT or DELETE should be idempotent, meaning that you can repeat these operations over and over again, but the end result should be the same. While HTTP defines these four methods,
HTML only supports two: GET and POST.
Fortunately, there are two possible workarounds:

第一种: you can either use JavaScript to do your PUT or DELETE,[不推荐,因为有一些浏览器不不支持PUT/DELETE IE6/7/8]

第二种方式步骤如下:

第一步: 需要使用POST请求为真正的请求方式
or simply do a POST with the ‘real’ method
第二步: 表单当中需要自己设置一个隐藏的<input>标签
as an additional parameter (modeled as a hidden input field in an HTML form).
第三步: web.xml中配置过滤器

This latter trick is what Spring’s HiddenHttpMethodFilter does. This filter is a plain Servlet Filter (Servlet过滤器)
/* 路径过滤器
名字Servlet过滤器
and therefore it can be used in combination with any web framework (not just Spring MVC). Simply add this filter to your web.xml, and a POST with
第四步: 你的
a hidden _method parameter will be converted into the corresponding HTTP method request.

下面代码加在 web.xml

<!--http的请求转换过滤器
过滤器使html中支持除了get、post,的其他请求put,delete,patch-->
<filter>
   <filter-name>httpMethodFilter</filter-name>
   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
   <filter-name>httpMethodFilter</filter-name>
   <!--设置需要过滤的Servlet的名字,就是下面那个servlet-->
   <servlet-name>springmvc</servlet-name>
</filter-mapping>

6.请求方式controller

MethodDemoController.java

package com.hs.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 请求方式
 */
@Controller
@RequestMapping("/http")
public class MethodDemoController {

   @RequestMapping(path = "/user", method = RequestMethod.GET)
   public ModelAndView GET() {
      System.out.println("GET请求:只能做查询操作");
      return null;
   }

   @RequestMapping(path = "/user", method = RequestMethod.POST)
   public ModelAndView POST() {
      System.out.println("POST请求:添加操作");
      return null;
   }

   @RequestMapping(path = "/user", method = RequestMethod.DELETE)
   public ModelAndView DELETE() {
      System.out.println("DELETE请求:删除操作");
      return null;
   }

   @RequestMapping(path = "/user", method = RequestMethod.PUT)
   public ModelAndView PUT() {
      System.out.println("PUT请求:更新操作[等幂(也就是全部更新)]");
      return null;
   }

   @RequestMapping(path = "/user", method = RequestMethod.PATCH)
   public ModelAndView PATCH() {
      System.out.println("PATCH请求:更新操作[不等幂]");
      return null;
   }

}

7.请求方式页面代码

request.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>Title</title>
</head>
<body>
<%--
   为了支持其他的请求方式,把method都改为post,然后加入一个
   <input type="hidden" name="_method" value="get|post|put|delete|patch">
    value不分大小写,name=xxx固定
--%>
   <h2>get请求</h2>
   <form action="http/user" method="post">
      <button>get请求</button>
      <input type="hidden" name="_method" value="get">
   </form>
   <h2>post请求</h2>
   <form action="http/user" method="post">
      <button>post请求</button>
      <input type="hidden" name="_method" value="post">
   </form>
   <h2>delete请求</h2>
   <form action="http/user" method="post">
      <button>delete请求</button>
      <input type="hidden" name="_method" value="delete">
   </form>
   <h2>put请求</h2>
   <form action="http/user" method="post">
      <button>put请求</button>
      <input type="hidden" name="_method" value="put">
   </form>
   <h2>patch请求</h2>
   <form action="http/user" method="post">
      <button>patch请求</button>
      <input type="hidden" name="_method" value="patch">
   </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您提供的引用内容,您遇到的问题是无法删除工程目录下的access_log文件,提示另一个程序正在使用此文件,进程无法访问。这通常是因为该文件正在被其他程序占用,导致您无法对其进行删除操作。 要解决这个问题,您可以尝试以下几种方法: 1. 首先,您可以尝试关闭正在使用该文件的程序。可以通过任务管理器或系统监控工具查找到正在使用该文件的进程,并将其关闭,然后再尝试删除该文件。 2. 如果关闭其他程序后仍然无法删除该文件,您可以尝试重启您的计算机。重启后,所有占用该文件的进程都会被终止,然后您可以尝试删除该文件。 3. 如果以上方法都无法解决问题,您可以通过使用命令行工具来删除该文件。打开命令提示符窗口,进入到该文件所在的目录,然后使用del命令加上文件名来删除该文件。例如,您可以输入以下命令来删除文件: del D:\IDEA\新Spring\springMvc\springmvc03-requestMapping\target\tomcat\logs\access_log.2023-08-22 请注意,使用命令行删除文件时,请确保您有足够的权限来执行该操作。 希望以上方法可以帮助您解决问题。如果问题仍然存在,请检查是否还有其他程序在使用该文件,或者尝试联系技术支持人员寻求帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SSM 框架 (Spring+SpringMVC+MyBatis) --学习笔记](https://blog.csdn.net/qq_22465297/article/details/83793440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [idea创建spring-springmvc-mybatis-maven](https://blog.csdn.net/Baldprogrammer/article/details/103211243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值