티스토리 뷰

Spring MVC 5-4  Annotation 기반 설정

 

Annotation 관련 설정

Controller가 아주 많아지면? --> XML 설정 파일이 복잡해짐.

Controller Annotation 지원(Spring 2.5부터)

action-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemalLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/contxt/springcontext-3.0.xsd">
           <context:component-scan base-package="com.multicampus.view" />
</beans>

 

<context:component-scan />을 지정

 

@Controller 사용

<context:component-scan />에 의해 bean으로 자동 등록

LoginController.java
public class LoginController  implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request,
 HttpServletResponse response) throws Exception {
        return null;
}

 

applicationContext.xml
<beans ...>
<bean class="com.multicampus.view.user.controller.LoginController" />
</beans>

 

 

 

LoginController.java
@Controller
public class LoginController {
    public String loginUser(UserVO vo){
        return null;
}
}

 

 

 

 

 

 

 

 

@RequestMapping 사용

요청 URL 경로와 Controller Class 또는 Controlle Class 내의 메서드를 매핑함. 아래의 두 코드는 동일한 의미

LoginController.java
@Controller
public class LoginController {
    @RequestMapping("/loign.do")
public ModelAndView login(HttpServletRequest request,
           HttpServletResponse response) {
}
}

 

WEB-INF/spring-mvc.xml
<!-- HandlerMapping -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
           <props>
               <prop key="/login.do">loginController</prop>
           </props>
</property>
</bean>
<bean id="loginController" class="com.multicampus.view.user.LoginController"/>

 

 

요청 방식에 따른 @RequestMapping 사용

요청URL은 같아도 method에 따라 다르게 처리할 수 있음

LoginController.java
package com.multicampus.view.user.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
@RequestMapping("/loignView.do", method=RequestMethod.GET)
public String loginView(HttpServletRequest request,
           HttpServletResponse response) {
}
    @RequestMapping("/loign.do", method=RequestMethod.POST)
public String login(HttpServletRequest request,
           HttpServletResponse response) {
}
}

 

 

컨트롤러 통합하기

너무 많은 컨트롤러 클래스들 -> 하나의 컨트롤러로 통합

@RequestMapping 에서 요청 URL을 매핑

UserController.java
@Controller("/user.do")
public class UserController {
           @RequestMapping(value="addUser.do")
           public String addUser(UserVO vo) {
                     System.out.println("=== 회원 등록 ===");
                return null;
           }         
           @RequestMapping(value="getUser.do")
           public String getUser(UserVO vo) {
                     System.out.println("=== 회원 등록 ===");
                     return null;
           }         
           @RequestMapping(value="getUserList.do")
           public String getUserList(UserVO vo) {
                     System.out.println("=== 회원 등록 ==="); 
                     return null;
           }
}

 

 

 

 

댓글