🌱SPRING
Spring @RequestMapping이란? 작성방법, 예시
jhj.sharon
2023. 4. 25. 20:11
반응형
1. @RequestMapping이란
- @RequestMapping은 클라이언트이 요청(url)에 맞는 클래스나 메서드를 연결시켜주는 어노테이션이다.
이 어노테이션은 그 위치에 따라 의미가 다르다. - 클래스 레벨 : 공통 주소
- 메서드 레벨 : 공통 주소 외 나머지 하위 주소
- 단, 클래스 레벨에 @RequestMapping이 없다면, 메서드 레벨은 단독 요청 처리 주소이다.
2.@RequestMapping 작성 방법
- @RequestMapping("url") : 요청방식(GET/POST)에 관계없이 url이 일치하는 요청을 처리한다.
- @RequestMapping(value = "url", method = RequestMethod.Get | POST) : 지정된 요청 방식에 따라 처리한다.
- 요청 방식에 관계없이 요청을 처리할 경우 메서드 레벨에서 GET/POST 방식을 구분하여 Mapping하여야 한다. @GetMapping('url') / @PostMapping('url')
3. @RequestMapping 작성 예시
@Controller
@RequestMapping("/member") //localhost:8080/comm/member 이하의 요청을 처리하는 컨트롤러
@SessionAttributes({"loginMember"})
public class MemberController {
private Logger logger = LoggerFactory.getLogger(MemberController.class);
@Autowired
private MemberService service;
@RequestMapping("/login") // 경로 : localhost:8080/comm/member/login
public String login(HttpServletRequest req) {
logger.info("로그인요청");
String inputEmail = req.getParameter("inputEmail");
String inputPw = req.getParameter("inputPw");
logger.debug("inputEmail::"+inputEmail);
logger.debug("inputPw::"+inputPw);
return "redirect:/";
}
}
반응형