zoukankan      html  css  js  c++  java
  • SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder

    • @Pathvariable
    public ResponseEntity<String> ordersBack(  
            @PathVariable String reqKey,  
            @RequestParam(value="intVal") Integer intVal,  
            @RequestParam(value="strVal") String strVal) throws Exception{  
        return new ResponseEntity("ok", HttpStatus.OK);  
    } 
      当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。这样就可以实现类似restful风格的请求。
    • @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用来指示参数是否必须绑定
    @Controller 
    @RequestMapping("/pets") 
    @SessionAttributes("pet") 
    publicclass EditPetForm { 
        @RequestMapping(method = RequestMethod.GET) 
        public String setupForm(@RequestParam("petId") int petId, ModelMap model) { 
            Pet pet = this.clinic.loadPet(petId); 
            model.addAttribute("pet", pet); 
            return"petForm"; 
        } 
    }
      其作用实际就是将url中?后面部分请求参数作为参数的一部分。
    • @RequestBody
      @RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象。POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将Json、Xml协议转换成你需要的对象。
    • @ResponseBody
      @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并调用适HttpMessageConverter的Adapter转换对象,写入输出流。表示该方法的返回结果直接写入HTTP response body中。
      一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。@ResponseBody可以标注任何对象,由Srping完成对象——协议的转换。
    @Controller  
    public class PersonController {  
      
        /** 
         * 查询个人信息 
         *  
         * @param id 
         * @return 
         */  
        @RequestMapping(value = "/person/profile/{id}/{name}/{status}", method = RequestMethod.GET)  
        public @ResponseBody  
        Person porfile(@PathVariable int id, @PathVariable String name,  
                @PathVariable boolean status) {  
            return new Person(id, name, status);  
        }  
      
        /** 
         * 登录 
         *  
         * @param person 
         * @return 
         */  
        @RequestMapping(value = "/person/login", method = RequestMethod.POST)  
        public @ResponseBody  
        Person login(@RequestBody Person person) {  
            return person;  
        }  
    }  

      @InitBinder

      表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 该标签是进行数据类型转换的,将string类型转为自定义的类型。

    @InitBinder  
        public void initBinder(WebDataBinder binder) {  
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
            dateFormat.setLenient(false);  
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
        }

     

  • 相关阅读:
    福利 | 简历模板大放送
    如何脱颖而出?成为优秀的人
    未来最重要的三个能力
    如何提升你的阅读能力?
    2016 Top 10 Android Library
    如何提升你的面试机会?
    推荐一些非常有用的学习网站
    谈谈学习方法
    你为什么还不够优秀?
    vue
  • 原文地址:https://www.cnblogs.com/lcngu/p/5734307.html
Copyright © 2011-2022 走看看