学习SpringMVC(二)之RequestMapping

1:@RequestMapping 除了修饰方法, 还可来修饰类


类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录

方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL相对于 WEB 应用的根目录

在前一讲中,是单单用来修饰方法,这一讲同时也用来修饰类,所以在index.jsp中加入了以下代码:


新写了一个controller

<span style="font-family:SimSun;font-size:18px;">package com.cgf.springmvc.handlers;

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

<span style="color:#ff0000;">@RequestMapping("/springmvc")</span>
@Controller
public class MyRequestMapping {
	<span style="color:#ff0000;">@RequestMapping("/RequestMapping")</span>
	public String myRequestMapping(){
		System.out.println("myRequestMapping");
		return "hello";
	}

}</span>

2:RequestMapping 的value属性指定修饰的URL路径和method属性用来指定请求方式(GET,POST)

<span style="font-family:SimSun;font-size:18px;">package com.cgf.springmvc.handlers;

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

<span style="color:#ff0000;">@RequestMapping("/springmvc")</span>
@Controller
public class MyRequestMapping {
	@RequestMapping(value="/RequestMapping",method="RequestMethod.POST")
	public String myRequestMapping(){
		System.out.println("myRequestMapping");
		return "hello";
	}

}</span>

3:RequestMapping的params属性和headers属性,用法简单,而且不太常用,这里就不会介绍了

4:@RequestMapping还支持ANT风格的URL映射

ANT风格支持3种通配符:

? ---匹配一个字符

*   ---匹配任意多个字符

** ---匹配多层路径

<span style="font-family:SimSun;font-size:18px;">package com.cgf.springmvc.handlers;

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

<span style="color:#ff0000;">@RequestMapping("/springmvc")</span>
@Controller
public class MyRequestMapping {
	@RequestMapping(value="/RequestMapping/*/cgf")
	public String myRequestMapping(){
		System.out.println("myRequestMapping");
		return "hello";
	}

}</span>
<a href=" springmvc/RequestMapping/cgf/cgf ">test_Ant</a>

5:@PathVariable映射URL绑定的占位符,通过@PathVariable可以将URL中的占位符参数绑定到控制器处理方法的入参当中。如:

@RequestMapping("/myPathVariable/{id}")
	public String myPathVariable(@PathVariable(value="id") int id){
		System.out.println("myPathVariable"+id);
		return "hello";
	}
<a href="springmvc/myPathVariable/88888888">myPathVariable</a>

6:HiddenHttpMethodFilter 浏览器的FORM表单只支持GET,POST请求,而DELETE,PUT方法并不支持,Spring3.0增加了一个过滤器,可以将POST转化为PUT或DELETE。

首先在WEB.XML文件中配置一个Filter

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

然后在index.jsp中:

<span style="font-family:SimSun;font-size:18px;"> <a href="springmvc/testRest/1">Get</a>
  <br>
   <form action="springmvc/testRest" method="post">
  	<input type="submit" value="post">
  </form>
  <br>
   <form action="springmvc/testRest/1" method="post">
  	<input type="hidden" name="_method" value="PUT">
  	<input type="submit" value="put">
  </form>
  <br>
  <form action="springmvc/testRest/1" method="post">
  	<input type="hidden" name="_method" value="DELETE">
  	<input type="submit" value="delete">
  </form></span>

最后在controller中:

<span style="font-family:SimSun;font-size:18px;">	@RequestMapping(value="/testRest",method=RequestMethod.POST)
	public String myTestRest(){
		System.out.println("myTestRest POST");
		return "hello";
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
	public String myTestRestPut(@PathVariable(value="id") int id){
		System.out.println("myTestRest PUT"+id);
		return "hello";
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
	public String myTestRestDelete(@PathVariable(value="id") int id){
		System.out.println("myTestRest Delete"+id);
		return "hello";
	}
	
	@RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
	public String myTestRestGet(@PathVariable(value="id") int id){
		System.out.println("myTestRest Get"+id);
		return "hello";
	}</span>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值