[2]IntelliJ + (Maven)SpringMVC + Tomcat 기본 환경 구성
[1]IntelliJ + (Maven)SpringMVC + Tomcat 기본 환경 구성
기존에 사용했던 'eclipse'가 아닌 새로운 개발 환경인 intellij에서 Spring Boot환경을 구성하고 간단한 웹 페이지를 출력해보는 실습을 진행해 보려고 한다. 1. 'intellij'에서 Project를 생성한다. 아래 세
rspring41.tistory.com
저번 편에 가장 기본적인 SpiringMVC프로젝트 생성과 Tomcat연동을 진행했고 이번에는 Controller와 JSP를 이용하여 매핑하는 실습을 진행하겠다.
프로젝트에 Web/WEB-INF/views에 있는 web.xml파일과 dispatcher-servlet.xml파일을 수정하여야 한다.
(applicationContext.xml은 DB연동과 관련 있는것 같아 해당 내용은 3편에서 진행하겠다.)
아래 코드는 글쓴이가 개인적으로 찾아보고 수정한 코드이다.
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">
<!--한글 인코딩 설정-->
<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>
<!-- 연결할 XML파일 설정 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 공유하는 bean설정 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Controller가 공유하는 Bean들을 포함하는 Spring Container를 생성한다 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- aliases 설정 -->
<servlet>
<!-- 아래 서블릿 매핑에 servlet-name은 반드시 같아야 한다 -->
<!-- 파일 이름 + -servlet.xml로 생성 -->
<servlet-name>dispatcher</servlet-name>
<!-- 개발자에 의해 작성된 실제 클래스 이름 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 작은 수 부터 로드된다 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 서블릿 매핑 -->
<servlet-mapping>
<!-- 매핑할 서블릿 이름 -->
<servlet-name>dispatcher</servlet-name>
<!-- URL주소 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
여기서 JSP 파일을 연동하기 위해서는 servlet이 들어간 태그에 집중하면 된다.
가장 먼저 연결에 사용할 xml설정 파일을 불러오고 해당 파일에 맵핑 주소를 설정 해주게 된다.
해당 부분에 코드는 아래 따로 분리해 보았다.
<!-- aliases 설정 -->
<servlet>
<!-- 아래 서블릿 매핑에 servlet-name은 반드시 같아야 한다 -->
<!-- 파일 이름 + -servlet.xml로 생성 -->
<servlet-name>dispatcher</servlet-name>
<!-- 개발자에 의해 작성된 실제 클래스 이름 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 작은 수 부터 로드된다 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 서블릿 매핑 -->
<servlet-mapping>
<!-- 매핑할 서블릿 이름 -->
<servlet-name>dispatcher</servlet-name>
<!-- URL주소 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
이때 <servlet-name>으로 저장한 이름과 실제 WEB-INF폴더 내부에 name-servlet.xml파일에 이름이 같아야 한다.
<servlet-mapping>는 매핑할 URL주소를 설정하게 된다.
2. dispatcher-servlet.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/>
<!--정적자원 매핑-->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!--viewResolver 설정-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- base-package 경로 설정 -->
<context:component-scan base-package="com.modules.home"/>
</beans>
web.xml에 특정 주소로 호출하게 되면 이때 연결할 Controller를 설정 해주어야 한다. 이 기능을 dispatcher-servlet.xml에서 담당한다. 다양한 설정에 태그들이 있지만 기본적으로 있어야할 태그는 3개 이며 어노테이션, viewResolver, base-package경로 설정이 있다.
- viewResolver : JSP파일 경로
- base-package : Controller가 있는 패키지 경로
3. 테스트
4. 사용된 코드
- 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">
<!--한글 인코딩 설정-->
<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>
<!-- 연결할 XML파일 설정 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 공유하는 bean설정 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Controller가 공유하는 Bean들을 포함하는 Spring Container를 생성한다 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- aliases 설정 -->
<servlet>
<!-- 아래 서블릿 매핑에 servlet-name은 반드시 같아야 한다 -->
<!-- 파일 이름 + -servlet.xml로 생성 -->
<servlet-name>dispatcher</servlet-name>
<!-- 개발자에 의해 작성된 실제 클래스 이름 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 작은 수 부터 로드된다 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 서블릿 매핑 -->
<servlet-mapping>
<!-- 매핑할 서블릿 이름 -->
<servlet-name>dispatcher</servlet-name>
<!-- URL주소 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- dispatcher-servlet.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/>
<!--정적자원 매핑-->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!--viewResolver 설정-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- base-package 경로 설정 -->
<context:component-scan base-package="com.modules.home"/>
</beans>
- HomeAction.java
package com.modules.home;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeAction {
@GetMapping(value = "/home1")
public String goHome1(Model model) {
model.addAttribute("data", "1번 페이지");
return "homeWeb/home";
}
@GetMapping(value = "/home2")
public String goHome2(Model model) {
model.addAttribute("data", "2번 페이지");
return "homeWeb/home";
}
}
- home.jsp
<%--
Created by IntelliJ IDEA.
User: rufn4
Date: 2021-10-02
Time: 오후 1:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Spring을 이용한 ${data} 출력 테스트 입니다.
</body>
</html>