RequestMethod는 HTTP 메서드를 정의한 ENUM이다.GET, POST, PUT, DELETE, OPTIONS, TRACE로 총 7개의 HTTP 메서드가 정의되어 있다.
@RequestMapping에 method를 명시하면 똑같은 URL이라도 다른 메서드로 매핑해줄 수 있다.
@RequestMapping(value="/aaa", method=RequestMethod.GET)
@RequestMapping(value="/aaa", method=RequestMethod.POST)
기본 형태
//spring 4.3
// @GetMapping("/test/login.do") @PostMapping("/test/login.do")
@RequestMapping(value = "/test/login.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
ModelAndView mav = new ModelAndView();
mav.setViewName("result");
String userID = request.getParameter("userID");
String userName = request.getParameter("userName");
mav.addObject("userID", userID);
mav.addObject("userName", userName);
return mav;
}
2)축약형
// @GetMapping("/test/login.do") @PostMapping("/test/login.do")
@RequestMapping(value = "/test/login.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
return mav;
}