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

xiaoxiao2021-03-01  12

关于映射路径的定义方式(多路径,通配符,占位符传递数据),控制传递的参数,请求方式(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

在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>
转载请注明原文地址: https://www.6miu.com/read-4050168.html

最新回复(0)