Java注解之@RequestParam

上节内容回顾:

1、针对@RequestMapping的学习,需要了解它的使用位置和功能,包含的方法。

位置用于类或方法之上,提供访问路径及参数的设置。另外包含的参数和方法有8中,name, value , path ,produces, consumes, method , headers, params.

针对每个参数的不同所起到的作用不同,详细内容参考:@RequestMapping

2、@RequestParam学习记录

@RequestParam也是一个接口,其中定义的接口方法value,name, required和defaultValue。

作用:将请求参数区数据映射到功能处理方法的参数上。

下面是该接口的源码。

2.1、源码:

package org.springframework.web.bind.annotation;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {

	/**
	 * Alias for {@link #name}.
	 */
	@AliasFor("name")
	String value() default "";

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

	boolean required() default true;

	String defaultValue() default ValueConstants.DEFAULT_NONE;
}

2.2、value() 和 name()

这两个方法的作用是一样的,和@RequestMapping中的valeu()和path()是一样,知识起了一个别名方法。

方法的作用:表示参数的名字,即入参的请求参数名字。

如下是xiaocui信息查询方法,其中包含的参数均是用@RequestParam实现的,默认的方法是value.

@RequestMapping(value = "/xiaocui/info",method = RequestMethod.GET)
@ResponseBody
public void XiaoCuiInfoQuery(@RequestParam String name, @RequestParam Integer age, @RequestParam String school){
  System.out.println("name:"+name+",age:"+age+",school:"+school);
}

在前台可以直接传入下面的参数等:

<input type="text" name="name" />
<input type="text" name="age" />

注:springMVC在使用过程中会根据参数名字来注入,因此,在使用Value时必须保证名字一致,不然入会注入。

拓展:如果和前台的参数不一致了,则处理方法如下所示:

@RequestMapping(value = "/xiaocui/info",method = RequestMethod.GET)
@ResponseBody
public void XiaoCuiInfoQuery(@RequestParam(name="xiaocuiname") String name, @RequestParam(name="xiaocuiage") Integer age, @RequestParam String school){
  System.out.println("name:"+name+",age:"+age+",school:"+school);
}

在前台需要传入的参数如下:

<input type="text" name="xiaocuiname" />
<input type="text" name="xiaocuiage" />

2.3、required()

作用:要求前端配置的前端参数是否一定要传。默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

如果设置为false,则在请求过程中可以忽略参数,不去考虑。

@RequestMapping(value = "/xiaocui/name",method = RequestMethod.GET)
@ResponseBody
public void XiaoCuiInfoQuery(@RequestParam(name="xiaocuiname",required=true) String name){
  System.out.println("name:"+name);
}

上述代码表示请求需要参数的传入。

另外,如果required设置为false,则默认为参数赋值为null。

2.4、defaultVale()

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,默认值可以是SpEL表达式,如“#{systemProperties['java.vm.version']}”。

 方法调用的是常量值ValueConstants.DEFAULT_NONE.

查看该常量的信息如下:

public interface ValueConstants {

	/**
	 * Constant defining a value for no default - as a replacement for
	 * {@code null} which we cannot use in annotation attributes.
	 * <p>This is an artificial arrangement of 16 unicode characters,
	 * with its sole purpose being to never match user-declared values.
	 * @see RequestParam#defaultValue()
	 * @see RequestHeader#defaultValue()
	 * @see CookieValue#defaultValue()
	 */
	String DEFAULT_NONE = "\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n";
}

注:其唯一目的是永远不匹配用户声明的值。

采用该方法后就不会有用户匹配到该值。(小概率事件)

发布了327 篇原创文章 · 获赞 133 · 访问量 63万+

猜你喜欢

转载自blog.csdn.net/qq_30507287/article/details/80891274