※ MultiActionController
- 특정 파라미터의 값이나 URL 등을 이용해여 해당 클래스의 메소드를 실행시킴.
- 비슷한 기능을 가진 메서드를 모아 불필요한 클래스를 많이 생성하는 것을 줄여줌
public [ModelAndView | Map | void] methodName(HttpServletRequest request, HttpServletResponse response)
public [ModelAndView | Map | void] methodName(HttpServletRequest request, HttpServletResponse response, SomeCommand command) |
▶ 특징
- return 값은 ModelAndView, Map, void 중의 하나여야 함
- 반드시 첫번째 파라미터의 경우는 HttpServletRequest, 두번째 파라미터의 경우는 HttpServletResponse이며, 세번째는 선택적으로 지정할 수 있음.
● MethodNameResolver 종류
◑ ParameterMethodNameResolver : 특정 파라미터의 값을 메서드 이름으로 사용
◑ InternalPathMethodNameResolver(default) : URL의 마지막 경로를 메서드 이름으로 사용
◑ PropertiesMethodNameResolver : URL과 메서드 이름의 매핑을 프로퍼티로 설정
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class BoardMultiActionController extends MultiActionController {
public ModelAndView onBoardList(HttpServletRequest request, HttpServletResponse response){
System.out.println("<<<<< onBoardList >>>>>");
//로직구현
String param = request.getParameter("type");
System.out.println("type = " + param);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewName");
modelAndView.addObject("attributeName", "attributeValue");
return modelAndView;
}
public ModelAndView onBoardDetail(HttpServletRequest request, HttpServletResponse response){
System.out.println("<<<<< onBoardDetail >>>>>");
//로직구현
String param = request.getParameter("type");
System.out.println("type = " + param);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewName");
modelAndView.addObject("attributeName", "attributeValue");
return modelAndView;
}
public ModelAndView onBoardInsert(HttpServletRequest request, HttpServletResponse response){
System.out.println("<<<<< onBoardInsert >>>>>");
//로직구현
String param = request.getParameter("type");
System.out.println("type = " + param);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("viewName");
modelAndView.addObject("attributeName", "attributeValue");
return modelAndView;
}
}
|
◑ ParameterMethodNameResolver : 특정 파라미터의 값을 메서드 이름으로 사용
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- HandlerMapping -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/multi.sp">boardMultiActionController</prop>
</props>
</property>
</bean>
<!-- ViewResolver -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="boardMultiActionController" class="controller.BoardMultiActionController">
<property name="methodNameResolver" ref="parameterMethodNameResolver"></property>
</bean>
<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="mode"></property>
</bean>
</beans>
|
log 출력
//http://localhost:8080/springMVC//multi.sp?mode=onBoardList
<<<<< onBoardList >>>>>
type = null
//http://localhost:8080/springMVC//multi.sp?mode=onBoardDetail&type=003
<<<<< onBoardDetail >>>>>
type = 003 |
◑ InternalPathMethodNameResolver : URL의 마지막 경로를 메서드 이름으로 사용 - 가장 많이쓰는 형태
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- HandlerMapping -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/onBoardList.sp">boardMultiActionController</prop>
<prop key="/onBoardDetail.sp">boardMultiActionController</prop>
</props>
</property>
</bean>
<!-- ViewResolver -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="boardMultiActionController" class="controller.BoardMultiActionController">
<property name="methodNameResolver" ref="internalPathMethodNameResolver"></property>
</bean>
<bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
</bean>
//이런식의 prefix, suffix를 설정할 수도 있음
<bean id="internalPathMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
<property name="prefix" value="subBoard"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
|
log 출력
//http://localhost:8080/springMVC/onBoardList.sp
<<<<< onBoardList >>>>>
type = null
//http://localhost:8080/springMVC/onBoardDetail.sp?type=007
<<<<< onBoardDetail >>>>>
type = 007 |
◑ PropertiesMethodNameResolver : URL과 메서드 이름의 매핑을 프로퍼티로 설정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- HandlerMapping -->
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/boardList.sp">boardMultiActionController</prop>
<prop key="/boardInsert.sp">boardMultiActionController</prop>
</props>
</property>
</bean>
<!-- ViewResolver -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="boardMultiActionController" class="controller.BoardMultiActionController">
<property name="methodNameResolver" ref="propertiesPathNameResolver"></property>
</bean>
<bean id="propertiesPathNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/boardList.sp">onBoardList</prop>
<prop key="/boardInsert.sp">onBoardInsert</prop>
</props>
</property>
</bean>
</beans>
|
log 출력
//http://localhost:8080/springMVC/boardList.sp
<<<<< onBoardList >>>>>
type = null
//http://localhost:8080/springMVC/boardInsert.sp?type=123
<<<<< onBoardInsert >>>>>
type = 123 |
[참고자료] 스프링 2.5 프로그래밍 (최범균)