๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/WEB

[38์ผ์ฐจ] FirstSpring

by GWLEE 2022. 8. 12.

๐Ÿ’™2022-08-12๐Ÿ’™

 

Spring MVC ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ... web application 

์ผ๋ถ€์ผ ๋ฟ์ด๋‹น..


 

 

war  jar์€ ํด๋ž˜์Šค๋งŒ ๋•Œ๋ ค๋ฐ•์œผ๋ฉด ๋˜๋Š”๋ฐ war๋Š” html, ์ด๋ฏธ์ง€ ์ด๋Ÿฐ๊ฑธ ๋„ฃ์–ด์•ผํ•˜๊ธฐ๋•Œ๋ฌธ์— ์•ˆ๋œ๋‹ต!

dependency maven ๋Œ๊ณ  ์˜ค๊ธฐ!!

https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/4.0.1

https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api

https://mvnrepository.com/artifact/javax.servlet/jstl/1.2

https://mvnrepository.com/artifact/org.springframework/spring-webmvc/5.3.22

 

FirstSpring/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>com.gyuone</groupId>
	<artifactId>FirstSpring</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>11</release>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.3</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.3.22</version>
		</dependency>
	</dependencies>
</project>

 

๋ณด์•ˆ ๋•Œ๋ฌธ์— WEB-INF์— ๋งŒ๋“ค์—ˆ๋‹ค.

์›๋ž˜๋Š” webapp ์•ˆ์— ๋„ฃ์—ˆ๋Š”๋ฐ ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ WEB-INF ์•ˆ์— ๋„ฃ์–ด์•ผํ•œ๋‹ค.

 

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ๋ฐ›๊ณ  ์ฃผ๋Š” ์ปจํŠธ๋กค

์ ๋‹นํ•œ ๋ชจ๋ธ ์ชฝ์— ํ˜ธ์ถœํ•จ

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋“ค์–ด์žˆ๋Š” ๋ชจ๋ธ

์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฟŒ๋ ค์ฃผ๋Š”๊ฒŒ ๋ทฐ

๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜์˜ค๋ฉด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋Š”

๋ทฐ์—์„œ jsp์— ๋„์ง‘์–ด๋‹ค๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’ ๊ตฌํ•œ ๊ฐ’๋“ค์„ jsp์— ๋‹ค ๊ฐ€์ ธ์„œ client๊ฐ€ ๋˜์ ธ์คŒ.

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<display-name>FirstSpring</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<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>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextClass</param-name>
			<param-value>
				org.sprinframework.web.context.support.AnnotationConfigWebApplicationContext
			</param-value>
		</init-param>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				com.gyuone.config.MvcConfig
				com.gyuone.config.ControllerConfig
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup> 
	</servlet>
</web-app>

ํ†ฐ์บฃํŒŒ์ผ ๊ตฌ๋™ํ•˜๋ผ load on startup

 

 

 

 

๊ทธ๋Œ€๋กœ ํ†ฐ์บฃ ์ƒํƒœ๋กœ ์ „๋‹ฌ Dispatcher Servelt ์ด ๋ฐ›๋Š” ๊ฒƒ Dispatcher servelt์ด ์–ด๋–ค ์š”์ฒญ์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ณด๋‚ธ๋‹ค.

๋ชจ๋ธ์„ ์•ˆ๋งŒ๋“ค์—ˆ์ง€๋งŒ, ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ๋ชจ๋ธ๋กœ ์ ‘๊ทผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋“ค์–ด๊ฐ. ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊นŒ์ง€ ์ ‘๊ทผํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ค€๋‹ค. 

 

 

 

 

 

127.0.0.1:8080/FirstSpring/hello?name=kim

 

 

 

 

์ ‘๊ทผ์ด ์•ˆ๋œ๋‹ค. view ํŒŒ์ผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋Š” ์ด์œ  

 

 

NGINX๋ฅผ ์„ค์น˜ํ•˜๊ณ  ํ†ฐ์บฃ์œผ๋กœ ์ง‘์–ด ๋„ฃ์–ด ๋ฒ„๋ฆฌ๋Š” ..

 

<< ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• >>

 

์ดํด๋ฆฝ์Šค ์„œ๋ฒ„ ์ฃฝ์ด๊ณ  ์‹œ์ž‘ํ•  ๊ฒƒ ์ถฉ๋Œ์ฃผ์˜ !

 

spring์œผ๋กœ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ

 

E:\apache-tomcat-9.0.65\bin startup.bat ์‹คํ–‰ 

 

spring์œผ๋กœ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. 

 

 

 

 

 

 

com.gyuone

config

ControllerConfig.java

package com.gyuone.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gyuone.controller.HelloController;

@Configuration // Spring์šฉ ์„ค์ • ํด๋ž˜์Šค
public class ControllerConfig {
	@Bean
	public HelloController helloController() {
		return new HelloController();
	}
}

 

MvcConfig.java

package com.gyuone.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer{

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		// TODO Auto-generated method stub
		configurer.enable();
	}

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		// TODO Auto-generated method stub
		registry.jsp("/WEB-INF/view/", ".jsp"); // .jsp๋ฅผ ์•Œ์•„์„œ ๋ถ™์—ฌ๋ผ.
	}
	
	
}

 

 

 

controller

HelloController.java

package com.gyuone.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller // ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š” controller
public class HelloController {
	@GetMapping("/hello") // get๋ฐฉ์‹์œผ๋กœ ๋“ค์–ด์™”์„ ๋•Œ hello ๋ฉ”์†Œ๋“œ ์‹คํ–‰
	public String hello(Model model, @RequestParam(value="name", required = false) String name) { // RequestParam ํด๋ผ์ด์–ธํŠธ์—์„œ name์ด๋ผ๋Š” ํ•ญ๋ชฉ์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค. 
		model.addAttribute("greeting","์•ˆ๋…•ํ•˜์„ธ์š”, " + name); // greeting ํ‚ค๊ฐ’. ํด๋ผ์ด์–ธํŠธ ๋ชจ๋ธํ˜•์œผ๋กœ ์ „๋‹ฌ.
		return "hello"; // string ์œผ๋กœ ๋ฆฌํ„ด ์ •์ฒด: view name ์ด๋ฆ„๋งŒ ๋˜์ ธ์ฃผ๋Š” ๊ฒƒ hello.jsp๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.
		// ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์ด ์—†๋Š” ๊ตฌ์กฐ
	}
}

 

 

WEB-INF

view

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
	์ธ์‚ฌ๋ง : ${greeting} 
</body>
</html>

 

 

FirstSpring/hello?name=kim์„ ์ž…๋ ฅํ•˜๋ฉด.. 

 

 


 

๋Œ“๊ธ€