웹사이트 검색

스프링 MVC 튜토리얼


이 Spring MVC Tutorial에서는 Spring Tool Suite를 사용하여 Spring MVC 웹 애플리케이션을 개발하는 방법을 배웁니다. Spring MVC 프레임워크는 Java 웹 애플리케이션에 널리 사용됩니다.

스프링 MVC

스프링 MVC 튜토리얼

우리는 이전에 Spring Dependency Injection이 어떻게 작동하는지 보았고 이 자습서에서는 Spring MVC 프레임워크를 사용하여 간단한 웹 애플리케이션을 만드는 방법을 배웁니다. 우리는 Spring 프로젝트 개발을 위해 Eclipse 또는 IntelliJ IDE를 사용할 수 있지만 SpringSource는 Eclipse 기반 IDE인 Spring Tool Suite(STS)를 제공하며 Apache Tomcat 컨테이너 위에 구축되고 최적화된 VMware vFabric tc Server가 내장되어 있습니다. Spring 기반 애플리케이션용. 다음 기능을 제공하여 개발자의 삶을 더 쉽게 만들어주기 때문에 Spring MVC 튜토리얼 및 기타 향후 튜토리얼에 STS를 사용할 것입니다.

  • 기본적인 Spring 애플리케이션(MVC, Rest, Batch 등) 생성 지원, 처음부터 프로젝트 시작에 적합. Spring MVC 튜토리얼에서 곧 Spring MVC 프로젝트를 생성하는 것이 얼마나 쉬운지 보게 될 것입니다.
  • Spring 구성 파일 생성, 구성 파일 및 클래스 파싱과 같은 유용한 기능을 제공하여 이에 대한 유용한 정보를 제공합니다.
  • Spring 애플리케이션의 자동 유효성 검사
  • 리팩터링 지원으로 프로젝트를 쉽게 변경할 수 있으며 변경 사항은 구성 파일에도 반영됩니다.
  • 클래스뿐만 아니라 구성 파일에 대한 코드 지원도 가능합니다. 대부분의 경우 사용할 수 있는 항목과 세부 정보를 알아야 하기 때문에 이 기능이 매우 마음에 듭니다.
  • AspectJ 통합을 통한 AOP(Aspect Oriented Programming)에 대한 최상의 지원

STS 또는 Eclipse에서 Spring MVC 애플리케이션 생성

스프링 MVC 종속성

생성된 pom.xml 파일은 아래와 같습니다.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev</groupId>
	<artifactId>SpringMVCExample</artifactId>
	<name>SpringMVCExample</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
		<org.aspectj-version>1.7.4</org.aspectj-version>
		<org.slf4j-version>1.7.5</org.slf4j-version>
	</properties>
	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				 </exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
				
		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>	
		
		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>
				
		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	
		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>        
	</dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

artifactId는 웹 애플리케이션의 서블릿 컨텍스트가 되므로 원하는 경우 변경할 수 있습니다. Spring Framework, AspectJ 및 SLF4j 버전에 대해 정의된 속성이 거의 없으며 최신 버전을 반영하지 않는 것을 발견하여 오늘 현재 안정적인 최신 버전으로 변경했습니다. 관심 있는 프로젝트 종속성은 다음과 같습니다.

  • spring-context: Spring Core 의존성. SLF4J를 선호하는 일반 로그인은 제외됩니다.
  • spring-webmvc: MVC 지원을 위한 Spring 아티팩트
  • aspectjrt: AspectJ API 참조
  • SLF4J 및 Log4j: 로깅을 위해 Spring은 SLF4J 통합으로 인해 log4j 또는 Java 로깅 API에 대해 매우 쉽게 구성할 수 있습니다.
  • javax.inject - 종속성 주입을 위한 JSR330 API

Servlet, JSP, JSTL 및 JUnit API와 같은 다른 종속성이 추가되었지만 시작 응용 프로그램의 경우 간과할 수 있습니다.

Spring MVC 튜토리얼 - Log4j 구성

생성된 log4j.xml 파일은 아래와 같습니다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="https://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>
	
	<!-- Application Loggers -->
	<logger name="com.journaldev.spring">
		<level value="info" />
	</logger>
	
	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>

	<!-- Root Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="console" />
	</root>
	
</log4j:configuration>

