티스토리 뷰

728x90
반응형

사전 정보

java 21.0.4 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

 

Tomcat v10.1

Eclipse Version: 2024-06 (4.32.0)

 

Dynamic Web Project

이클립스에서 "Dynamic Web Project"를 생성 합니다.

Context root = / 로 설정 하고 web.xml 생성을 체크 합니다.

Dynamic Web Project
Context root( / ) 및 web.xml 설정

Context root 설정 후 적용이 잘 되었는지 확인.(간혹 Context root 설정이 적용 안되는 현상이 발생하므로 꼭 확인해 보길 바란다.)

Context root 체크


Maven 프로젝트로 전환

프로젝트 선택 후 마우스 우클릭 -> Configure -> Convert to Maven Project 를 선택합니다.

Maven 프로젝트 전환 전
Maven 프로젝트 전환 후


pom.xml 설정

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>testForSpringWebMVC</groupId>
  <artifactId>testForSpringWebMVC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>21</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
  
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>6.1.11</version>
	</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>6.1.11</version>
	</dependency>
    <!--https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
        <scope>provided</scope> <!-- 배포 시에는 포함되지 않습니다. -->
    </dependency>
    <!-- https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
	<dependency>
	    <groupId>jakarta.servlet.jsp.jstl</groupId>
	    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
	    <version>3.0.0</version>
	</dependency>
    
    
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-beans</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context-support</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-expression</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>6.1.11</version>
	    <scope>test</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-tx</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>6.1.11</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
	<dependency>
	    <groupId>jakarta.servlet</groupId>
	    <artifactId>jakarta.servlet-api</artifactId>
	    <version>6.0.0</version>
	    <scope>provided</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/jakarta.servlet.jsp/jakarta.servlet.jsp-api -->
	<dependency>
	    <groupId>jakarta.servlet.jsp</groupId>
	    <artifactId>jakarta.servlet.jsp-api</artifactId>
	    <version>3.1.1</version>
	    <scope>provided</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/jakarta.inject/jakarta.inject-api -->
	<dependency>
	    <groupId>jakarta.inject</groupId>
	    <artifactId>jakarta.inject-api</artifactId>
	    <version>2.0.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/log4j/log4j -->
	<dependency>
	    <groupId>log4j</groupId>
	    <artifactId>log4j</artifactId>
	    <version>1.2.17</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-api</artifactId>
	    <version>2.0.13</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-log4j12</artifactId>
	    <version>2.0.7</version>
	    <type>pom</type>
	    <scope>test</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>jcl-over-slf4j</artifactId>
	    <version>2.0.13</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.2</version>
	</dependency>
    
  </dependencies>
</project>

 

web.xml 설정

DispatcherServlet 및 텍스트 인코딩을 설정합니다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://jakarta.ee/xml/ns/jakartaee"
	xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
	id="WebApp_ID" version="5.0">
	<display-name>com.mvc</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>default.htm</welcome-file>
	</welcome-file-list>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- DispatcherServlet 설정 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/applicationContext.xml</param-value>
		</init-param>
		<!-- 값이 1 이상이면 톰켓(Tomcat) 실행시 DispatcherServlet 을 미리 메모리에 로드합니다. -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- url-pattern 패턴 설정 -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


	<!-- 한글 깨짐 설정(텍스트 인코딩) -->
	<filter>
		<filter-name>encodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

applicationContext.xml 생성 및 설정

WEB-INF 폴더 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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           https://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context 
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc 
           https://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- spring MVC annotation(@)을 사용하기 위한 설정 -->
	<mvc:annotation-driven />
	
	<!-- base-package 속성에 지정된 패키지와 그 하위 패키지에 있는 모든 클래스를 스캔합니다. -->
	<context:component-scan base-package="com.mvc.controller" />

	<!-- controller 를 처리할 view 위치 및 확장자명 설정 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

controller package 및 views 폴더

"com.mvc.controller" package 를 생성합니다.

"views" 폴더를 "WEB-INF" 폴더에 생성합니다.


*.java 생성

"com.mvc.controller" package 에 "MemberController.java" 파일을 생성합니다.

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MemberController {

	@RequestMapping(value = "/member/list", method = RequestMethod.GET)
	public ModelAndView memberList() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("user_id", "jh.jang");
		modelAndView.setViewName("/member/memberList");
		return modelAndView; // WEB-INF/views/member/memberList.jsp로 이동
	}

	@RequestMapping(value = "/member/add", method = RequestMethod.GET)
	public String memberAdd() {
		return "/member/memberAdd"; // // WEB-INF/views/member/memberAdd.jsp로 이동
	}

	@RequestMapping(value = "/member/edit", method = RequestMethod.GET)
	public String memberEdit() {
		return "/member/memberEdit"; // // WEB-INF/views/member/memberEdit.jsp로 이동
	}

	@RequestMapping(value = "/member/delete", method = RequestMethod.GET)
	public String memberDelete() {
		return "/member/memberDelete"; // // WEB-INF/views/member/memberDelete.jsp로 이동
	}

}

*.jsp 생성

"WEB-INF/views/" -> "member" 폴더를 생성한 후 "memberList.jsp" 파일을 생성합니다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"
	isELIgnored="false"%>
	
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	this is a member list page.<br>
	ModelAndView-user_id = ${ user_id }
</body>
</html>

호출

http://localhost:8080/member/list

 

 

추가 정보

Tomcat Clean & Publish


오류 메세지(beans.xml)

Downloading external resources is disabled.

"Download external resources like referenced DTD. XSD" 를 체크해 주면 된다.


web.xml 수동 생성

프로젝트 선택 후 마우스 우클릭 - Java EE Tools → Generate Deployment Descriptor Stub 선택

 

728x90
반응형