티스토리 뷰

 Intellij를 사용하여 gradle 기반의 Spring MVC 구조를 생성하려고 한다. 


<Project Detail>

 - JDK: 1.8.0_261

 - Spring framework: 5.2.3 RELEASE

 - tomcat: tomcat 8.0


1. new project 생성

  Gradle - java를 체크하고 JDK 버전을 확인한 후, 새 프로젝트를 생성한다.

2. build.gradle 작성

 Spring MVC를 구성하기 위해 필요한 라이브러리를 직접 build.gradle에 작성하여 빌드한다. 또한 war 생성을 위한 플러그인 설정도 추가한다.

 

○build.gradle

plugins {
    id 'java'
    id 'war'  //get added
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'

    implementation group: 'org.springframework', name: 'spring-context', version: '5.2.3.RELEASE'

    implementation group: 'org.springframework', name: 'spring-core', version: '5.2.3.RELEASE'
    
    implementation group: 'org.springframework', name: 'spring-beans', version: '5.2.3.RELEASE'

    implementation group: 'org.springframework', name: 'spring-webmvc', version: '5.2.3.RELEASE'

    implementation group: 'org.springframework', name: 'spring-aop', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-expression', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-instrument', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-jdbc', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-jms', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-messaging', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-orm', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-oxm', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-test', version: '5.2.3.RELEASE'
    implementation group: 'org.springframework', name: 'spring-tx', version: '5.2.3.RELEASE'
}

test {
    useJUnitPlatform()
}

 

3. webapp directory 설정

 build.gradle을 사용하고 add framework support 기능을 사용하지 않았기 때문에, 직접 필요한 설정파일을 담을 수 있도록 디렉토리를 구성해준다.

- src/main/webapp

- src/main/webapp/WEB-INF

- src/main/webapp/WEB-INF/spring

- src/main/webapp/WEB-INF/views


4. context 파일 작성 

 

○root-context.xml 

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

</beans>

 

○servlet-context.xml

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:component-scan base-package="com.sgt.test"/>

</beans>

 

 위와 같이 root-context 파일과 servlet-context파일을 작성한 뒤에 File - Project Structure - Facets에서 직접 맵핑 설정을 해줘야 한다.

맵핑을 마치면, context 파일 상단에 뜨던 오류들이 사라질 것이다.

 

5. web.xml 생성


 마찬가지로 Facet 설정에서 web 탭에서 +를 클릭하여 WEB-INF내부로 경로를 지정하고 web.xml을 생성한다.

path는 다음과 같다.

 - src\main\webapp\WEB-INF\web.xml

 

이후 아래와 같이 web.xml을 작성하면 된다.

○web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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>

 

 

6. Test Controller & index.jsp 생성
 test를 위한 Controller와 index 페이지를 아래와 같이 구성한다.

 

○com.stg.test.TestController.java

package com.sgt.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/")
    public String test() {
        return "index";
    }
}

 

○webapp/WEB-INF/views/index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <div>
        <h1>8 14 25 28 30 37  42</h1>
    </div>
</body>
</html>

 

구성이 끝나면 다음과 같은 디렉토리 구조를 가질 것이다.

 

7. tomcat 설정
 이제 서버 구동을 위한 Tomcat 설정만 완료하면 된다.

 

8. 끝!

 

 끝!

 

반응형
Comments