모든 것을 콘솔에 출력하고 있다는 점에 주목하세요. 로깅을 파일로 리디렉션하기 위해 어펜더를 쉽게 추가할 수 있습니다.

Spring MVC 자습서 - 배포 설명자 구성

web.xml을 보고 분석해 봅시다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-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>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>

</web-app>

ContextLoaderListenerApplicationContext 수명 주기를 ServletContext 수명 주기에 연결하고 ApplicationContext 생성을 자동화합니다. ApplicationContext는 Spring 빈의 위치이며 contextConfigLocation 컨텍스트 매개변수를 통해 구성을 제공할 수 있습니다. root-context.xml 파일은 WebApplicationContext에 대한 구성 세부 정보를 제공합니다. DispatcherServlet은 Spring MVC 애플리케이션의 컨트롤러 클래스이며 모든 클라이언트 요청은 이 서블릿에 의해 처리됩니다. 구성이 servlet-context.xml 파일에서 로드되고 있습니다.

Spring MVC 튜토리얼 - 설정 파일

root-context.xml 파일:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
		
</beans>

우리는 여기서 공유 빈을 정의할 수 있습니다. 지금은 아무것도 없습니다. servlet-context.xml 코드:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="https://www.springframework.org/schema/beans"
	xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<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="com.journaldev.spring" />	
	
</beans:beans>

이것이 표준 Spring 구성 파일의 모습입니다. 이 모든 것을 직접 작성한다고 상상하면 STS 도구를 좋아하게 될 것입니다. annotation-driven 요소는 주석이 bean 구성에 사용될 것임을 컨트롤러 서블릿에 알리는 데 사용됩니다. resources 요소는 Spring 프레임워크를 통과하지 않으려는 이미지, HTML 페이지 등과 같은 정적 파일을 넣을 수 있는 위치를 정의합니다. InternalResourceViewResolver는 보기 확인자이며 접두사 및 접미사 속성을 통해 보기 페이지 위치를 제공할 수 있습니다. 따라서 모든 JSP 페이지는 /WEB-INF/views/ 디렉토리에 있어야 합니다. context:component-scan 요소는 컨트롤러 클래스 스캔을 위한 기본 패키지 위치를 제공하는 데 사용됩니다. 프로젝트 생성 시 주어진 최상위 패키지의 값을 기억하세요. 여기서 사용되는 값은 동일합니다.

Spring MVC 컨트롤러 클래스

HomeController는 home() 메서드를 사용하여 자동으로 생성되지만 loginPage() 및 login() 메서드를 추가하여 약간 확장했습니다.

package com.journaldev.spring;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage(Locale locale, Model model) {
		return "login";
	}
	
	@RequestMapping(value = "/home", method = RequestMethod.POST)
	public String login(@Validated User user, Model model) {
		model.addAttribute("userName", user.getUserName());
		return "user";
	}
}

@Validated 주석. 모든 메소드에는 Model이 인수로 포함되어 있으며 나중에 JSP 응답 페이지에서 사용할 속성을 설정할 수 있습니다.

Spring MVC 모델 클래스

모델 클래스는 양식 변수를 보유하는 데 사용되며 사용자 모델 빈은 아래와 같습니다.

package com.journaldev.spring;

public class User {

	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}

변수 이름과 해당 getter 및 setter 메서드가 있는 간단한 Java Bean.

Spring MVC 튜토리얼 - 페이지 보기

아래와 같이 3개의 JSP 페이지가 있습니다. home.jsp 코드:

<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

속성 값을 가져오기 위해 JSP 표현 언어를 사용하는 것에 주목하십시오. login.jsp 코드:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="home" method="post">
<input type="text" name="userName"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

사용자가 userName을 입력으로 제공하는 간단한 JSP 페이지입니다. 양식 변수 이름은 사용자 클래스 변수 이름과 동일합니다. 또한 Form action은 "home\이고 method는 "post\입니다. HomeController login() 메서드가 이 요청을 처리할 것이 분명합니다. 사용자.jsp 코드:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Home Page</title>
</head>
<body>
<h3>Hi ${userName}</h3>
</body>
</html>

사용자 이름이 표시되는 사용자를 위한 간단한 홈 페이지입니다. 로그인 방법에서 이 속성을 설정하고 있습니다.

Spring MVC 예제 애플리케이션 테스트

Spring MVC 프로젝트 다운로드