一、SpringMVC 使用 @PathVariable、@RequestParam、@RequestHeader、@CookieValue 等来解决参数获取问题。

1. @PathVariable:映射 URL 绑定的占位符,可以借助于传入到方法参数列表中的 @PathVariable 注解获取到 URL 映射中的参数值。如:

<a href="handler01/1">test pathvariable</a>
@RequestMapping("/handler01/{id}")
public String testPathVariable(@PathVariable("id") String id) {
  System.out.println("id:" + id);
  return "success";
}

说明:URL 绑定占位符使 SpringMVC 对 REST 提供了支持。对于具体的 SpringMVC 的 REST 风格的例子会在以后的文章里介绍。

2.@RequestParam 

官方文档是这样描述的:

* Annotation which indicates that a method parameter should be bound to a web
* request parameter. Supported for annotated handler methods in Servlet and
* Portlet environments.
*
* <p>If the method parameter type is {@link Map} and a request parameter name
* is specified, then the request parameter value is converted to a {@link Map}
* assuming an appropriate conversion strategy is available.
*
* <p>If the method parameter is {@link java.util.Map Map&lt;String, String&gt;} or
* {@link org.springframework.util.MultiValueMap MultiValueMap&lt;String, String&gt;}
* and a parameter name is not specified, then the map parameter is populated
* with all request parameter names and values.

 



 

 

 

 

 

 

说明一下:

(1)该注解表明 web 请求参数绑定到目标 handler 方法的入参。

(2)如果方法的入参类型是一个 Map,不包含泛型类型,并且请求参数名称是被指定的

(如:public String testRequestParam5(@RequestParam("userName") Map map)),请求参数会被转换为一个 Map,前提是存在转换策略。

这里所说的转换策略,通常是指 请求参数 到 Map 的类型转换,如请求参数为 userName=a|12,b|34 这样的数据,需要通过一个转换策略(类型转换器)

来完成 a|12,b|34 到 map 的转换。在我们一般开发的过程中,不包含这种情况。是一种扩展。关于类型转换会在后面的文章中介绍。

(3)如果方法的入参是一个 Map 且指定了泛型类型 Map<String,String> 或者是 org.springframework.util.MultiValueMap 类型的 MultiValueMap<String, String>

并且没有指定请求参数,那么这个 Map 类型的参数会将所有的请求参数名称和值填充(populate)到其中。

如:

请求:<a href="testRequestParam4?userName=jack&age=23">test request param4</a>

handler 方法:

@RequestMapping("/testRequestParam4")
public String testRequestParam4(@RequestParam Map<String, String> map) {
  System.out.println("map:" + map);
  return "success";
}

控制台输出:

map:{userName=jack, age=23}

上面整体介绍了 @RequestParam,下面详细看看它的API:

SpringMVC核心——参数获取与Servlet资源获取问题

包含三个属性:

(1)value 属性,默认为 ""

官方文档说明:

The name of the request parameter to bind to.

解释的已经很明白了,不再赘述。

(2)required 属性,默认为 true

官方文档说明:Whether the parameter is required.

见名知意,该请求参数是否是必须的。为 true 的请求下,若请求参数中没有,则会抛出一个异常。为 false 的情况下,如果请求参数中没有,则方法入参对应值为 null。

另外,提供一个 defaultValue 属性,则会是此属性设置为 false

(3)defaultValue 属性

当没有提供对应的请求参数,或者请求参数为空时,会使用此属性对应的值。当设置此属性的时候,会将 required 属性设置为 false

下面提供几个常见请求情况的例子:

(1)请求为:<a href="testRequestParam?userName=abc">test request param</a> 

handler 方法:

@RequestMapping("/testRequestParam")
public String testRequstParam01(@RequestParam("userName") String userName) {
  System.out.println("userName: " + userName);  return "success";
}

(2)请求为:<a href="testRequestParam2?userName=jack&userName=lucy">test request param2</a>

handler 方法:

@RequestMapping("/testRequestParam2")
public String testRequestParam02(@RequestParam("userName") List<String> userNames) {
  System.out.println("userNames:" + userNames);
  return "success";
}

控制台输出:

userNames:[jack, lucy]

(3)请求为:<a href="testRequestParam4?userName=jack&age=23">test request param4</a>

handler 方法:

@RequestMapping("/testRequestParam4")
public String testRequestParam4(@RequestParam Map<String, String> map) {
  System.out.println("map:" + map);
  return "success";
}

控制台输出:

map:{userName=jack, age=23}

主要就分为这三种情况,其中第一种最为常用,第二种和第三种很少能想到,若能想到的话,能为我们开发节省不少时间。

3.@RequestHeader

官方文档中是这样描述的:

Annotation which indicates that a method parameter should be bound to a web request header.
Supported for annotated handler methods in Servlet and Portlet environments.

 

 

和 @RequestParam 描述类似,只不过绑定的是 web 请求头信息到方法入参。

SpringMVC核心——参数获取与Servlet资源获取问题

定义的三个属性和 @RequestParam 一样,默认值和使用的方法也一样。由于用的比较少,这里只做一个例子说明:

请求:<a href="testRequestHeader">test request header</a>

handler 方法:

@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value = "Accept", required = false) String accept) {
  System.out.println("accept:" + accept);
  return "success";
}

控制台输出:

accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

4.@CookieValue

官方文档描述:

Annotation which indicates that a method parameter should be bound to an HTTP cookie.
Supported for annotated handler methods in Servlet and Portlet environments.

 

 

绑定一个 http cookie 到方法的入参,其中 value 属性表明要入参的 cookie 的 key。

SpringMVC核心——参数获取与Servlet资源获取问题

默认值和使用方式和 @RequestParam 类似。

例子:

请求:<a href="testCookieValue">test cookie value</a>

handler 方法:

@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value = "JSESSIONID", required = false) String sessionId) {
  System.out.println("sessionId:"+sessionId);
  return "success";
}

控制台输出:

sessionId:9D16BDF7063E1BFD9A0C052F1B109A0D

5.绑定请求参数到方法入参处的 bean 对象。

先看两个例子:

(1)绑定请求参数到 bean 

请求:包括 get 和 post 请求方式提交的情况。

<a href="testBean?personName=jack&age=23">test bean</a>
<form action="testBean" method="post">
  <label>
    personName:<input type="text" name="personName"/>
  </label>
  <label>
    age:<input type="text" name="age"/>
  </label>
  <input type="submit" value="submit"/>
</form>

handler 方法:

@RequestMapping("/testBean")
public String testBean(Person person) {
  System.out.println(person);//Person{personName='jack', age='23'}
  return "success";
}

发现不论是通过 get 方式,还是post 方式,都可以将对应的请求参数注入到对应的 bean 中。

(2)绑定请求参数到级联的 bean

bean 的结构:

/**
 * @author solverpeng
 * @create 2016-08-04-9:43
 */
public class Employee {
    private String empName;
    private Address address;

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empName='" + empName + '\'' +
                ", address=" + address +
                '}';
    }
}
Employee

相关文章:

  • 2021-12-29
  • 2021-09-24
  • 2021-09-22
  • 2022-02-21
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2021-05-09
  • 2021-07-09
  • 2022-12-23
相关资源
相似解决方案