• SpringMVC注解说明


    @controller

    通过@controller标注即可将class定义为一个controller类。

    @RequestMapping

    value 表示需要匹配的url的格式。
    method 表示所需处理请求的http 协议(如get,post,put,delete等),可选值为RequestMethod这个enum的值。
    params 格式为”paramname=paramvalue” 或 “paramname!=paramvalue”。 表示参数必须等于某值,或者不等于才进入此映射方法。不填写的时候表明不限制
    headers 用来限定对应的reqeust请求的headers中必须包括的内容,例如headers={"Connection=keep-alive"}, 表示请求头中的connection的值必须为keep-alive。

    @RequestParam

    value 对应表单name空间的值
    required 是否允许为空
    defaultValue 默认值

    @PathVariable

    获得地址栏中传的参数 例如:

    [java] view plain copy
     
     print?
    1. @RequestMapping(value="/{groupId}.do")  
    2. public void detail(@PathVariable long groupId){  
    3.     groupRepository.selectOne(groupId);  
    4. }  

    @RequestBody

    在参数之前加入@RequestBody注解。用来将指定的客户端发送过来的请求参数的数据格式转换成java实体

    [java] view plain copy
     
     print?
    1. @RequestMapping(value = "/xxxxx.do")  
    2. public void create(@RequestBody() String host){  
    3.     System.out.println("-----------" + host);  
    4. }  


    @RequestHeader

    在参数之前加入@RequestHeader注解。用来将指定的请求头信息影射为方法的参数。

    [java] view plain copy
     
     print?
    1. @RequestMapping(value = "/xxxxx.do")  
    2. public void create(@RequestHeader() MultiValueMap<String, String> host){  
    3.     System.out.println("-----------" + host);  
    4. }  

    @ResponseBody

    如果这个方法定义了@ResponseBody注解。那么会把返回值转换成这个数据格式,输出给客户端

    [java] view plain copy
     
     print?
    1. @RequestMapping(value = "/xxx.do")  
    2. @ResponseBody  
    3. public MultiValueMap<String, String> create(@RequestHeader() MultiValueMap<String, String> hosts) throws Exception {  
    4.     return hosts;  
    5. }  

    @ResponseStatus
    返回一个指定的http response状态码。

    [java] view plain copy
     
     print?
    1. @ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)  
    2. @RequestMapping("/responsestatus")  
    3. public void responseStatusTest(){  
    4.     
    5. }  



    @SessionAttributes

    写在类级别的注解,定义一个session attributes,属性名字为SessionAttributes指定。可以指定多个(数组),也同时可以指定类型。

    [java] view plain copy
     
     print?
    1. @Controller  
    2. @SessionAttributes( { "user" })  
    3. @RequestMapping("/test")  
    4. public class ControllerTest {  
    5.   @RequestMapping("/session")  
    6.   @ResponseBody  
    7.   public String sessionIn(@ModelAttribute("user") User user) {  
    8.   return "index";  
    9.   }   
    10. }  
     


    @CookieValue

    [java] view plain copy
     
     print?
    1. @RequestMapping("/cookie")  
    2. @ResponseBody  
    3. public String cookie(@CookieValue("JSESSIONID") String sessionId) {  
    4. return sessionId;  
    5. }  



    @InitBinder 

    在controller中注册一个customer protperty editor以解析request中的参数并通过date bind机制与handler method中的参数做绑定。

    [java] view plain copy
     
     print?
    1. @InitBinder  
    2. public void initBinder(WebDataBinder binder) {  
    3.    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
    4.    dateFormat.setLenient(false);  
    5.    binder.registerCustomEditor(Date.class, new CustomDateEditor(  
    6.           dateFormat, false));  
    7. }  


    [java] view plain copy
     
     print?
    1. @RequestMapping("/databind1")  
    2.  public ModelAndView databind1(Date date) {  
    3.    …     
    4. }  


    访问url http://localhost:8080/springmvc/databind1.action?date=2000-01-02
    通过initbinder中注册的customeDateEditor类型,自动将2000-01-02转换为日期类型

    @ExceptionHandler

    [java] view plain copy
     
     print?
      1. @RequestMapping("/exception")  
      2.  public void ExceptionTest() throws Exception{  
      3.     throw new Exception("i don't know");  
      4.  }    
      5.  @ExceptionHandler  
      6.  public String handleException(Exception e,HttpServletRequest request){  
      7.     System.out.println(e.getMessage());  
      8.     return "helloworld";  
      9.  }  
  • 相关阅读:
    HDU 1754 I Hate It (线段树)
    HDU 1394 Minimum Inversion Number (树状数组)
    天梯赛 L2-012 关于堆的判断 (二叉树)
    HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
    [leetcode-77-Combinations]
    [leetcode-101-Symmetric Tree]
    [leetcode-21-Merge Two Sorted Lists]
    [leetcode-109-Convert Sorted List to Binary Search Tree]
    [leetcode-507-Perfect Number]
    [leetcode-537-Complex Number Multiplication]
  • 原文地址:https://www.cnblogs.com/Feiyang-Lafe/p/5667180.html
Copyright © 2020-2023  润新知