GVKun编程网logo

@RequestParam与@PathVariable

15

对于想了解@RequestParam与@PathVariable的读者,本文将提供新的信息,并且为您提供关于@CookieValue,@PathVariable,@RequestBody,@Reque

对于想了解@RequestParam与@PathVariable的读者,本文将提供新的信息,并且为您提供关于@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam、@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明、@PathVariable 注解的使用和 @Requestparam、@PathVariable与@RequestParam的区别的有价值信息。

本文目录一览:

@RequestParam与@PathVariable

@RequestParam与@PathVariable

@RequestParam

是映射表单或者请求参数中的值

表单:

	<form action="rm/testRequestParams" method="post">
		username:<input type="text" name="username" /><br>
		email:<input type="text" name="email" /><br>
		age:<input type="text" name="age" /><br>
		city:<input type="text" name="address.city" /><br>
		province:<input type="text" name="address.province" />
		<input type="submit" value="submit" />
	</form>

url: 

<a href="rm/testRequestParams?username=jionsvolk&password=123abc">testRequestParams</a>
	@RequestMapping(value = "testRequestParams" , method = {RequestMethod.GET})
	public String testRequestParams(@RequestParam(value = "username") String username,
			@RequestParam(value = "password",required = false,defaultValue = "abc123") String password){
		System.out.println("testRequestParams username:" + username+",password:" + password);
		return SUCCESS;
	}

– value:参数名

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

– defaultValue:默认值

@RequestHeader @CookieValue注解使用方式同@RequestParam,且都使用在方法参数上,@RequestHeader得到请求头,可以使用chrome等浏览器打开开发者工具找到对应的请求头,@CookieValue得到Cookie值,可以使用chrome等浏览器打开开发者工具找到对应Cookie名

 

@PathVariable

用于将请求路径中占位符对应的值映射到方法参数上

该注解是spring实现restful的关键之一

<a href="rm/testPathVariable/lucy">test pathVariable</a>

其中lucy是占位符对应的值

	@RequestMapping("/testPathVariable/{name}")
	public String testPathVariable(@PathVariable("name") String name){
		System.out.println("testPathVariable:"+name);
		return SUCCESS;
	}

运行结果如下:

@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

原文链接:http://blog.sina.com.cn/s/blog_6d3c1ec601017q4l.html


下列参数一般都和@RequestMapping配合使用。

 

A@CookieValue

org.springframework.web.bind.annotation.CookieValue

public @interface CookieValue

Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释表示一个方法参数绑定到一个HTTP cookie。支持ServletPortlet环境。

The method parameter may be declared as type Cookie or as cookie value type (String,int,etc).

这个方法的参数可声明为Cookie类型或String,int等。

A.1@CookieValue的属性

String value

The name of the cookie to bind to.

绑定的cookie名称。

boolean required

Whether the header is required.

Default is true,leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.

Head是否需要。默认是true,请求中头丢失将抛出一个异常。False,请求中头丢失将返回null

Alternatively,provide a defaultValue,which implicitly sets this flag to false.

因此,提供一个defaultValue

String defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

requiredfalse,请求中头丢失将返回这个值。

B@PathVariable

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.

    这个参数指出方法的一个参数绑定到一个URI template变量。在Servlet环境中的被@RequestMapping注释的处理器方法。

B.1@PathVariable的属性

value

The URI template variable to bind to.

绑定URI template变量。

举例说明

@Controller

public class HelloWorldController {    @RequestMapping("/helloWorld/{userId}")

public String helloWorld(ModelMap model,@PathVariable("userId") String userId) {

       model.addAttribute("attributeName",userId);

       return "helloWorld";

    }

}

URI template变量和方法的参数名称一样时,可以省略value的定义,@PathVariable达到同样的效果。

 

C@RequestBody

Annotation which indicates that a method parameter should be bound to the web request body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的body。它支持Servlet环境中的注释处理器方法。

class HelloWorldController {

    "/hello.do")   

    public String helloWorld(Model model,100);font-size:10pt;">@RequestBody String reqBody) {

"message",reqBody);

    }

}

这时这个参数reqBody的值是请求页面的form表单的所有值。

 

D@ RequestHeader

Annotation which indicates that a method parameter should be bound to a web request header. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的头信息。它支持ServletPortlet环境中的注释处理器方法。

D.1@ RequestHeader的属性

String defaultValue

The default value to use as a fallback.

默认返回值。

Boolean required

Whether the header is required.

是否需要header

String value

The name of the request header to bind to.

绑定的请求头名称。

@RequestHeader("Accept") String info) {

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70, info);

这时这个参数info将获得请求的Accept头信息。

E@RequestParam

org.springframework.web.bind.annotation.RequestParam

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.

