【转】SpringMVC注解说明

原文链接:http://blog.csdn.net/w40338544/article/details/6881464

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

@RequestMapping

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

@RequestParam

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

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

@RequestMapping(value="/{groupId}.do")  
public void detail(@PathVariable long groupId){  
    groupRepository.selectOne(groupId);  
}

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

@RequestMapping(value = "/xxxxx.do")  
public void create(@RequestHeader() MultiValueMap<String, String> host){  
    System.out.println("-----------" + host);  
}

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

@RequestMapping(value = "/xxx.do")  
@ResponseBody  
public MultiValueMap<String, String> create(@RequestHeader() MultiValueMap<String, String> hosts) throws Exception {  
    return hosts;  
}

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

@ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)  
@RequestMapping("/responsestatus")  
public void responseStatusTest(){  

}

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

@Controller  
@SessionAttributes( { "user" })  
@RequestMapping("/test")  
public class ControllerTest {  
  @RequestMapping("/session")  
  @ResponseBody  
  public String sessionIn(@ModelAttribute("user") User user) {  
  return "index";  
  }   
}

@CookieValue

@RequestMapping("/cookie")  
@ResponseBody  
public String cookie(@CookieValue("JSESSIONID") String sessionId) {  
return sessionId;  
}

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

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

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

@RequestMapping("/exception")  
 public void ExceptionTest() throws Exception{  
    throw new Exception("i don't know");  
 }    
 @ExceptionHandler  
 public String handleException(Exception e,HttpServletRequest request){  
    System.out.println(e.getMessage());  
    return "helloworld";  
 }


文/一木各(简书作者)
原文链接:http://www.jianshu.com/p/071b153c542a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值