spring mvc学习笔记二:@RequestMapping

时间:2023-03-08 15:04:48
spring mvc学习笔记二:@RequestMapping

@RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@RequestMapping注解有六个属性:value、method、params、header、 consumes,produces

1、value:指定请求的实际地址 。

2、method:指定请求的method类型, GET、POST、PUT、DELETE等。

3、params:指定request中必须包含指定的参数值,才让该方法处理。

4、headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

value和method是常用属性,params和headers属性了解即可。

案例:

1、value/method

value值:

①value的值为普通值,如value="helloworld"

②value的值含变量,如value="hellowrold/{id}"  此处使用了{}占位符来传递变量,可以使用@PathVariable注解取值,后面会有例子

当然value的值还可以为Ant风格或者是正则表达式,由于不是很常用,此处略过。

案例1:value值为普通值,method为GET请求方式

index.jsp

<a href="helloWorld">点击我跳转到hello.js页面</a>
HelloWorldController.java
package com.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HelloWorldController { /**
* 1. 使用 @RequestMapping 注解来映射请求的 URL
* 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
* 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
*
* /WEB-INF/views/hello.jsp
*
* @return
*/
@RequestMapping(value="helloWorld",method=RequestMethod.GET)
public String helloWorld(){
System.out.println("Hello World");
return "hello";
}
}

案例2:value的值含变量,method为GET请求方式

index.jsp

<a href="requestMapping/Are you ok">传递参数</a>

HelloWorldController.java

 package com.springmvc;
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;
@Controller
public class HelloWorldController { @RequestMapping(value="/requestMapping/{info}",method=RequestMethod.GET)
public String testRequestMapping(@PathVariable(value="info") String info){
System.out.println("info:"+info);
return "hello";
}
}
 
这个例子中用到了@PathVariable注解,该注解可以将URL中的占位符映射到目标方法的参数中。

案例3:value的值为普通值,method为POST请求方式

index.jsp

<form action="springmvc/testPostMethod" method="POST">
  <input type="submit" value="post请求" />
</form>
SpringMVCTest.java
 package com.springmvc;
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 SpringMVCTest {
private static String SUCCESS = "success"; @RequestMapping(value="testPostMethod",method=RequestMethod.POST)
public String testPostMethod(){
System.out.println("POST Request success");
return SUCCESS;
} }


2、params/headers
params和headers可以更加精确的映射请求,支持简单的表达式。
SpringMVCTest.java
 package com.springmvc;
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 SpringMVCTest {
private static String SUCCESS = "success"; /**
* 该方法处理同时满足以下条件
*1、 params中包含username参数并且age=10
*2、headers中Accept-Language参数值为zh-CN,zh;q=0.8
*的请求
*
* @return
*/
@RequestMapping(value="/testParamsAndHeader",params={"username","age=10"},headers={"Accept-Language=zh-CN,zh;q=0.8"})
public String testParamsAndHeader(){
System.out.println("Test Params and Header");
return SUCCESS;
} /**
* 该方法处理params中包含username参数并且age=10的请求
* @return
*/
@RequestMapping(value="/testParams",params={"username","age=10"})
public String testParams(){
System.out.println("Test Params");
return SUCCESS;
} }