SpringMVC_@PathVariable、REST、@RequestParam、

一、@PathVariable

1、@PathVariable 映射URL绑定的占位符

  •  带有占位符的URL是Spring 3.0新增的功能,该功能在SpringMVC向REST目标挺近发展的过程中具有里程碑的意义。
  •  通过 @PathVariable 可以将URL中占位符参数绑定到控制器处理方法的入参中:URL中的 {xxx} 占位符可以通过 @PathVariable("xxx")绑定到操作方法的入参中。

 

/**
 * @PathVariable 可以来映射URL中的占位符到目标方法的参数中
 * @param id
 * @return
 */
@RequestMapping("testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") int id){
    System.out.println("testPathVariable "+id);
    return SUCCESS;
}

二、REST

REST:Representational Statement Transfer,资源表现层状态转化,是目前最流行的一种互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便,所以得到越来越多的网站的采用。

  •  资源:网络上的一个实体,或者说是网络上一个具体的信息,每种资源都有一个特定的URL,因此URL成为每个资源独一无二的识别符。
  •  表现层:把资源具体呈现出来的形式,叫做它的表现层
  •  状态转化:每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议是一个无状态协议,即所有状态都保存在服务器端。因此,如果客户端需要操作服务器,必须通过某种手段,让服务器端发生“状态转化”,而这种转化是建立在表现层之上的,所以就是“表现层状态转化”具体说就是HTTP协议中,四个表示操作的动调:GET、POST、PUT、DELETE,它们分别代表了四中基本操作GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源
    如:
    — /order/1 HTTP GET:得到id=1的order
    — /order/1 HTTP DELETE:删除id=1的order
    — /order/1 HTTP PUT:更新id=1的order
    — /order/1 HTTP POST:新增id=1的order
  •  HiddenHttpMethodFilter:浏览器form表单只支持GET和POST请求,而DELETE、PUT等method并不支持,Spring3.0添加了一个过滤器,可以将这些请求转化为标准的http方法,使得支持GET、POST、DELETE和PUT。

在web.xml中配置:

 

<!--配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把 POST 请求转化为 DELTE 或 POST 请求-->
<filter>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <!--过滤所有请求-->
  <url-pattern>/*</url-pattern>
</filter-mapping>
/**
 * REST风格的URL
 * 以CRUD为例:                以前写法:
 * 新增:/order POST         
 * 修改:/order/1  PUT        update?id=1
 * 获取:/order/1 GET         get?id=1
 * 删除:/order/1 DELETE      delete?id=1
 *
 * 如何发送 DELETE请求和 PUT请求
 * 1、需要配置HiddenHttpMethodFilter
 * 2、需要发送post请求
 * 3、需要在发送post请求时,携带一个name="_method" 的隐藏域,值为DELETE或PUT
 *
 * 在SpringMVC的目标方法中如何得到id拉?
 * 使用@PathVariable注解
 *
 * @param id
 * @return
 */
@RequestMapping(value = "/testREST/{id}",method = RequestMethod.PUT)
@ResponseBody
public String testRestPut(@PathVariable("id")Integer id){
    System.out.println("testRest PUT "+id);
    return SUCCESS;
}

@RequestMapping(value = "/testREST/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String testRestDelete(@PathVariable("id")Integer id){
    System.out.println("testRest DELETE "+id);
    return SUCCESS;
}

@RequestMapping(value = "/testREST",method = RequestMethod.POST)
public String testRest(){
    System.out.println("testRest POST ");
    return SUCCESS;
}

@RequestMapping(value = "/testREST/{id}",method = RequestMethod.GET)
public String testRest(@PathVariable("id")Integer id){
    System.out.println("testRest GET "+id);
    return SUCCESS;
}
<form action="/testRestPost/11" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit" value="post">
</form>

三、@RequestParam

请求处理方法签名:

- SpringMVC通过分析处理方法的签名,将HTTP请求信息绑定到处理方法相应的入参中。

- SpringMVC对控制器处理方法签名的限制是很宽松的,几乎可以按自己喜欢的任何方式进行签名

- 必要时可以对方法及方法入参标注相应的注解(@PathVariable,@RequestParam、@RequestHeader等),SpringMVC框架会将HTTP请求的信息绑定到相应的方法入参中,并根据方法的返回值作出相应的后续处理。

使用@RequestParam绑定请求参数值:

在处理方法入参处,使用@RequestParam可以把请求参数传递给请求方法

    —value:参数名

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

 

/**
 * @RequestParam来映射请求参数,其中:
 * value:请求参数的参数名
 * required:该参数是否必须,默认为true
 * defaultValue:请求参数的默认值
 * @param un
 * @param age
 * @return
 */
@RequestMapping(value = "/testRequestParam")
public String testRequestParam(@RequestParam(value = "username",required = true)String un,
                               @RequestParam(value = "age",required = false)Integer age){
    System.out.println("testRequestParam,username "+un+",age "+age);
    return SUCCESS;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值