zoukankan      html  css  js  c++  java
  • java

    1、 @PathVariable 

    当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

    示例代码:

    1. @Controller  
    2. @RequestMapping("/owners/{ownerId}")  
    3. public class RelativePathUriTemplateController {  
    4.   
    5.   @RequestMapping("/pets/{petId}")  
    6.   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    7.     // implementation omitted  
    8.   }  

    3、@RequestParam, @RequestBody

    @RequestParam 

    A) 常用来处理简单类型的绑定,通过 Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所 以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

    B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;

    C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;

    示例代码:

    1. @Controller  
    2. @RequestMapping("/pets")  
    3. @SessionAttributes("pet")  
    4. public class EditPetForm {  
    5.   
    6.     // ...  
    7.   
    8.     @RequestMapping(method = RequestMethod.GET)  
    9.     public String setupForm(@RequestParam("petId") int petId, ModelMap model) {  
    10.         Pet pet = this.clinic.loadPet(petId);  
    11.         model.addAttribute("pet", pet);  
    12.         return "petForm";  
    13.     }  
    14.   
    15.     // ...  

    @RequestBody

    该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;

    它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。

    因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageConverter api;

    示例代码:

    1. @RequestMapping(value = "/something", method = RequestMethod.PUT)  
    2. public void handle(@RequestBody String body, Writer writer) throws IOException {  
    3.   writer.write(body);  
    4. }
  • 相关阅读:
    【英语天天读】Feed Your Mind
    【IBM Tivoli Identity Manager 学习文档】4 TIM基本概念
    【英语天天读】Dreams
    【嵌入式开发技术之串口】Linux下串口主机程序
    【IBM Tivoli Identity Manager 学习文档】3 系统部署
    【IBM Tivoli Identity Manager 学习文档】2 部署准备知识
    【Linux开发技术之常见问题】一个建立线程时常见的问题:invalid conversion from `void*' to `void*(*)(void*)
    C# winform combobox 在绑定数据之后插入一项选择项
    Response.Redirect在新窗口打开 && 3.0扩展方法
    sql字符转换函数大全
  • 原文地址:https://www.cnblogs.com/ycmxm/p/7777585.html
Copyright © 2011-2022 走看看