728x90

스프링 2.5 부터는 Controller 인터페이스를 구현하지 않은 클래스도 어노테이션을 사용하여 컨트롤러로 사용할 수 있게 되었다. 이를 위해 스프링은 @Controller, @RequestMapping 등 컨트롤러를 구현하는데 필요한 어노테이션을 제공하고 있다.

어노테이션을 이용하여 컨트롤러를 구현할 때는 요청 URL 매핑을 @RequestMapping 어노테이션을 이용하여 설정한다.
@RequestMapping 어노테이션을 처리하기 위해서 DefaultAnnotationHandlerMapping 을 HandlerMapping 으로 등록해 주어야 한다.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
p:alwaysUseFullPath="true" />


그런데 DispatcherServlet 이 사용하는 설정 파일에 별도의 HandlerMapping 명시를 하지 않으면, DispatcherServlet 은 기본적으로 DefaultAnnotationHandlerMapping 을 등록하므로 디폴트로 설정 파일에 명시를 하지 않아도 된다.

(만약 DispatcherServlet 이 사용하는 스프링 설정 파일에 HandlerAdapter 를 등록했다면, 추가적으로 AnnotationMethodHandlerAdapter 를 빈으로 등록해주어야 어노테이션을 이용한 컨트롤러가 정상 동작함)


이제 @Controller 어노테이션에 대해서 알아보자. @Controller 어노테이션은 클래스 타입에 적용되며, @Controller 를 붙이면 해당 클래스를 웹 요청을 처리하는 컨트롤러로 사용할 수 있다.

@Controller
public class HelloController {
...
}


컨트롤러로 사용하기 위해 @Controller 가 적용된 클래스는 <bean> 태그에서 스프링 빈으로 등록해주면 된다.

<bean id="helloController" class="...package.HelloController" />


아니면 <context:component-scan> 태그를 이용하여 @Controller 를 적용한 클래스를 자동으로 로딩할 수 있다.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context2.5.xsd">
    
    <context:component-scan
        base-package="...package" />


</beans>


위에서 component-scan 영역은 base-package 까지만 잡아주면 된다. 클래스 명을 제외한 패키지 이름까지만.

정리해보면 @Controller 어노테이션을 적용한 클래스는 <bean> 태그를 이용하거나 <context:component-scan> 태그를 이용하여 등록하면, DefaultAnnotationHandlerMapping 을 통해 컨트롤러로 사용이 되는 것이다.

 

이제는 @RequestMapping 어노테이션을 이용한 요청 매핑에 대해서 알아보자.

@RequestMapping 어노테이션은 컨트롤러가 처리할 요청 URL 을 명시하는데 사용되며, 클래스나 메서드에 적용된다.

@RequestMapping 을 클래스에 적용하지 않고 메서드에만 적용할 경우 각각의 메서드가 처리할 요청 URL 을 명시하게 된다.

@Controller
public class RequestController {

    @RequestMapping("/test/mapping1.do")
    public String mappingFirst(@RequestParam("id") String id, @RequestParam("pwd") String pwd, ModelMap modelMap) {
        // 로직 처리와 리턴 구문
    }

    @RequestMapping("/test/mapping2.do")
    public String mappingSecond(@RequestParam("id") String id, @RequestParam("pwd") String pwd, ModelMap modelMap) {
        // 로직 처리와 리턴 구문
    }
    ...
}

다수의 메서드에 @RequestMapping 을 적용하면, MultiActionController 처럼 한 개의 컨트롤러에서 다수의 요청을 처리할 수가 있다.

그리고 @RequestMapping 의 method 엘리먼트를 이용하면 처리할 수 있는 Http Method 를 지정할 수도 있다.

@RequestMapping(value="/test/mapping1.do", method=RequestMethod.POST)
public String mappingSomething(...) {...}

@RequestMapping 을 클래스 타입에 적용하게 되면, 해당 컨트롤러 클래스는 지정한 URL 만을 처리할 수 있다.
이 경우 메서드에 적용되는 @RequestMapping 은 더 이상 URL 명시를 할 수 없고, method, params 엘리먼트만 지정할 수 있게 된다.


출처 : http://northface.tistory.com/

728x90

+ Recent posts