对Spring MVC常用注解的理解

因为工作中经常用到的注解,特此写个博客,记录一下,方便自己和他人解决问题

  • NO.1 @requestMapping
@requestMapping(name/value = {"/vip/list","/svip/list"}, method = {requestMethod.GET,reqeustMethod.POST})
public String method_1(HttpServletRequest request, HttpServletResponse response) {
 return "index";   
}

key:name/value/(不写)都是可以的,作用是一样的

value:可以是一个数组,也可以是一个字符串,如果是字符串的话,只能访问这个请求,如果是数组的话,访问某个数组中的一个就可以

method表示访问该方法必须是什么类型,这里表示的是仅支持get和post请求,如果不写method属性的话,默认是8个请求类型都支持

  • NO.2 @requestParam
@requestMapping(name/value = {"/vip/list","/svip/list"}, method = {requestMethod.GET,reqeustMethod.POST})
public String method_2(@requestParam(name/value = "id",defaultValue = "1",required = true/false) Integer id,HttpServletRequest request, HttpServletResponse response) {
 return "index";   
}

key:defaultValue表示id没有的话,默认是对应的value中的值1

下班在更吧

猜你喜欢

转载自blog.csdn.net/lyw4631/article/details/81282762