欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC注解说明

程序员文章站 2022-07-15 13:24:29
...

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

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

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

 

@RequestBody

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

@RequestMapping(value = "/xxxxx.do")  
public void create(@RequestBody() String host){  
    System.out.println("-----------" + host);  
}  
@RequestMapping(value = "/xxxxx.do")
public void create(@RequestBody() String host){
	System.out.println("-----------" + host);
}
    1.  

@RequestHeader

 

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

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

 

	@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;  
}  
@RequestMapping(value = "/xxx.do")
@ResponseBody
public MultiValueMap<String, String> create(@RequestHeader() MultiValueMap<String, String> hosts) throws Exception {
	return hosts;
}
 @ResponseStatus
  1. 返回一个指定的http response状态码。
@ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)  
@RequestMapping("/responsestatus")  
public void responseStatusTest(){  
    
}  
@ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)
@RequestMapping("/responsestatus")
public void responseStatusTest(){
      
}

    1.  

 

@SessionAttributes

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

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

    1.  

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

 

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

   …     

}  

    1.  
 
   @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";  
 }  
  
   @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";
    }
    1.  

 

 

本文部分收集于其他博文。如有不全请大家补充,如有错误请指正。