??@RequestParam
用于将Web请求参数数据映射到控制器处理方法的参数中,支持Servlet
和Portlet
环境中的带注解的处理方法。
??注解解析
??① value
:
????绑定的参数名称,参数值为String
类型。
??② name
:
????绑定的参数名称,参数值为String
类型。name
和value
可以同时使用,但两者的值需一致,否则会出现错误。
attribute 'name' and its alias 'value' are present with values of [XXX] and [XXX], but only one is permitted
1
??③ required
:
????请求参数中是否必须包含指定的值,默认值为true
。
????required
为true
时,如果请求参数中缺少指定的值,则会抛出异常。
????required
为false
时,如果请求参数中缺少指定的值,则会返回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
来接收参数。
??若文中存在错误和不足,欢迎指正!