@requestParam注解

Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中。

Spring MVC 对控制器处理方法签名的限制是很宽松的,几乎可以按喜欢的任何方式对方法进行签名。

必要时可以对方法及方法入参标注相应的注解(@PathVariable、 @RequestParam、 @RequestHeader 等)、 SpringMVC 框架会将 HTTP 请求的信息绑定到相应的方法入参中,并根据方法的返回值类型做出相应的后续处理。

在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法

– value:参数名

– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常

 

控制器类和处理函数如下:

 
  1. package com.happyBKs.springmvc.handlers;

  2.  
  3. import org.springframework.stereotype.Controller;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestParam;

  6.  
  7. @RequestMapping("class")

  8. @Controller

  9. public class RPTestHandler {

  10.  
  11. String page="successrm";

  12.  
  13. @RequestMapping("student")

  14. public String handle(@RequestParam(value="username") String un, @RequestParam(value="age") int age)

  15. {

  16. System.out.println("a student's request has come. username: "+un+", age: "+age);

  17. return page;

  18. }

  19.  
  20.  
  21.  
  22. }

这里使用@RequestParam注解的value属性值来映射请求参数。

请求页面index8.jsp:

 
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

  2.     pageEncoding="ISO-8859-1"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

  7. <title>Insert title here</title>

  8. </head>

  9. <body>

  10. <a href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a>

  11. </body>

  12. </html>

好,运行过程如下:

@requestParam注解

点击超拦截请求:

@requestParam注解

控制台此时输出:

a student's request has come. username: happyBKs, age: 100

 

也可以直接在浏览器地址栏输入http://localhost:8080/mymvc/class/student?username=happyBKs&age=101

控制台显示   a student's request has come. username: happyBKs, age: 101

 

 

问题1:如果我们的请求少一个参数age,会怎么样?

@requestParam注解

这个问题该如何解决呢?

这里需要用到@RequestParam注解的required属性或defaultValue属性。

required属性标注这个参数是否是必需大的,默认是true,如果想让它可以不存在,那么就设置为false。但是请注意,required设置成false的参数对应的处理函数的参数类型必须是对象类型,否则回报错500!

例如这里我们如果将控制器类改成:

 
  1. package com.happyBKs.springmvc.handlers;

  2.  
  3. import org.springframework.stereotype.Controller;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestParam;

  6.  
  7. @RequestMapping("class")

  8. @Controller

  9. public class RPTestHandler {

  10.  
  11. String page="successrm";

  12.  
  13. @RequestMapping("student")

  14. public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age)

  15. {

  16. System.out.println("a student's request has come. username: "+un+", age: "+age);

  17. return page;

  18. }

  19.  
  20.  
  21.  
  22. }

结果会报错,因为age虽然设置了required为false,请求参数可以不包含age,但是在处理函数中对应的参数类型是int,int是基本数据类型,无法为空,所以报错。解决办法是将int改为Integer。

运行结果:

@requestParam注解

 

控制台输出:

a student's request has come. username: happyBKs, age: null

 

 

如果我们仍然想用基本类型作为参数类型,那么可以用到@RequestParam注解的defaultValue属性来指定默认值。

 
  1. package com.happyBKs.springmvc.handlers;

  2.  
  3. import org.springframework.stereotype.Controller;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestParam;

  6.  
  7. @RequestMapping("class")

  8. @Controller

  9. public class RPTestHandler {

  10.  
  11. String page="successrm";

  12.  
  13. @RequestMapping("student")

  14. public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age)

  15. {

  16. System.out.println("a student's request has come. username: "+un+", age: "+age);

  17. return page;

  18. }

  19.  
  20.  
  21.  
  22. }

运行结果:

@requestParam注解

 

控制台 a student's request has come. username: happyBKs, age: 0

 

最后,做个总结:(@RequestParam注解是很常用的,所以很重要)

 * @RequestParam 来映射请求参数

 * value值 即请求参数名

 * required 该参数是否必需。默认为true

 * defaultValue请求参数的默认值