πŸ’»Coding/πŸ“• spring

[spring] μŠ€ν”„λ§ ν”„λ‘œμ νŠΈ λ§Œλ“€κΈ°

μŒμ€μ‘ 2022. 6. 10. 11:44
728x90
λ°˜μ‘ν˜•

[spring] μŠ€ν”„링 ν”„λ‘œμ νŠΈ λ§Œλ“€κΈ°

βœ… Dynamic Web Project λ§Œλ“€κΈ° μ„€μ • 방법

 

 

βœ… 메이븐 ν”„λ‘œμ νŠΈλ‘œ λ³€κ²½ν•˜λŠ” 방법

μ½”λ“œ μΆ”κ°€μ‹œ λ‹€μš΄λ‘œλ“œ μžλ™

βœ… pom.xml μ„€μ • : Spring Library μ˜μ‘΄μ„± μΆ”κ°€ μ½”λ“œ ↓
βœ… λ‹€μš΄λ‘œλ“œ ν•  것 <dependencies> ~ </dependencies> μ•ˆμ— μ½”λ“œ λ„£κΈ°
<!-- 4.3.3.RELEASE 버전을 μ“°κΈ° μœ„ν•΄ -->
	<properties>
		<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
	</properties>

	<!-- λ‹€μš΄λ‘œλ“œ ν•  것 dependencies μ•ˆμ— μ½”λ“œ λ„£κΈ° -->
	<dependencies>
		<!-- 1. spring container(core) -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- 2. Spring Web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- 3. Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
	</dependencies>

 


βœ… μ½”λ“œ μž‘μ„± ν›„, μ €μž₯ ν•˜λ©΄ μƒκΈ°λŠ” λ³€ν™” : Maven Dependencies 생성

 

βœ… web.xml : DispatcherServlet μ •μ˜

<!-- DispatChServlet Mapping : Front Controller 등둝 -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
βœ… web.xml : ν•œκΈ€μ²˜λ¦¬
→ μœ„μ˜ μ½”λ“œ λ°”λ‘œ 밑에 μΆ”κ°€λ‘œ μž‘μ„±
<!-- ν•œκΈ€μ²˜λ¦¬ -->
	<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-INF 폴더 μ•ˆμ— spring-servlet.xml 파일 생성 ν›„, μ•„λž˜ μ½”λ“œ μž…λ ₯

βœ…  /WEB-INF/spring-servlet.xml λ§Œλ“  ν›„ μ•„λž˜ μ½”λ“œ μž…λ ₯ ↓
<?xml version="1.0" encoding="UTF-8"?>
<!-- μ•„λž˜ μ½”λ“œ ctrl+c , v -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	xmlns:mvc="http://www.springframework.org/schema/mvc">

	<!-- annotation 섀정을 ν•˜κ² λ‹€. -->
	<context:annotation-config />

	<!-- com.javaex.controller νŒ¨ν‚€μ§€ 밑에 μžˆλŠ” 클래슀 쀑에 @Controllerλ₯Ό 달고 μžˆλŠ” 클래슀의 객체λ₯Ό 
		생성 ν•˜κ² λ‹€. (new ν•˜κ² λ‹€) -->
	<context:component-scan
		base-package="com.javaex.controller" />

</beans>

 

βœ… src/main/java 폴더 μ•ˆμ— com.javaex.controller νŒ¨ν‚€μ§€ 생성
β‡’ class 생성 ν›„, Hello.java 파일 λ§Œλ“€κΈ°
β‡’ μ•„λž˜ μ½”λ“œλ‘œ controller μž‘μ„±ν•˜κΈ°
β‡’ @Controller / @RequestMapping μ½”λ“œ 볡뢙 ν›„, ctrl + shift + o 둜 import μƒμ„±ν•˜κΈ°

package com.javaex.controller;

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

@Controller
public class Hello {
	
	@RequestMapping( "/hello")
	public String hello(){
	System.out.println("/hellospring/hello");
	return "/WEB-INF/views/index.jsp";
	}

}
@Controller /  @RequestMapping μ½”λ“œ 볡뢙 ν›„, ctrl + shift + o 둜 import 생성후 ν™”λ©΄ λ³€ν™”

βœ… index.jsp 파일 λ§Œλ“€κΈ° : view μž‘μ„±ν•˜κΈ°
β‡’ WEB-INF 폴더 μ•ˆμ— views 폴더 생성 ν›„, index.jsp 파일 λ§Œλ“€κΈ°
β‡’ index.jsp μ•ˆμ— μ½”λ“œ μž‘μ„±

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- ν˜ΈμΆœμ£Όμ†Œ : localhost:8088/hellospring/hello -->
	<h1>hello spring ν—¬λ‘œ μŠ€ν”„λ§ !@#$%^*()</h1>
</body>
</html>

 

μ‹€ν–‰ν™”λ©΄μœΌλ‘œ ν™•μΈν•˜κΈ°
βœ… localhost:8088/hellospring/hello μ£Όμ†Œλ‘œ 확인해보기
β‡’ localhost:번호/νŒŒμΌμœ„μΉ˜

728x90
λ°˜μ‘ν˜•