handlerMapping

Spring/Spring MVC 2009. 1. 17. 12:03

두개의 servlet.xml 파일을 만들어서 각각 요청에 맞게 사용하고 싶은데.. 어떻게 해야할까...
책에 ApplicationContext설정을 보니까
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/front.xml</param-value>
</init-param>

이런식으로 쓰더라구..

처음 만들었던 dispatcher-servlet.xml과 새로 만든 hello-servlet.xml 을 web.xml에 위에 맞게 각각 설정해줬다..

hello.htm , hello2.htm 요청을 하니 hello2.htm만 정상적으로 뜬다,.
어찌된 걸까... 하나를 주석처리하고 실행해보면 각각 잘 된다.
그렇다면 설정에 문제가 있다고 생각했다...
삽질에 삽질...
책을 다시 봤다..
웹요청과 컨트롤러 매핑 : HandlerMapping 챕터에서 설명이 나와있었다..
문제는 <servlet-mapping> 두 서블릿 모두 .htm 으로 url-pattern을 설정한 것...

web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <display-name>
 SpringTest</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
<!--한글인코딩 -->
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>
   org.springframework.web.filter.CharacterEncodingFilter
  </filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>EUC-KR</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <servlet>
  <servlet-name>
   dispatcher
  </servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
 </servlet>
 
 <servlet>
  <servlet-name>
    hello
  </servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
 </servlet>
<!--http://localhost/spring/say/hello.htm으로 호출 -->
 <servlet-mapping>
  <servlet-name>
   dispatcher
  </servlet-name>
  <url-pattern>/say/*</url-pattern>
 </servlet-mapping>
<!--http://localhost/spring/hello2.htm으로 호출 --> 
 <servlet-mapping>
  <servlet-name>
   hello
  </servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
</web-app>

hello-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                http://www.springframework.org/schema/aop  
                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                        http://www.springframework.org/schema/context 
                              http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="kame.spring" />   
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">      
            <props>        
                   <prop key="/index.htm">indexController</prop>   
                   <prop key="/hello2.htm">helloController</prop>
           </props>   
      </property>  
</bean>
<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/WEB-INF/test/"    
         p:suffix=".jsp" />
<bean name="indexController"  class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />
</beans>
============================================================================
dispatcher-servlet.xml
<?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-2.5.xsd">
 <!--핸들러-->
 <bean id="handlerMapping"
  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
 <bean name="/hello.htm" class="kame.spring.chap04.HelloController" />
<bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/view/" />
  <property name="suffix" value=".jsp" />
 </bean>
</beans>      

스프링은 설정파일 부분이 너무 복잡하다. 방식도 다양하고 상속관계도 알아둘 필요가 있다.
아무튼 성공!!
AND