springMVC笔记系列(8)——RequestParam注解

说明:本文章的内容转载至:https://my.oschina.net/happyBKs/blog/417032
如有侵权的地方,请联系本人,本人将会立即删除!

摘要: 前面的文章介绍过注解@PathVariable,它能够为Rest风格的URL用占位符的方式传递一个参数,但是这个参数并不是真正意义上的请求参数。请求参数怎么处理是本文的主要内容。

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

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

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

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

– value:参数名

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

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

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {

    String page="successrm";

    @RequestMapping("student")
    public String handle(@RequestParam(value="username") String un, @RequestParam(value="age") int age)
    {
        System.out.println("a student's request has come. username: "+un+", age: "+age);
        return page;
    }
}

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

请求页面index8.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a>
</body>
</html>

好,运行过程如下:
这里写图片描述
点击超拦截请求:
这里写图片描述

控制台此时输出:

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注解的required属性或defaultValue属性。

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

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

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {

    String page="successrm";

    @RequestMapping("student")
    public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age)
    {
        System.out.println("a student's request has come. username: "+un+", age: "+age);
        return page;
    }
}

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

运行结果:
这里写图片描述

控制台输出:

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

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

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {

    String page="successrm";

    @RequestMapping("student")
    public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age)
    {
        System.out.println("a student's request has come. username: "+un+", age: "+age);
        return page;
    }
}

运行结果:
这里写图片描述

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

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

  • @RequestParam 来映射请求参数

  • value值 即请求参数名

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

  • defaultValue请求参数的默认值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值