Category: Spring 5

0

스프링 MVC 시작하기 - XML로 세팅하기

스프링 MVC 시작하기 - XML로 세팅하기 Web.xml 설정하기 root-context.xml 설정하기 servlet-context 설정하기 의존관계 설정하기<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springMVCstep3</artifactId> <version>1.0-SNAPSHOT</version> <properties> <javax.servlet-version>4.0.1</javax.servlet-version> <javax.servlet.jsp-version>2.3.3</javax.servlet.jsp-version> <javax.servlet.jsp.jstl-version>1.2</javax.servlet.jsp.jstl-version> <org.springframework-version>5.2.2.RELEASE</org.springframework-version> <!-- <org.springframework-version>4.3.25.RELEASE</org.springframework-version> --> </properties> <!-- 라이브러리 셋팅 --> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-version}</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${javax.servlet.jsp-version}</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${javax.servlet.jsp.jstl-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> <build> <finalName>springMVCstep3</finalName> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <pluginManagement> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.1</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </pluginManagement> </build> <!-- 라이브러리 버전관리 --></project> Web.xml 설정하기 톰캣 DispatcherServlet 을 스프링 DispatcherServlet 으로 바꾸기 Bean을 정의할 xml 파일을 정의하기 Listener 를 설정한다. 파라미터 인코더 를 설정한다. <!-- 현재 웹 애플리케이션에서 받아들이는 모든 요청에 대해 appServlet이라는 이름으로 정의되어 있는 서블릿을 사용하겠다. --><servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern></servlet-mapping><!-- 요청 정보를 분석해서 컨트롤러를 선택하는 서블릿을 지정한다. --><servlet> <servlet-name>appServlet</servlet-name> <!-- Spring MVC에서 제공하고 있는 기본 서블릿을 지정한다. --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Spring MVC 설정을 위한 xml 파일을 지정한다. --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet> <!-- Bean을 정의할 xml 파일을 지정한다. --><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/root-context.xml</param-value></context-param> <!-- 리스너설정 --><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>