2016년 9월 2일 금요일

스프링(Spring) 개발하기 - (5) 스프링 설정파일

1. pom.xml 수정




2. index.jsp 추가


2-1. webapp 폴더에 index.jsp 를 생성한다.


2-2. index.jsp 소스를 수정한다.

<body>
 <h3>index page</h3>
</body>

2-3. web.xml 에서 index.jsp 설정을 추가한다.

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
 <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

2-4. Server 실행후 http://localhost:8080/프로젝트이름/ 주소창에 입력한다.


3. Servlet 설정 변경


web.xml 파일을 열고

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
    
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
를 다음과 같이 변경한다.

<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/config/*-servlet.xml 
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
</servlet-mapping>


4. contextConfigLocation 변경


4-1. WEB-INF 폴더 밑에 config 폴더를 생성후 servlet-context.xml을 복사해서 action-servlet.xml 로 이름을 변경한다. spring 폴더는 삭제한다.


4-2. web.xml 소스를 수정한다.


<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/config/*-servlet.xml 
     </param-value>
        </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

5. UTF-8 설정


web.xml에 추가한다.


<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>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

6. 기타 설정

기존 contextConfigLocation 설정을 수정한다.

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value></param-value>
</context-param>

7. 최종 web.xml


<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
 <welcome-file-list>
             <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/spring/context-*.xml</param-value>
 </context-param>
 
 <!-- Creates the Spring Container shared by all Servlets and Filters -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!-- Processes application requests -->
 <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/config/*-servlet.xml 
         </param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
         <servlet-name>action</servlet-name>
         <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <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>
 </filter>
 <filter-mapping>
     <filter-name>encodingFilter</filter-name>
 <url-pattern>*.do</url-pattern>
 </filter-mapping>

</web-app>
Share:

0 개의 댓글:

댓글 쓰기