这个参数指出一个方法的参数应绑定到一个web请求的参数。支持ServletPortlet环境下注释处理器的方法。

E.1@RequestParam的属性

E.1.1value

The name of the request parameter to bind to.

绑定的请求参数的名称。

@RequestParam(value="abc")等同于@RequestParam("abc")

E.1.2required

Whether the parameter is required.

是否需要参数。

Default is true,leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.

默认为true,若请求中没有参数会导致抛出一个异常。若设置为false,若请求中没有参数就会返回null

Alternatively,which implicitly sets this flag to false.

required=false时,最好设置一个defaultValue默认值。

@RequestParam(value = "abc",required=false)

E.1.3defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

required=false时,设定默认值。

"/a")

"/b")

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70,@RequestParam("a") String abc) {

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70,abc);

 

F@ResponseBody

Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的返回值应该绑定到一个web响应的body中。它支持Servlet环境中的注释处理器方法。

应用@ResponseBody将会跳过视图处理,而是调用合适HttpMessageConverter,将返回值写入输出流。

@ResponseBody

public String helloWorld() {

或者这样定义

"/a/b")

public @ResponseBody String helloWorld() {

这时访问/a/b时,不是返回一个view名为helloWorld的视图,而是作出一个响应,其内容为helloWorld


@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明

@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明

@Param主要是用来注解dao类中方法的参数,在不使用@Param注解的时候,函数的参数只能为一个,并且在查询语句取值时只能用#{},且其所属的类必须为Javabean,而使用@Param注解则可以使用多个参数,在查询语句中使用时可以使用#{}或者${}

 

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上,http://127.0.0.1:8040/findById/1-->>@GetMapping("/findById/{id}")      参数不能为空

 

@RequestParam注解主要有哪些参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

 

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

 

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

 

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

 

@ResponseBody将响应的结果转为json格式

 

@RequestBody将请求参数转为json格式

@PathVariable 注解的使用和 @Requestparam

@PathVariable 注解的使用和 @Requestparam

一、 @PathVariable

 @PathVariable 这是一个路径映射格式的书写方式注解,在类映射路径的后加上 /{对应方法参数中属性 @PathVariable ("code") 中的 code},

 

 

@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(value = "/decodeUserInfo/{codee}", method = RequestMethod.GET)
@ResponseBody
public Map decodeUserInfo(@PathVariable("codee") String codee) {

Map map = new HashMap();

/////////////////////////////////////////////////////////////////////////自己定义的code

String code = codee;

 

 

 

 

 二、@Requestparam 注解将请求参数绑定至方法参数即你可以使用 @RequestParam 注解将请求参数绑定到你控制器的方法参数上

1.value: 请求参数名 (必须配置)

2.required: 是否必须,默认 true,即请求中必须包含该参数,如果没有包含,将会抛出异常 (可选配置)

3.defaultValue: 默认值,如果设置了该值,require 将自动设为 false,无论你是否配置了 required, 配置了什么值,required 将自动设为 false

 

@Requestparam (value="表示参数名字",require=boolean 类型表示是否为必须,defaultValue=“表示默认值”)

 

 

@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(value = "/decodeUserInfo", method = RequestMethod.GET)
@ResponseBody
public Map decodeUserInfo(@RequestParam String codee) {

Map map = new HashMap();

/////////////////////////////////////////////////////////////////////////自己定义的code

String code = codee;

 

 

@PathVariable与@RequestParam的区别

@PathVariable与@RequestParam的区别

@PathVariable与@RequestParam的区别
首先呢这二个注解都是接收参数使用的,下面来看一下它们的区别。
@PathVariable注解
@RequestMapping(value ={“/hello/{id}”,”{id}/hi”},method = RequestMethod.GET)
//@GetMapping(“/hello/{id}”)
public String hello(@PathVariable(“id”) Integer id){
return “id:”+id;
}
地址栏参数如下,后面直接跟id值就可以。
http://localhost:8888/hello/66

@RequestParam注解
@RequestMapping(value ={“/hello/{id}”,”{id}/hi”},method = RequestMethod.GET)
//@GetMapping(“/hello/{id}”)
public String hello(@RequsetParam(value=”id”,required=false,defaultValue=”0”) Integer id){

  1. return "id:"+id;
  2. //return "index";
  3. }

http://localhost:8888/hello?id=66
以上就是二个注解传参的区别,希望对大家有帮助。
@RequestMapping可以换成@GetMapping,大家可以了解一下。

我们今天的关于@RequestParam与@PathVariable的分享就到这里,谢谢您的阅读,如果想了解更多关于@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam、@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明、@PathVariable 注解的使用和 @Requestparam、@PathVariable与@RequestParam的区别的相关信息,可以在本站进行搜索。

本文标签: