SpringMVC(六):@RequestParam注解
SpringMVC(六):@RequestParam注解
LevelCoder 发表于6个月前
SpringMVC(六):@RequestParam注解
  • 发表于 6个月前
  • 阅读 14
  • 收藏 0
  • 点赞 0
  • 评论 0

@RequestParam 映射请求方法参数绑定到指定web请求参数

@RequestParam源码 3中方法

value():值即请求参数的参数名

required():当前请求参数是否必须:默认true

defaultValue():请求参数默认值

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

/**
 * 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.
 *
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 2.5
 * @see RequestMapping
 * @see RequestHeader
 * @see CookieValue
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
 * @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
 * @see org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {

	/**
	 * The name of the request parameter to bind to.
	 */
	String value() default "";

	/**
	 * Whether the parameter is required.
	 * <p>Default is {@code true}, leading to an exception thrown in case
	 * of the parameter missing in the request. Switch this to {@code false}
	 * if you prefer a {@code null} in case of the parameter missing.
	 * <p>Alternatively, provide a {@link #defaultValue() defaultValue},
	 * which implicitly sets this flag to {@code false}.
	 */
	boolean required() default true;

	/**
	 * The default value to use as a fallback when the request parameter value
	 * is not provided or empty. Supplying a default value implicitly sets
	 * {@link #required()} to false.
	 */
	String defaultValue() default ValueConstants.DEFAULT_NONE;

}

针对三种方法做不同测试

控制层

package com.levelcoder;

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

/**
 * 
 * 描述:@RequestParam注解
 *
 * 作者:LevelCoder
 *
 * 邮箱:LevelCoder@126.com
 *
 * 日期:2017年6月5日 下午2:16:25
 *
 * 版本:V1.0.0
 */
@Controller
public class RequestParamController {

	/**
	 * @RequestParam value() 当前参数名称
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/requestParams")
	public String requestParams(@RequestParam("username") String username){
		System.out.println("参数值:"+username);
		return "success";
	}
	
	/**
	 * 
	 * @RequestParam required() 表示当前参数是否为必须 true:必须 false:非必须
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/requestParamsRequired")
	public String requestParamsRequired(@RequestParam(value="username",required = false) String username){
		System.out.println("参数值:"+username);
		return "success";
	}
	
	/**
	 * @RequestParam defaultValue() 表示当前参数 为空时赋予一个默认值
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/requestParamsDefaultValue")
	public String requestParamsDefaultValue(@RequestParam(value="username",required = false,defaultValue="test") String username){
		System.out.println("参数值:"+username);
		return "success";
	}
}

 

测试index页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p>@RequestParam 注解</p>
	
	<a href="requestParams?username=levelcoder">value</a>
	<a href="requestParamsRequired">required</a>
	<a href="requestParamsDefaultValue">defaultValue</a>
	
</body>
</html>

 

测试结果页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Spring MVC : RequestParam</title>
</head>
<body>
	恭喜LevelCoder:RequestParam 程序跑通了,继续学习吧
</body>
</html>

 

测试结果

 

至此@RequestParam介绍完毕 

demos地址:http://git.oschina.net/LevelCoder/demos 

 

标签: SpringMVC
共有 人打赏支持
粉丝 8
博文 54
码字总数 23491
×
LevelCoder
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: