2022. 1. 28. 21:14ㆍSpring
1. New Project
좌측 상단 File -> new -> project
Gradle -> java 체크 -> Next
프로젝트명, 그룹ID 등 작성 후 Finish
2. Gradle.build 수정
plugins에 'war' 추가, dependencies에 spring-webmvc, servlet-api 추가
plugins {
id 'java'
id 'war' //new
}
group 'com.tutorial'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
implementation 'org.springframework:spring-webmvc:5.2.3.RELEASE' //new
implementation 'javax.servlet:servlet-api:2.5' //new
}
test {
useJUnitPlatform()
}
Load Gradle changes 실행 우측 상단 코끼리 누르기 or [ctrl + shift + o] 누르기
3. Add Framework Support
프로젝트 폴더에 우클릭 -> Add Framework Support...
Spring 체크
※주의사항 : 절대 Spring MVC 추가하지 마세요!!! .XML .java 경로설정이 자동으로 안돼서 차라리 수동으로 하는게 나아요!!!
제가 저걸로 3일 삽질했어요 ㅜㅜ
4. webapp 생성 및 structure 구성
다음과 같이 폴더 및 xml 파일, jsp 파일 생성
file -> Project Structure... 혹은 [ctrl + shift + alt + s]
Facets -> Web 목록 클릭 -> +버튼 web.xml 클릭 -> ok
Source Roots 하단 두 개 체크 -> ok (하단 경고 무시)
5. 톰켓 실행 환경 추가
run -> Edit Configurations
+ 클릭 -> Tomcat Server -> Local
우측 하단 fix 클릭 혹은 Deployment 메뉴 선택 -> + 클릭 -> Artifact... 클릭 위에거 추가
Application context 경로를 '/' 으로 변경
Server 탭으로 돌아가서 URL 뒤에 붙은 것 제거
다른 포트를 원하면 HTTP port 변경을 해도 됨
톰켓을 실행해보면 연결이 잘 되는 것을 볼 수 있다.
6. 컨트롤러 생성
package com.tutotral.api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/")
public String hello() {
return "index";
}
}
7. XML, jsp 파일 작성
7-1 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/applicationContext.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/dispatcher-servlet.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>
7-2 applicationContext.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>
7-3 dispatcher-servlet.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/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.tutorial"/>
</beans>
com.tutorial을 본인의 bese package에 맞게 변경
7-4 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
제발 되라 제발 되라 제발 되라
</body>
</html>
8. 테스트
'Spring' 카테고리의 다른 글
io.awspring 라이브러리를 활용한 Spring-Amazon S3 연동 (0) | 2023.09.06 |
---|---|
spring 환경에서 redis pub-sub을 이용한 멀티 캐스팅 (0) | 2023.07.15 |
Spring boot oauth2로 로그인하기 (0) | 2022.02.13 |
JWT, 그리고 OAuth (0) | 2022.02.11 |