by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

Spring MVC 개발환경 설정




Spring MVC 개발환경 설정

web.xml

  • Spring MVC를 사용하기 위해서 아래의 내용들을 web.xml에 설정해야 한다.
  • encodingFilter를 설정하여 한글이 깨지지 않도록 UTF-8로 인코딩한다.
  • Spring applicationContext*.xml 파일을 톰캣 startup시 읽도록 설정한다.
  • Spring MVC에서 처리하도록 DispatcherServlet을 설정한다.
/WEB-INF/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>spring-project</display-name>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>     
    </welcome-file-list>
    
    <display-name>www.gurubee.net study application</display-name>
 
    <!-- encodingFilter를 설정하여 한글이 깨지지 않도록 UTF-8로 인코딩한다.  -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
  
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

    <!-- Spring MVC에서 처리하도록 DispatcherServlet을 설정한다. --> 
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet-context.xml</param-value>
        </init-param>
         -->
        <load-on-startup>1</load-on-startup>        
    </servlet>
     
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
   <!-- Spring applicationContext*.xml 파일을 톰캣 startup시 읽도록 설정한다. -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/applicationContext.xml 
        </param-value>
    </context-param>    

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener> 
</web-app>
 
 

mvc-dispatcher-servlet.xml

  • 기본적으로 /WEB-INF/ 디렉토리에 위치하며 [STUDY: 서블릿명]-servlet.xml 파일로 존재한다.
  • [STUDY: 서블릿명]-servlet.xml 파일에서는 스프링 MVC의 구성 요소인 HandlerMapping, Controller, ViewResolver, View 등의 빈 설정을 한다.
  • 위 예에서 servlet-name을 mvc-dispatcher로 정의하였기 때문에 mvc-dispatcher-servlet.xml 파일로 생성하면 된다.
/WEB-INF/mvc-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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  
    <context:component-scan base-package="com.oracleclub.study" >  
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
    
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
        <property name="order" value="0" />
    </bean>
 
</beans>
    
 

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.