티스토리 뷰
Maven Project 생성
File - New - Maven Project - Archetype 선택(Group Id : org.apache.maven.archetypes, Artifact Id : maven-archetype-webapp) - Group Id, Artifact Id 입력 - finish
폴더 추가
Maven Project를 최초 생성하면 위와 같은 구조를 가지고 있는데 Maven Project 디렉토리 구조를 만들기 위해서 아래와 같이 main 폴더 하위에 java, resources 폴더를 생성한다.
ㄴsrc
ㄴmain
ㄴjava : 자바 패키지 폴더와 소스 코드가 위치함.
ㄴresources : *.properties, *.xml 등 설정파일들이 위치함.
ㄴwebapp : WEB-INF와 웹 관련 리소스(html, jsp)들이 위치함.
ㄴtest
ㄴjava : 테스트와 관련된 자바 패키지와 소스코드가 위치함.
ㄴresources : 테스트와 관련된 설정파일이 위치함.
ㄴtarget : 컴파일, 패키징된 결과물이 위치함.
ㄴpom.xml : Maven 설정 파일
1. pom.xml 수정
pom.xml 파일을 아래와 같이 수정 후 프로젝트 우클릭 -> Maven -> Update Project
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Servlet JSP JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
2. org.eclipse.wst.common.project.facet.core.xml 수정
이클립스 Navigator에서 .settings 하위 org.eclipse.wst.common.project.facet.core.xml을 열어서 버전을 고친다.
저장 후 이클립스 재시작
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="3.1"/><!-- servlet 버전 2.5 -> 3.1 -->
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
3. src/main/java 폴더 생성 -> 패키지 생성(kr.or.connect.mvcexam.config) -> WebMvcContextConfiguration.java 작성(=DispatcherServlet 설정)
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "kr.or.connect.mvcexam.controller" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(31556926);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
// default servlet handler를 사용하게 합니다.
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
System.out.println("addViewControllers가 호출됩니다. ");
registry.addViewController("/").setViewName("main");
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
4. web.xml 수정
WebMvcContextConfiguration의 경로를 지정한다.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Servlet 3.1 이상에서 설정 -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>kr.or.connect.mvcexam.config.ApplicationConfig</param-value>
</context-param>
<!-- ContextLoaderListener가 실행될 때 위 context-param을 참조한다. -->
<!-- Context Load(서버 Load)할 때 DBConfig과 ApplicationConfig을 읽는다. -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>kr.or.connect.mvcexam.config.WebMvcContextConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. 테스트
src/main/webapp/WEB-INF/views/main.jsp 생성 후 Run
참조
'Framework > Spring' 카테고리의 다른 글
페이징 처리 (0) | 2021.01.07 |
---|---|
Spring MVC 실행 과정 및 Controller 작성 (0) | 2021.01.06 |
Spring MVC (0) | 2021.01.06 |
Spring JDBC 실습 (0) | 2021.01.04 |
Spring JDBC (0) | 2021.01.04 |