인텔리제이 File → New → Project…
JDK : 원하는 jdk 버전 등록
Gradle DSL : Kotlin
Editor → File Encodings
Build, Execution, Deployment → Build Tools → Gradle
Build, Execution, Deployment → Compiler → Annotaion Processors
build.gradle.kts
plugins {
id("java")
id("war")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.springframework:spring-test:4.2.0.RELEASE")
testImplementation("junit:junit:4.12")
compileOnly("org.projectlombok:lombok:1.18.22")
compileOnly("javax.servlet:javax.servlet-api:3.1.0")
implementation("org.springframework:spring-context:4.2.0.RELEASE")
implementation("org.springframework:spring-webmvc:4.2.0.RELEASE")
}
tasks.test {
useJUnitPlatform()
}
src → main 폴더 하위에 webapp 디렉토리 생성
webapp 하위에 WEB-INF 폴더 생성
File → Project Structure
📌 build.gradle.kts
에 id("war")
를 추가하지 않았다면 아티팩트가 나타나지 않음
WEB-INF 하위에 views폴더, spring 폴더 생성, spring 하위에 appServlet 폴더 생성
spring 폴더 내에 root-context.xml 생성
spring 우클릭 → New → XML Configuration File → Spring Config
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>
root-context.xml 와 같은 방식으로 appServlet 폴더 내에 servlet-context.xml 생성
servlet-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://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>
</beans>
src → main → java → 그룹 에 있던 main.java 삭제
controller 패키지 추가 후 컨트롤러 클래스 생성
servlet-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://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="org.example.controller"/>
</beans>
<?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">
<!-- 한글깨짐 방지 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- root-context -->
<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-context -->
<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>
Run → Edit Configurations… 클릭
Deployment 탭에서 Artifact 추가
Application context 는 / 만 남도록 수정
Controller
package org.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "index";
}
}
index.jsp
: webapp → WEB-INF → views 폴더 내에 위치
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>HOME</title>
</head>
<body>
<h1>Index</h1>
</body>
</html>
톰캣 실행 후 URL 접속