Controller 클래스 수행후 이동하게 될 .jsp를 찾는 일은 Spring 기반의 프로젝트에서 늘상하게 되는 일인데 @RequestMapping에 의해서 특정 메소드의 리턴 타입이 String 타입이 있는가하면 void 타입도 있다. 이들 각각의 경우에 이동하게 될 .jsp를 찾능 원리에 대해 정리하고자 한다.

아래와 같이 메소드의 리턴타입이 String 일 경우는

@Controller
public class SomeController {
	
	@RequestMapping("/product")
	public String doD(Model model) {
		
		return "productDetail"; 
	}
}

이 경우 이동하게 될 .jsp는 productDetail.jsp로 이동하게 된다. 

아래와 같이 메소드의 리턴 타입이 void 인경우

@RequestMapping(value = "/info", method=RequestMethod.GET)
public void show(@RequestParam("seq") int seq, @ModelAttribute("myMEM") MemberInfo info, Model model) {

}

이 경우는 리턴 타입이 void인데 이 메소드 실행후 어떻게 .jsp로 넘어가는가?
리턴 타입이 void 일 경우 .jsp 페이지로 넘어가는 규칙은 접근하는 url 경로에 해당하는 .jsp를 찾는다. 이 경우 RequestMapping이 지정한 경로인 /info에 근거해서 info.jsp를 찾아서 실행한다. 

이때 info.jsp를 찾을 때 기본적으로 webapp/ 아래에서 찾지만 이건 정확히 말하면 web.xml에서 DispatcherServlet이 로딩하는 서블릿 컨테이너의 설정 파일이 지정한 위치에서 찾는다.
예를들어서 web.xml의 DispatcherServlet의 설정 내용이 다음과 같을 경우

	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
	</servlet>

DispatcherServlet 클래스가 사용할 서블릿 컨터이너의 설정 파일은 아래 위치라는 뜻이고 
/webapp/WEB-INF/spring/appServlet/servlet-context.xml

/webapp/WEB-INF/spring/appServlet/servlet-context.xml의 내용은 다음과 같다고 한다면

	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

즉 prefix가 지정한 위치(webapp/WEB-INF/views/) 아래에서 suffix가 지정한 확장자를 붙여서 view 페이지(.jsp)를 찾는다. 결론적으로 webapp/WEB-INF/views/info.jsp를 찾는다는 뜻이다.




+ Recent posts