java-web spring框架之理解各种映射方式

参考视频:传送门

@RequestMapping 注解除了可以修饰类中的方法外,还可以用来修饰类,修改SpringMVCHandler类,通过在类上添加@Mapping注解可以将请求分路径

在index.jsp文件页面中HelloWorld超链要做如下修改:

<a href="spingmvc/helloWorld">helloWorld</a>

(1)根据请求方式进行映射

通过method属性  (get请求,post请求)

reset 风格的URL路径的映射

GET请求

POST请求

DELETE请求

PUT请求

扫描二维码关注公众号,回复: 10150349 查看本文章

rest意思是表现层状态转换。在WEB中,REST使用HTTP协议连接器来标识对资源的操作(获取/查询、创建、删除、修改),用HTTP Method 标识操作类型。HTTP GET 获取和查询资源、HTTP POST创建资源 、HTTP PUT修改资源 、HTTP DELETE删除资源

由于from表单只支持GET和POST方式,spring提供了一个过滤器HiddenHttpMethodFilter,可以将DELETE和PUT请求转换为标准的HTTP方式,即能将POST请求转为DELET或PUT请求

过程:

1)在web.xml配置一个过滤器

 <!-- 配置HiddenHttpFilter 可将POST请求转为我们所需要的PUT或DELETE 定义部分--> 
  <filter>
  	 <filter-name>HiddenHttpFilter</filter-name>
  	 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <!-- 映射部分 -->
  <filter-mapping>
  		<filter-name>HiddenHttpFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>

2)处理GET请求

①在index.jsp页面中添加Rest GET超链接:<a href="springmvc/rest/1">Rest GET </a>

上篇文章也是做过的了,不知道有什么区别

3)处理POST请求,上篇文章其实已经做过了

用form表单

<form action="springmvc/rest" method="post">
		<input type="submit" value="RestPOST">
	</form>

类中对应的方法:
@RequestMapping(value="/rest",method=RequestMethod.POST)
	public String restPost(){
		System.out.println("REST POST:");
		return "success";
	}

4)处理DELETE请求

依然用表单完成,POST转换为DELET

<form action="springmvc/rest/1" method="post">
		<input type="hidden" name="_method" value="DELETE">
		<input type="submit" value="rest DELETE">
	</form>

类中方法:

@RequestMapping(value="/rest/{id}",method=RequestMethod.DELETE)
	public String restDELETE(@PathVariable Integer id){
		System.out.println("REST DELETE:"+id);
		return "redirect:/springmvc/doTransfer";//重定向
	}
	
	@RequestMapping("doTransfer")
	public String doTransfer(){
		return "success";
	}

5)处理PUT请求

跟DELETE一样,DELET改成PUT就可以了


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="springmvc/helloWorld">helloWorld</a><br><br> <!-- 超链接,跳到 "helloWorld的控制器 -->
	
	<!--  <a href="springmvc/requestMethod">RequestMethod</a><br><br> 用于get -->
	
	<form action="springmvc/requestMethod" method="post">
		<input type="submit" value="RequestMethod">
	</form>
	<br>
	
	<a href="springmvc/rest/1">Rest GET</a><br><br>
	
	<form action="springmvc/rest" method="post">
		<input type="submit" value="RestPOST">
	</form>
	<br>
	
	<form action="springmvc/rest/1" method="post"><!-- post转DELETE -->
		<input type="hidden" name="_method" value="DELETE">
		<input type="submit" value="rest DELETE">
	</form>
	<br><br>
	
	<form action="springmvc/rest/1" method="post"><!-- post转PUT -->
		<input type="hidden" name="_method" value="PUT">
		<input type="submit" value="rest PUT">
	</form>
	
	
</body>
</html>
package com.springmvc.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;
//声明一个控制器
@RequestMapping("/springmvc")
@Controller
public class SpringMVCHandler {
	//地址映射
	@RequestMapping("/helloWorld")
	public String sayHello() {
		System.out.println("Hello World");
		return "success";//逻辑视图名称
	}
	
	@RequestMapping(value="/requestMethod",method=RequestMethod.POST)
	public String requestMethod() {
		System.out.println("Hello World1");
		return "success";
	}
	
	@RequestMapping(value="/rest/{id}")
	public String restGet(@PathVariable Integer id){
		System.out.println("REST GET:"+id);
		return "success";
	}
	
	@RequestMapping(value="/rest",method=RequestMethod.POST)
	public String restPost(){
		System.out.println("REST POST:");
		return "success";
	}
	
	@RequestMapping(value="/rest/{id}",method=RequestMethod.DELETE)
	public String restDELETE(@PathVariable Integer id){
		System.out.println("REST DELETE:"+id);
		return "redirect:/springmvc/doTransfer";//重定向
	}
	
	@RequestMapping(value="/rest/{id}",method=RequestMethod.PUT)
	public String restPUT(@PathVariable Integer id){
		System.out.println("REST PUT:"+id);
		return "redirect:/springmvc/doTransfer";//重定向
	}
		
	@RequestMapping("doTransfer")
	public String doTransfer(){
		return "success";
	}
	
	
	
}
发布了498 篇原创文章 · 获赞 66 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/105059057
今日推荐