@RequestMapping中有一个value属性用来映射url,如果不写的话,默认就是value属性
@RequestMapping({" ", " "})
@RequestMapping({"/test02_01","/test02_02"}) public ModelAndView test02(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test01"); modelAndView.setViewName("hello"); return modelAndView; }
@RequestMapping如果用在类上面,就表示这个类里面的所有响应请求都是以该地址作为父路径
@Controller @RequestMapping("/url") public class UrlController { @RequestMapping("/test01") public ModelAndView test01(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test01"); modelAndView.setViewName("hello"); return modelAndView; } @RequestMapping({"/test02_01","/test02_02"}) public ModelAndView test02(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test02"); modelAndView.setViewName("hello"); return modelAndView; } @RequestMapping("/test03") public ModelAndView test03(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test03"); modelAndView.setViewName("hello"); return modelAndView; } }
指定控制器的方法URI
@RequestMapping(value="/test01") public ModelAndView test01(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test01"); modelAndView.setViewName("hello"); return modelAndView; }
指定请求的method类型,可以接受GET,POST,PUT,DELETE等,默认是可以接收GET请求和POST请求
@RequestMapping(value = "/test01",method = RequestMethod.PUT) public ModelAndView test01(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test01"); modelAndView.setViewName("hello"); return modelAndView; }
params:指定request中必须包含某些参数值,才让该方法处理。
@RequestMapping(value = "/test04",params = "id") public ModelAndView test04(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("hello", "test04"); modelAndView.setViewName("hello"); return modelAndView; }
如果请求路径没有id则报错
而且params还可以指定请求中属性值必须为多少
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。