SpringMVC_PathVariable、REST、@RequestParam、

@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用来删除资源

- 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
 * 获取:/order/1 GET
 * 删除:/order/1 DELETE
 *
 * 如何发送 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;
}

@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;
}

猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/80637118