IT EDU/SPRING

[Spring] MVC를 위한 Maven 프로젝트 생성

yoonhoou 2022. 3. 10.
728x90

 

 

MVC를 위한 Maven 프로젝트 생성

 

setting 1

New Maven Project

 

Version 1.0

 

Group Id, Artifact Id 입력

 

Dynamic Web Module - 3.1, Java -11, Apache Tomcat - 활성화

 

 


 

setting 2

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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Mvc01</groupId>
	<artifactId>Mvc01</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Mvc01 Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.3.10</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>11</source>
					<target>11</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.3.2</version>
			</plugin>
		</plugins>
	</build>

</project>

 

 


 

setting 3

 

web.xml

 

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://JAVA.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<!--dispatcher 설정 -->
	<servlet-mapping>
		<servlet-name>dispatcher</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>
	</filter>


	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>

 

 


 

setting 4

 

WEB-INF 하위에 dispatcher-servlet.xml 생성

 

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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       
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">



	<!-- 컨트롤러 위치 설정 : 자바코드가 있는 위치 -->
	<context:component-scan base-package="Pack01" />

	<mvc:annotation-driven />
	<mvc:default-servlet-handler />


	<!-- 뷰 위치 설정 : jsp가 있는 위치 -->
	<mvc:view-resolvers>
		<mvc:jsp prefix="/WEB-INF/Views/" />
	</mvc:view-resolvers>

</beans>

 

 


 

setting 5

 

index.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.text.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<head>
	<title>메인 페이지</title>
</head>

<body>
	<%=new Date()%>
	<h2>Hello World</h2>
	
	<!-- t1 컨트롤러로 이동하라 -->
	<a href="t1">링크1</a>
	<br />
</body>

 

Server 실행!

 


 

setting 6

 

main 하위에 java 폴더 생성

 

클래스 생성

 

프로그램 시작 진입점이 java가 아니기 때문에 main함수가 필요없음

 

 

Tiger.java

package Pack01;

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

@Controller
public class Tiger {
	
	// t1이라는 아이디 부여
	@RequestMapping("/t1")
	String func01() {
		System.out.println("호랑이");
		
		// 이동해야 할 view를 return 값으로
		// 밑은 Tiger.jsp 라는 뜻
		// 관례적으로 TigerView 등으로 jsp를 생성해 두어야 한다.
		return "TigerView";
	}
}

 

링크 클릭 시 : Console에 호랑이 호출

 

 


 

setting 7

 

WEB-INF 하위에 Views 폴더 생성

 

TigerView.jsp 생성

 

 

TigerView.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h3>여기는 호랑이</h3>
</body>
</html>
링크 클릭시 : TigerView.jsp로 이동

 

 

MVC Setting 끝!

댓글