当前位置: 代码迷 >> 综合 >> Spring 注解面面通 之 @RequestParam
  详细解决方案

Spring 注解面面通 之 @RequestParam

热度:48   发布时间:2024-01-17 01:02:58.0

??@RequestParam用于将Web请求参数数据映射到控制器处理方法的参数中,支持ServletPortlet环境中的带注解的处理方法。

??注解解析

??① value:

????绑定的参数名称,参数值为String类型。

??② name:

????绑定的参数名称,参数值为String类型。namevalue可以同时使用,但两者的值需一致,否则会出现错误。

attribute 'name' and its alias 'value' are present with values of [XXX] and [XXX], but only one is permitted
1

??③ required:

????请求参数中是否必须包含指定的值,默认值为true

????requiredtrue时,如果请求参数中缺少指定的值,则会抛出异常。

????requiredfalse时,如果请求参数中缺少指定的值,则会返回null

??④ defaultValue

????请求参数绑定失败时的默认值,指定默认值后,会隐式的将required设置为false

??注解示例

??1)Controller,用来演示@RequestHeader使用方法。

package com.arhorchin.securitit.webannotations;import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSON;/*** @author Securitit.* @note 演示@RequestParam使用方法.*/
@Controller
@RequestMapping("/WebAnnotations")
public class RequestParamController {
    /*** logger.*/private Logger logger = LoggerFactory.getLogger(RequestParamController.class);/*** 跳转页面.*/@RequestMapping(value = "/RequestParam.html",method = RequestMethod.GET)public ModelAndView requestParamHtml(HttpServletRequest req, HttpServletResponse res, ModelMap modelMap)throws Exception {
    return new ModelAndView("webannotations/RequestParam", modelMap);}/*** 从请求参数中以Map解析方法参数.*/@ResponseBody@RequestMapping(value = "/RequestParamMap.do",method = RequestMethod.GET)public String requestParamMap(@RequestParam Map<String, String> requestParamMap) throws Exception {
    logger.info("Current value of RequestParam is " + JSON.toJSONString(requestParamMap));return "Current value of RequestParam is " + JSON.toJSONString(requestParamMap);}/*** 从请求参数中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = "/RequestParamSingle.do",method = RequestMethod.GET)public String requestParamSingle(@RequestParam(value = "Province") String requestParamProvince) throws Exception {
    logger.info("Current value of RequestParam [Province] is " + requestParamProvince);return "Current value of RequestParam [Province] is " + requestParamProvince;}/*** 从请求参数中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = "/RequestParamSingleRequiredTrue.do",method = RequestMethod.GET)public String requestParamSingleRequiredTrue(@RequestParam(value = "TmpParam",required = true) String requestParamTmpParam) throws Exception {
    logger.info("Current value of RequestParam [TmpParam] is " + requestParamTmpParam);return "Current value of RequestParam [TmpParam] is " + requestParamTmpParam;}/*** 从请求参数中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = "/RequestParamSingleRequiredFalse.do",method = RequestMethod.GET)public String requestParamSingleRequiredFalse(@RequestParam(value = "TmpParam",required = false) String requestParamTmpParam) throws Exception {
    logger.info("Current value of RequestParam [TmpParam] is " + requestParamTmpParam);return "Current value of RequestParam [TmpParam] is " + requestParamTmpParam;}}

??2)html,用来演示RequestParamController示例。

<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="Author" content="Securitit"><meta name="Keywords" content="-"><meta name="Description" content="Securitit @RequestParam 测试页面"><title>@RequestParam 测试页面</title></head><body><a href="http://localhost:9199/spring-annotations/WebAnnotations/RequestParamMap.do?Province=LiNing&City=DaLian" target="_blank">点击链接,@RequestParam进行参数绑定,后端以Map解析方法参数.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/RequestParamSingle.do?Province=LiNing&City=DaLian" target="_blank">点击链接,@RequestParam进行参数绑定,以单键值形式解析方法参数.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/RequestParamSingleRequiredTrue.do?Province=LiNing&City=DaLian" target="_blank">点击链接,@RequestParam进行参数绑定,后端指定required=true,以单键值形式解析方法参数.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/RequestParamSingleRequiredFalse.do?Province=LiNing&City=DaLian" target="_blank">点击链接,@RequestParam进行参数绑定,后端指定required=true,以单键值形式解析方法参数.</a></body>
</html>

??3) 启动服务,访问http://localhost:9199/spring-annotations/WebAnnotations/RequestParam.html页面。

在这里插入图片描述

??分别点击页面中的链接,查看@RequestParam对应的效果。

??① 点击链接,@RequestParam进行参数绑定,后端以Map解析方法参数.

在这里插入图片描述

??② 点击链接,@RequestParam进行参数绑定,以单键值形式解析方法参数.

在这里插入图片描述

??③ 点击链接,@RequestParam进行参数绑定,后端指定required=true,以单键值形式解析方法参数.

在这里插入图片描述

??④ 点击链接,@RequestParam进行参数绑定,后端指定required=true,以单键值形式解析方法参数.

在这里插入图片描述

??总结

??@RequestParam是应用频率相当高的注解,一般应用中都会在URL中传输参数,都可以使用@RequestParam来接收参数。

??若文中存在错误和不足,欢迎指正!

  相关解决方案