스프링(Spring) 공부
RequestMapping
Leezu_
2021. 1. 8. 16:23
RequestMapping : 컨트롤러에 URL을 매핑해주는 Annotation
예시.
@Controller
public class HomeController {
@RequestMapping("/index")
public String Index(){
//tiles를 통해 resolving이 가능
return "root.index"
}
}
/index로 요청이 들어오면 tiles로 resolving된 화면이 출력됨.
RequestMapping을 쓰기 위해서 아래 두 줄을 .xml 파일에 추가해준다.
<!-- com.newlecture.web.controller를 스캔하고 Annotaition 확인 후 Bean 인스턴스로 생성 -->
<context:component-scan base-package="com.newlecture.web.controller" />
<!-- 매핑을 통해 사용자의 요청을 수반, spring mvc 컴포넌트를 활성화 -->
<mvc:annotation-driven />
물론 이를 사용하기 위해 아래와같이 추가해야하는 구문이 있다.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
<!-- 아래서부터 새로 추가해야하는 구문 -->
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
추가적으로 공통된 부분은 클래스를 RequestMapping해줌으로 코드를 간결화시킬 수 있다.