[MVC프로젝트] #1 Spring Legacy Project 생성
2019. 9. 21. 19:14ㆍGo to 코딩천재/Spring
STEP 1 . Spring Legacy Project 생성 (MVC 프로젝트)
STEP 2 . 프로젝트 파일의 Properties 설정
[build path]
① JRE-System Library - edit - 'workspace default JRE ( ... )' 체크
② Add Library - Server Runtime 라이브러리 - tomcat 8.5 server 추가
[Java Compiler]
① 'use default compilance from execution ...' 체크 해제
② 'use default compilance settings' 체크
③ 오른쪽 버전 변경 (1.8)
[Project Facets]
① Dynamic Web Module 버전 변경 (3.1)
② Java 버전 변경 (1.8)
STEP 3 . pom.xml 설정
* json, jdbc, mybatis 사용 기준
<?xml version="1.0" encoding="UTF-8"?>
...
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.1.5.RELEASE</org.springframework-version>
...
<jstl-version>1.2</jstl-version>
<mybatis-version>3.5.1</mybatis-version>
<mybatis-spring-version>2.0.1</mybatis-spring-version>
<json-version>20180913</json-version>
</properties>
<dependencies>
...
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<!-- json -->
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json-version}</version>
</dependency>
<!-- MyBatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!-- MyBatis spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring-version}</version>
</dependency>
<!-- jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
</dependencies>
STEP 4 . web.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
...
<!-- 클라이언트 요청 시, 서블릿 가기 전 필터링 작업하는 부분 -->
<!-- POST 방식만 해당되는 방법 -->
<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>/*</url-pattern>
</filter-mapping>
<!-- 디폴트 경로 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
STEP 5 . servlet-context.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
...
<!-- 요청에 대한 한글 인코딩 -->
<mvc:annotation-driven>
<mvc:message-converters>
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>text/html;charset=utf-8</beans:value>
<beans:value>application/json;charset=utf-8</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- resource 파일 매핑 -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- 컨트롤러에서 이용되는 view 경로의 기본 접두사, 접미사 설정 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- 컨트롤러로 인식될 클래스의 패키지 설정 -->
<context:component-scan base-package="xxx" />
</beans:beans>