SpringMVC Annotation

woomΒ·2023λ…„ 2μ›” 22일
0

Framework

λͺ©λ‘ 보기
13/20
post-thumbnail

🌼 SpringMVC (Annotation)

  • Annotation μ‚¬μš© : Sprinv MVC μ—­μ‹œ XML 섀정을 μ΅œμ†Œν™”ν•  수 μžˆλ„λ‘ Annotation 기반 섀정을 μ§€μ›ν•˜λŠ”λ° κΈ°λ³Έ 섀정은 IoC μ„€μ •κ³Ό 동일함
  1. context κ΄€λ ¨ Namespace μ„ μ–Έ
  2. <context:component-scan> μ΄λΌλŠ” νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜μ—¬ Controller Annotation μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”
  3. μŠ€μΊ”ν•  ν΄λž˜μŠ€λ“€μ˜ νŒ¨ν‚€μ§€λ₯Ό base-package 속성을 μ΄μš©ν•΄μ„œ 등둝

πŸ“Œ web.xml (appServlet)

  • μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜λŠ” component-scan μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•œ Spring Bean Configuration File은 ν•˜λ‚˜λ§Œ 제곡 κ°€λŠ₯

    • <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  • url-pattern μ—˜λ¦¬λ¨ΌνŠΈμ˜ λ‚΄μš©μ„ [/]둜 μ„€μ •ν•œ 경우 ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μ›Ήν”„λ‘œκ·Έλž¨ μš”μ²­μ— λŒ€ν•œ μ²˜λ¦¬κ°€ κ°€λŠ₯ν•˜λ„λ‘ μ„€μ •

    • <url-pattern>/</url-pattern>


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<!-- context-param : λͺ¨λ“  μ›Ήν”„λ‘œκ·Έλž¨ 싀행에 ν•„μš”ν•œ 값을 μ œκ³΅ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- => contextConfigLocation μ΄λ¦„μœΌλ‘œ μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆ(WebApplocationContext 객체)μ—κ²Œ
	Spring Bean Configuration File의 경둜λ₯Ό 제곡 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- λͺ¨λ“  μ›Ήν”„λ‘œκ·Έλž¨μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” Spring Beanλ₯Ό λ“±λ‘ν•˜κΈ° μœ„ν•œ Spring Bean Configuration File -->
		<!-- => λͺ¨λ“  Front Controller μ—­ν™œμ„ ν•˜λŠ” μ›Ήν”„λ‘œκ·Έλž¨μ—κ²Œ ν•„μš”ν•œ 객체λ₯Ό μ œκ³΅ν•˜κΈ° μœ„ν•΄ Spring Bean으둜 등둝  -->
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<!-- listener : WAS ν”„λ‘œκ·Έλž¨μ΄ μ‹œμž‘λ  λ•Œ Listener 클래슀λ₯Ό μ œκ³΅λ°›μ•„ 객체둜 μƒμ„±ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- Listener 클래슀 : ServletContextListener μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받은 μžμ‹ν΄λž˜μŠ€ -->
	<!-- => contextInitialized λ©”μ†Œλ“œ : Listener 객체 생성 ν›„ μžλ™ ν˜ΈμΆœλ˜λŠ” λ©”μ†Œλ“œ - μ΄ˆκΈ°ν™” μž‘μ—… -->
	<!-- => contextDestroyed λ©”μ†Œλ“œ : Listener 객체 μ†Œλ©Έ μ „ μžλ™ ν˜ΈμΆœλ˜λŠ” λ©”μ†Œλ“œ - 마무리 μž‘μ—… -->
	<listener>
		<!-- listener-class : ServletContextListener μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받은 μžμ‹ν΄λž˜μŠ€λ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- => WAS ν”„λ‘œκ·Έλž¨μ΄ μ‹œμž‘λ  λ•Œ ContextLoaderListener 클래슀λ₯Ό 객체둜 μƒμ„±ν•˜κ³ 
		contextInitialized λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ μ΄ˆκΈ°ν™” μž‘μ—… -->
		<!-- ContextLoaderListener 클래슀의 contextInitialized λ©”μ†Œλ“œμ—μ„œ μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆ(Spring Container)λ₯Ό
		μƒμ„±ν•˜κ³  Spring Bean Configuration File을 μ œκ³΅λ°›μ•„ Spring Bean으둜 λ“±λ‘λœ 클래슀λ₯Ό 객체둜 생성 -->
		<!-- Listener 객체의 λ©”μ†Œλ“œμ— μ˜ν•΄ μƒμ„±λœ μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆ(WebApplicationContext 객체)λŠ”
		λͺ¨λ“  μ›Ήμžμ›(μ„œλΈ”λ¦Ώ 및 ν•„ν„°)μ—κ²Œ κ³΅μœ λ˜μ–΄ μ‚¬μš© κ°€λŠ₯ - Spring Bean 제곡  -->
		<!-- => WebApplicationContext κ°μ²΄λŠ” 기본적으둜 src/main/webapp ν΄λ”μ˜ Spring Bean 
		Configuration File 파일만 μ œκ³΅λ°›μ•„ 처리 κ°€λŠ₯ -->
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- servlet : μ„œλΈ”λ¦Ώ 클래슀λ₯Ό μ„œλΈ”λ¦Ώ(μ›Ήν”„λ‘œκ·Έλž¨)으둜 λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<servlet>
		<!-- servlet-name : μ„œλΈ”λ¦Ώμ„ κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<servlet-name>mvc</servlet-name>
		<!-- servlet-class : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 객체둜 μƒμ„±λ˜μ–΄ 싀행될 μ„œλΈ”λ¦Ώ ν΄λž˜μŠ€μ„
		μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- DispatcherServlet 클래슀 : 컨트둀러(Controller) μ—­ν™œμ„ μ œκ³΅ν•˜λŠ” 클래슀 - Front Controller -->
		<servlet-class>xyz.itwill09.mvc.DispatcherServlet</servlet-class>
	</servlet>
	
	<!-- servlet-mapping : μ„œλΈ”λ¦Ώμ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•œ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<servlet-mapping>
		<!-- servlet-name : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 싀행될 μ„œλΈ”λ¦Ώμ˜ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<servlet-name>mvc</servlet-name>
		<!-- url-pattern : μ„œλΈ”λ¦Ώμ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•œ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- => Front Controller μ—­ν™œμ„ μ œκ³΅ν•˜λŠ” μ„œλΈ”λ¦Ώμ€ ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­μ„ λ°›μ•„ μ²˜λ¦¬λ˜λ„λ‘ μ„€μ • -->
		<url-pattern>*.itwill</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>springMvc</servlet-name>
		<!-- Spring Framework의 λΌμ΄λΈŒλŸ¬λ¦¬μ— μ˜ν•΄ μ œκ³΅λ˜λŠ” DispatcherServlet 클래슀 μ‚¬μš© - Front Controller -->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- init-param : μ„œλΈ”λ¦Ώ ν΄λž˜μŠ€μ—κ²Œ 싀행에 ν•„μš”ν•œ 값을 μ œκ³΅ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- => contextConfigLocation μ΄λ¦„μœΌλ‘œ μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆ(WebApplicationContext 객체)μ—κ²Œ
		Spring Bean Configuration File의 경둜λ₯Ό 제곡 -->
		<!-- => ν˜„μž¬ μ„€μ •λœ Front Controller μ—­ν™œμ˜ κ°μ²΄μ—κ²Œλ§Œ μ œκ³΅ν•œ Spring Bean을 λ“±λ‘ν•˜κΈ° 
		μœ„ν•œ Spring Bean Configuration File -->
		<!-- => Spring Bean Configuration File은 μ—”ν„°(Enter) λ˜λŠ” [,] 및 [;]으둜 κ΅¬λΆ„ν•˜μ—¬ μ—¬λŸ¬κ°œ 제곡 κ°€λŠ₯ -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/spring/springMvc/mvc-context1.xml
				/WEB-INF/spring/springMvc/mvc-context2.xml
			</param-value>
		</init-param>
		<!-- load-on-startup : WAS ν”„λ‘œκ·Έλž¨μ΄ μ‹œμž‘λ  λ•Œ μ„œλΈ”λ¦Ώ 클래슀λ₯Ό 미리 객체둜 μƒμ„±ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- => ν΄λΌμ΄μ–ΈνŠΈκ°€ μ„œλΈ”λ¦Ώ(Front Controller)λ₯Ό μš”μ²­ν•˜μ§€ μ•Šμ•„λ„ μ„œλΈ”λ¦Ώ 객체λ₯Ό 미리 생성 -->
		<!-- => μ—˜λ¦¬λ¨ΌνŠΈ λ‚΄μš©μœΌλ‘œ μ„€μ •λœ μ •μˆ˜κ°’μ„ μž‘μ„μˆ˜λ‘ λ¨Όμ € μ„œλΈ”λ¦Ώ 객체둜 생성 -->
		<load-on-startup>0</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜λŠ” component-scan μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•œ Spring Bean 
			Configuration File은 ν•˜λ‚˜λ§Œ 제곡 κ°€λŠ₯ -->
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<!-- url-pattern μ—˜λ¦¬λ¨ΌνŠΈμ˜ λ‚΄μš©μ„ [/]둜 μ„€μ •ν•œ 경우 ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μ›Ήν”„λ‘œκ·Έλž¨
		μš”μ²­μ— λŒ€ν•œ μ²˜λ¦¬κ°€ κ°€λŠ₯ν•˜λ„λ‘ μ„€μ • -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>





πŸ“• servlet-context.xml

  • servlet-context.xml : appServlet μ΄λ¦„μ˜ Front Controllerμ—μ„œ μ‚¬μš©λ  클래슀의 Spring Bean을 λ“±λ‘ν•˜κΈ° μœ„ν•œ Spring Bean Configuration File
  • annotation-driven : @Controller μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μš”μ²­ 처리 클래슀λ₯Ό Spring Bean으둜 λ“±λ‘ν•˜κ³  @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μž‘μ„±λœ λ©”μ†Œλ“œλ₯Ό μš”μ²­ 처리 λ©”μ†Œλ“œλ‘œ λ“±λ‘ν•˜μ—¬ ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ μš”μ²­ 처리 클래슀의 λ©”μ†Œλ“œκ°€ ν˜ΈμΆœλ˜μ–΄ μ²˜λ¦¬λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ
    • HandlerMapping 객체λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ μš”μ²­ 처리 클래슀의 μš”μ²­ 처리 λ©”μ†Œλ“œκ°€ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ— μ˜ν•΄ ν˜ΈμΆœλ˜λ„λ‘ μžλ™μœΌλ‘œ 맀핑 처리
  • InternalResourceViewResolver 클래슀λ₯Ό Spring Bean으둜 등둝
    • InternalResourceViewResolver 객체 : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜κ°’(ViewName)을 μ œκ³΅λ°›μ•„ 응닡 κ°€λŠ₯ν•œ JSP λ¬Έμ„œλ‘œ λ³€ν™˜ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” 객체
  • component-scan : μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜μ—¬ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ
    • base-package 속성 : μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜κΈ° μœ„ν•΄ μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 클래슀의 νŒ¨ν‚€μ§€λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • xyz.itwill10.controller νŒ¨ν‚€μ§€μ— μš”μ²­ 처리 클래슀(Model)λ₯Ό μž‘μ„±ν•˜κ³  μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ μ΄μš©ν•˜μ—¬ Spring Bean으둜 등둝
    • <context:component-scan base-package="xyz.itwill10.controller" />


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

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	<!-- servlet-context.xml : appServlet μ΄λ¦„μ˜ Front Controllerμ—μ„œ μ‚¬μš©λ  클래슀의 Spring
	Bean을 λ“±λ‘ν•˜κΈ° μœ„ν•œ Spring Bean Configuration File -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<!-- annotation-driven : @Controller μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μš”μ²­ 처리 클래슀λ₯Ό Spring Bean으둜
	λ“±λ‘ν•˜κ³  @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μž‘μ„±λœ λ©”μ†Œλ“œλ₯Ό μš”μ²­ 처리 λ©”μ†Œλ“œλ‘œ λ“±λ‘ν•˜μ—¬
	ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ μš”μ²­ 처리 클래슀의 λ©”μ†Œλ“œκ°€ ν˜ΈμΆœλ˜μ–΄ μ²˜λ¦¬λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- => HandlerMapping 객체λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ μš”μ²­ 처리 클래슀의 μš”μ²­ 처리 λ©”μ†Œλ“œκ°€ 
	ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ— μ˜ν•΄ ν˜ΈμΆœλ˜λ„λ‘ μžλ™μœΌλ‘œ 맀핑 처리 -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<!-- InternalResourceViewResolver 클래슀λ₯Ό Spring Bean으둜 등둝 -->
	<!-- InternalResourceViewResolver 객체 : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œμ˜
	λ°˜ν™˜κ°’(ViewName)을 μ œκ³΅λ°›μ•„ 응닡 κ°€λŠ₯ν•œ JSP λ¬Έμ„œλ‘œ λ³€ν™˜ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” 객체 -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<!-- component-scan : μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜μ—¬ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- base-package 속성 : μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„ κ²€μƒ‰ν•˜κΈ° μœ„ν•΄ μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 클래슀의 νŒ¨ν‚€μ§€λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ • -->	
	<!-- <context:component-scan base-package="xyz.itwill.controller" /> -->
	<!-- xyz.itwill10.controller νŒ¨ν‚€μ§€μ— μš”μ²­ 처리 클래슀(Model)λ₯Ό μž‘μ„±ν•˜κ³  μŠ€ν”„λ§ μ–΄λ…Έν…Œμ΄μ…˜μ„
	μ΄μš©ν•˜μ—¬ Spring Bean으둜 등둝 -->
	<context:component-scan base-package="xyz.itwill10.controller" />
</beans:beans>





πŸ“™ Model (ν¬μ›Œλ“œ 이동)

  • @Controller : μš”μ²­ 처리 클래슀λ₯Ό Spring Bean으둜 λ“±λ‘ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • 클래슀의 이름을 Spring Bean의 μ‹λ³„μž(beanName)으둜 μžλ™ μ„€μ • (μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜)
    • value 속성을 μ‚¬μš©ν•˜μ—¬ Spring Bean의 μ‹λ³„μž(beanName) λ³€κ²½ κ°€λŠ₯
    • Controller μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받지 μ•Šμ•„λ„ μš”μ²­ 처리 클래슀둜 처리
    • @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ λ‹€μˆ˜μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•˜μ—¬ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ— λŒ€ν•œ 처리 κ°€λŠ₯
  • μš”μ²­ 처리 λ©”μ†Œλ“œλŠ” Front Controllerμ—κ²Œ λ°˜λ“œμ‹œ 뷰이름(ViewName) 제곡

    • Front ControllerλŠ” μ œκ³΅λ°›μ€ 뷰이름을 μ΄μš©ν•˜μ—¬ 응닡 κ΄€λ ¨ μ •λ³΄λ‘œ λ³€ν™˜(ViewResolver)ν•˜μ—¬ 응닡 처리
  • Front Controller에 μ˜ν•΄ JSP λ¬Έμ„œλ‘œ ν¬μ›Œλ“œ μ΄λ™ν•˜μ—¬ μ‘λ‹΅λ˜λ„λ‘ 뷰이름을 μ œκ³΅ν•˜λŠ” 방법
    • JSP λ¬Έμ„œλ‘œ 응닡 μ²˜λ¦¬ν•˜μ§€ μ•Šμ„ 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ Model λ˜λŠ” Map으둜 μ„€μ • κ°€λŠ₯
  1. μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ void둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ λ©”μ†Œλ“œμ˜ 이름을 λ·°μ΄λ¦„μœΌλ‘œ 제곡

  2. μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ String둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ λ°˜ν™˜κ°’μ„ λ·°μ΄λ¦„μœΌλ‘œ 제곡

  3. μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ ModelAndView둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ ModelAndView 객체에 뷰이름을 μ €μž₯ν•˜μ—¬ 제곡

    • ModelAndView 객체 : μš”μ²­μ— λŒ€ν•œ 처리 κ²°κ³Όλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  뷰이름을 μ €μž₯ν•˜κΈ° μœ„ν•œ 객체
    • modelAndView.setViewName("hello"); : Setter λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ 뷰이름 λ³€κ²½
    • ModelAndView modelAndView=new ModelAndView("hello"); : μƒμ„±μžλ₯Ό μ΄μš©ν•˜μ—¬ 뷰이름 λ³€κ²½
  • @RequestMapping : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ„ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • 기본적으둜 ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­λ°©μ‹(Method - GET, POST, PUT, PATCH, DELETE λ“±)에 μ˜ν•΄ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•  경우 μ‚¬μš©ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
    • ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ„ κ΅¬λΆ„ν•˜μ—¬ μš”μ²­ 처리 λ©”μ†Œλ“œκ°€ ν˜ΈμΆœλ˜λ„λ‘ μ²˜λ¦¬ν•˜κ³ μž ν•  경우 @GetMaaping, @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping λ“±μ˜ μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯
    • value 속성 : ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†Œλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • Front Controller에 μ˜ν•΄ λ“±λ‘λœ ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­ URL μ£Όμ†Œ(μ‹λ³„μž)둜 μš”μ²­ 처리 λ©”μ†Œλ“œκ°€ μžλ™ ν˜ΈμΆœλ˜μ–΄ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ 처리
    • λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
    • λ‹€λ₯Έ μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ value 속성값과 μš”μ²­ URL μ£Όμ†Œκ°€ 쀑볡될 경우 WAS μ‹œμž‘μ‹œ μ—λŸ¬ λ°œμƒ


package xyz.itwill10.controller;

//@Controller : μš”μ²­ 처리 클래슀λ₯Ό Spring Bean으둜 λ“±λ‘ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
// => 클래슀의 이름을 Spring Bean의 μ‹λ³„μž(beanName)으둜 μžλ™ μ„€μ • - μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜
// => value 속성을 μ‚¬μš©ν•˜μ—¬ Spring Bean의 μ‹λ³„μž(beanName) λ³€κ²½ κ°€λŠ₯
// => Controller μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받지 μ•Šμ•„λ„ μš”μ²­ 처리 클래슀둜 처리
// => @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ λ‹€μˆ˜μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•˜μ—¬ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ— λŒ€ν•œ 처리 κ°€λŠ₯
@Controller
public class HelloController {//λͺ¨λΈ(Model) μ—­ν™œμ„ μ œκ³΅ν•˜λŠ” 클래슀 - μš”μ²­ 처리 클래슀
	private static final Logger logger=LoggerFactory.getLogger(HelloController.class);

	//μš”μ²­ 처리 λ©”μ†Œλ“œλŠ” Front Controllerμ—κ²Œ λ°˜λ“œμ‹œ 뷰이름(ViewName) 제곡
	// => Front ControllerλŠ” μ œκ³΅λ°›μ€ 뷰이름을 μ΄μš©ν•˜μ—¬ 응닡 κ΄€λ ¨ μ •λ³΄λ‘œ λ³€ν™˜(ViewResolver)ν•˜μ—¬ 응닡 처리
	//Front Controller에 μ˜ν•΄ JSP λ¬Έμ„œλ‘œ ν¬μ›Œλ“œ μ΄λ™ν•˜μ—¬ μ‘λ‹΅λ˜λ„λ‘ 뷰이름을 μ œκ³΅ν•˜λŠ” 방법 
	// => JSP λ¬Έμ„œλ‘œ 응닡 μ²˜λ¦¬ν•˜μ§€ μ•Šμ„ 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ Model λ˜λŠ” Map으둜 μ„€μ • κ°€λŠ₯
	//1.μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ void둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ λ©”μ†Œλ“œμ˜ 이름을 λ·°μ΄λ¦„μœΌλ‘œ 제곡 
	//2.μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ String둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ λ°˜ν™˜κ°’μ„ λ·°μ΄λ¦„μœΌλ‘œ 제곡 
	//3.μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ°˜ν™˜ν˜•μ„ ModelAndView둜 μ„€μ •ν•œ 경우 Front Controllerμ—κ²Œ ModelAndView 객체에 뷰이름을 μ €μž₯ν•˜μ—¬ 제곡
	
	//@RequestMapping : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ„ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	// => 기본적으둜 ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­λ°©μ‹(Method - GET, POST, PUT, PATCH, DELETE λ“±)에
	//μ˜ν•΄ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•  경우 μ‚¬μš©ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
	// => ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ„ κ΅¬λΆ„ν•˜μ—¬ μš”μ²­ 처리 λ©”μ†Œλ“œκ°€ ν˜ΈμΆœλ˜λ„λ‘ μ²˜λ¦¬ν•˜κ³ μž ν•  경우 @GetMaaping
	//, @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping λ“±μ˜ μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯
	//value 속성 : ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†Œλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => Front Controller에 μ˜ν•΄ λ“±λ‘λœ ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­ URL μ£Όμ†Œ(μ‹λ³„μž)둜 μš”μ²­ 처리 λ©”μ†Œλ“œκ°€
	//μžλ™ ν˜ΈμΆœλ˜μ–΄ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ 처리
	// => λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
	// => λ‹€λ₯Έ μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ value 속성값과 μš”μ²­ URL μ£Όμ†Œκ°€ 쀑볡될 경우 WAS μ‹œμž‘μ‹œ μ—λŸ¬ λ°œμƒ
	@RequestMapping(value = "/hello")
	public void hello() {//μš”μ²­ 처리 λ©”μ†Œλ“œ
		//μš”μ²­ 처리 λͺ…λ Ή μž‘μ„± - Service 객체의 λ©”μ†Œλ“œ 호좜
		logger.info("[/hello] νŽ˜μ΄μ§€ μš”μ²­ : HelloController 클래슀의 hello() λ©”μ†Œλ“œ 호좜");
	}
	
	@RequestMapping(value = "/helloViewName")
	public String helloViewName() {
		logger.info("[/helloViewName] νŽ˜μ΄μ§€ μš”μ²­ : HelloController 클래슀의 helloViewName() λ©”μ†Œλ“œ 호좜");
		return "hello";
	}
	
	@RequestMapping(value = "/helloModelAndView")
	public ModelAndView helloModelAndView() {
		logger.info("[/helloModelAndView] νŽ˜μ΄μ§€ μš”μ²­ : HelloController 클래슀의 helloModelAndView() λ©”μ†Œλ“œ 호좜");
		
		//ModelAndView 객체 : μš”μ²­μ— λŒ€ν•œ 처리 κ²°κ³Όλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  뷰이름을 μ €μž₯ν•˜κΈ° μœ„ν•œ 객체
		//ModelAndView modelAndView=new ModelAndView();
		//modelAndView.setViewName("hello");//Setter λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ 뷰이름 λ³€κ²½
		
		ModelAndView modelAndView=new ModelAndView("hello");//μƒμ„±μžλ₯Ό μ΄μš©ν•˜μ—¬ 뷰이름 λ³€κ²½
		
		return modelAndView;
	}
}





🐣 응닡 (뷰이름)


  • hello(), helloViewName(), helloModelAndView() λ©”μ†Œλ“œ λͺ¨λ‘ λ™μΌν•œ λ·° 제곡
    • μš”μ²­ 처리 λ©”μ†Œλ“œλ‘œ ꡬ뢄
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>Hello, Spring!!!</h1>
	<hr>
</body>
</html>





πŸ“™ Model (처리 κ²°κ³Ό return)

  • μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 처리된 κ²°κ³Όλ₯Ό λ·°(JSP λ¬Έμ„œ)에 μ œκ³΅ν•˜λŠ” 방법

  • μš”μ²­ 처리 λ©”μ†Œλ“œλŠ” Front Controller에 μ˜ν•΄ μžλ™ ν˜ΈμΆœλ˜λŠ” λ©”μ†Œλ“œ

    • μš”μ²­ 처리 λ©”μ†Œλ“œμ— λ§€κ°œλ³€μˆ˜λ₯Ό μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆλ‘œλΆ€ν„° λ§€κ°œλ³€μˆ˜μ— μ €μž₯ κ°€λŠ₯ν•œ μ›Ή κ΄€λ ¨ 객체λ₯Ό μ œκ³΅λ°›μ•„ μ „λ‹¬ν•˜μ—¬ μ €μž₯
  1. ModelAndView 객체에 처리결과λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ 제곡
    • ModelAndView.addObject(String attributeName, Object attributeValue) : ModelAndView 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ (Request Scope)
    • modelAndView.addObject("mavName","홍길동");
    • λ·°(JSP λ¬Έμ„œ)μ—μ„œλŠ” EL ν‘œν˜„μ‹μœΌλ‘œ 속성λͺ…을 μ‚¬μš©ν•˜μ—¬ 속성값을 μ œκ³΅λ°›μ•„ 좜λ ₯
  1. HttpServletRequest 객체에 처리결과λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ 제곡
    • HttpServletRequest.setAttribute(String attributeName, Object attributeValue) : HttpServletRequest 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ (Request Scope)
    • request.setAttribute("requestName", "μž„κΊ½μ •");
  1. Model 객체에 처리결과λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ 제곡
    • Model 객체 : 처리결과λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°(View)μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ 객체
    • Model.addAttribute(String attributeName, Object attributeValue) : Model 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ (Request Scope)


package xyz.itwill10.controller;

@Controller
public class ResultController {
	/*
	@RequestMapping("/resultMAV")
	public ModelAndView modelAndViewResult() {
		ModelAndView modelAndView=new ModelAndView("result_display");
		//ModelAndView.addObject(String attributeName, Object attributeValue)
		// => ModelAndView 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ - Request Scope
		// => λ·°(JSP λ¬Έμ„œ)μ—μ„œλŠ” EL ν‘œν˜„μ‹μœΌλ‘œ 속성λͺ…을 μ‚¬μš©ν•˜μ—¬ 속성값을 μ œκ³΅λ°›μ•„ 좜λ ₯
		modelAndView.addObject("mavName","홍길동");
		return modelAndView;
	}
	*/
	
	//μš”μ²­ 처리 λ©”μ†Œλ“œλŠ” Front Controller에 μ˜ν•΄ μžλ™ ν˜ΈμΆœλ˜λŠ” λ©”μ†Œλ“œ
	// => μš”μ²­ 처리 λ©”μ†Œλ“œμ— λ§€κ°œλ³€μˆ˜λ₯Ό μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆλ‘œλΆ€ν„°
	//λ§€κ°œλ³€μˆ˜μ— μ €μž₯ κ°€λŠ₯ν•œ μ›Ή κ΄€λ ¨ 객체λ₯Ό μ œκ³΅λ°›μ•„ μ „λ‹¬ν•˜μ—¬ μ €μž₯
	@RequestMapping("/resultMAV")
	public ModelAndView modelAndViewResult(ModelAndView modelAndView) {
		modelAndView.setViewName("result_display");
		modelAndView.addObject("mavName","홍길동");
		return modelAndView;
	}
	
	@RequestMapping("/resultRequest")
	public String requestResult(HttpServletRequest request) {
		//HttpServletRequest.setAttribute(String attributeName, Object attributeValue)
		// => HttpServletRequest 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ - Request Scope
		request.setAttribute("requestName", "μž„κΊ½μ •");
		return "result_display";
	}
	
	@RequestMapping("/resultModel")
	public String modelResult(Model model) {
		//Model 객체 : 처리결과λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°(View)μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ 객체
		//Model.addAttribute(String attributeName, Object attributeValue)
		// => Model 객체에 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ - Request Scope
		model.addAttribute("modelName", "μ „μš°μΉ˜");
		return "result_display";
	}
}





🐣 응닡 (속성λͺ…-속성값)


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>처리결과 좜λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<h2>ModelAndView = ${mavName }</h2>
	<h2>HttpServletRequest = ${requestName }</h2>
	<h2>Model = ${modelName }</h2>
</body>	
</html>





πŸ“™ @ModelAttribute (method)

  • μš”μ²­ 처리 클래슀의 λ©”μ†Œλ“œμ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš©

    • @ModelAttribute : λ©”μ†Œλ“œμ— μ˜ν•΄ λ°˜ν™˜λ˜λŠ” 객체(κ°’)을 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ ν˜„μž¬ ν΄λž˜μŠ€μ— μž‘μ„±λœ λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°(JSP λ¬Έμ„œ)μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 응닡될 λͺ¨λ“  λ·°μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” 객체(κ°’)을 μ œκ³΅ν•  λͺ©μ μœΌλ‘œ μ‚¬μš©
    • value 속성 : λ·°μ—μ„œ 속성값을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ 이름(속성λͺ…)을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • λ‹€λ₯Έ 속성이 μ—†λŠ” μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯

package xyz.itwill10.controller;

@Controller
public class ModelController {
	@RequestMapping("/display1")
	public String displayOne(Model model) {
		model.addAttribute("name", "홍길동");
		//model.addAttribute("now", new SimpleDateFormat("yyyyλ…„ MMμ›” dd일 HHμ‹œ mmλΆ„ ss초").format(new Date()));
		return "model_display";
	}
	
	@RequestMapping("/display2")
	public String displayTwo(Model model) {
		model.addAttribute("name", "μž„κΊ½μ •");
		//model.addAttribute("now", new SimpleDateFormat("yyyyλ…„ MMμ›” dd일 HHμ‹œ mmλΆ„ ss초").format(new Date()));
		return "model_display";
	}
	
	@RequestMapping("/display3")
	public String displayThree(Model model) {
		model.addAttribute("name", "μ „μš°μΉ˜");
		//model.addAttribute("now", new SimpleDateFormat("yyyyλ…„ MMμ›” dd일 HHμ‹œ mmλΆ„ ss초").format(new Date()));
		return "model_display";
	}
	
	//μ‹œμŠ€ν…œμ˜ ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ
	// => μš”μ²­ 처리 클래슀의 λ©”μ†Œλ“œμ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš©
	//@ModelAttribute : λ©”μ†Œλ“œμ— μ˜ν•΄ λ°˜ν™˜λ˜λŠ” 객체(κ°’)을 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ ν˜„μž¬ ν΄λž˜μŠ€μ—
	//μž‘μ„±λœ λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°(JSP λ¬Έμ„œ)μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	// => μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 응닡될 λͺ¨λ“  λ·°μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” 객체(κ°’)을 μ œκ³΅ν•  λͺ©μ μœΌλ‘œ μ‚¬μš©
	//value 속성 : λ·°μ—μ„œ 속성값을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ 이름(속성λͺ…)을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => λ‹€λ₯Έ 속성이 μ—†λŠ” μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
	@ModelAttribute("now")
	public String getNow() {
		return new SimpleDateFormat("yyyyλ…„ MMμ›” dd일 HHμ‹œ mmλΆ„ ss초").format(new Date());
	}
}





🐣 응닡 (now)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>처리결과 좜λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<h2>이름 = ${name }</h2>
	<h2>ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„ = ${now }</h2>
</body>
</html>





πŸ“™ Model (μš”μ²­λ°©μ‹)

  • method 속성 : RequestMethod μžλ£Œν˜•(Enum)의 μƒμˆ˜(Constant) 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •

    • RequestMethod μžλ£Œν˜•(Enum)은 ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ„ μƒμˆ˜λ‘œ 제곡
    • method μ†μ„±μœΌλ‘œ ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•˜κΈ° μœ„ν•œ μš”μ²­λ°©μ‹μ„ 지정 κ°€λŠ₯
    • method μ†μ„±κ°’μœΌλ‘œ μ„€μ •λœ μš”μ²­λ°©μ‹μ™Έμ˜ λ°©λ²•μœΌλ‘œ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•œ 경우 405 μ—λŸ¬μ½”λ“œ λ°œμƒ
  • ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό [GET] λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ κ²½μš°μ—λ§Œ μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜

    • @RequestMapping(value = "/method", method = RequestMethod.GET)
    • [GET] λ°©μ‹μœΌλ‘œ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜ λŒ€μ‹  @GetMapping μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯
  • ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό [POST] λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ κ²½μš°μ—λ§Œ μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜

    • @RequestMapping(value = "/method", method = RequestMethod.POST)
    • μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” μš”μ²­ URL μ£Όμ†Œκ°€ 같아도 μš”μ²­λ°©μ‹μ„ λ‹€λ₯΄κ²Œ μ§€μ •ν•˜λ©΄ λ‹€λ₯Έ μš”μ²­ 처리 λ©”μ†Œλ“œλ‘œ 맀핑 처리
    • [POST] λ°©μ‹μœΌλ‘œ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜ λŒ€μ‹  @PostMapping μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯


package xyz.itwill10.controller;

@Controller
public class MethodController {
	//μ‚¬μš©μžμ—κ²Œ 값을 μž…λ ₯λ°›κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜ ViewNameλ₯Ό λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ— 상관없이 무쑰건 μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜
	@RequestMapping("/method_input")
	public String inputOne() {
		return "method_input1";
	}
	
	//μž…λ ₯값을 λ°˜ν™˜λ°›μ•„ Request Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  속성값을 좜λ ₯ν•˜κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜
	//ViewNameλ₯Ό λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ  
	// => ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ— 상관없이 무쑰건 μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜
	@RequestMapping("/method_output")
	public String outputOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//ν΄λΌμ΄μ–ΈνŠΈκ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ₯Ό GET λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ 경우 - 비정상적인 μš”μ²­
		if(request.getMethod().equals("GET")) {
			//response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);//ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 405 μ—λŸ¬μ½”λ“œ 전달
			response.sendRedirect("method_input");//ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ URL μ£Όμ†Œ 전달 -  URL μ£Όμ†Œλ‘œ μž¬μš”μ²­
			return null;
		}
		
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		request.setAttribute("name", name);
		return "method_output";
	}
	
	//μ‚¬μš©μžμ—κ²Œ 값을 μž…λ ₯λ°›κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜ ViewNameλ₯Ό λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό [GET] λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ κ²½μš°μ—λ§Œ μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜
	//method 속성 : RequestMethod μžλ£Œν˜•(Enum)의 μƒμˆ˜(Constant) 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => RequestMethod μžλ£Œν˜•(Enum)은 ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­λ°©μ‹μ„ μƒμˆ˜λ‘œ 제곡
	// => method μ†μ„±μœΌλ‘œ ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•˜κΈ° μœ„ν•œ μš”μ²­λ°©μ‹μ„ 지정 κ°€λŠ₯
	// => method μ†μ„±κ°’μœΌλ‘œ μ„€μ •λœ μš”μ²­λ°©μ‹μ™Έμ˜ λ°©λ²•μœΌλ‘œ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•œ 경우 405 μ—λŸ¬μ½”λ“œ λ°œμƒ
	//[GET] λ°©μ‹μœΌλ‘œ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜ λŒ€μ‹  
	//@GetMapping μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯
	@RequestMapping(value = "/method", method = RequestMethod.GET)
	public String inputTwo() {
		return "method_input2";
	}

	//μž…λ ₯값을 λ°˜ν™˜λ°›μ•„ Request Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  속성값을 좜λ ₯ν•˜κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜
	//ViewNameλ₯Ό λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ  
	// => ν΄λΌμ΄μ–ΈνŠΈκ°€ νŽ˜μ΄μ§€λ₯Ό [POST] λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ κ²½μš°μ—λ§Œ μš”μ²­ 처리 λ©”μ†Œλ“œ 호좜
	//μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” μš”μ²­ URL μ£Όμ†Œκ°€ 같아도 μš”μ²­λ°©μ‹μ„ λ‹€λ₯΄κ²Œ μ§€μ •ν•˜λ©΄ λ‹€λ₯Έ 
	//μš”μ²­ 처리 λ©”μ†Œλ“œλ‘œ 맀핑 처리
	//[POST] λ°©μ‹μœΌλ‘œ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ @RequestMapping μ–΄λ…Έν…Œμ΄μ…˜ λŒ€μ‹  
	//@PostMapping μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© κ°€λŠ₯	
	@RequestMapping(value = "/method", method = RequestMethod.POST)
	public String outputTwo(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		request.setAttribute("name", name);
		return "method_output";
	}
}





🐣 응닡 (input1)


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>μž…λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<form action="method_output" method="post">
		이름 : <input type="text" name="name">
		<button type="submit">전솑</button>
	</form>
</body>
</html>





🐣 응닡 (input2)

  • μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” μš”μ²­ URL μ£Όμ†Œκ°€ 같아도 μš”μ²­λ°©μ‹(GET or POST)에 따라 λ‹€λ₯Έ μš”μ²­ 처리 λ©”μ†Œλ“œ μ‹€ν–‰


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>μž…λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<form action="method" method="post">
		이름 : <input type="text" name="name">
		<button type="submit">전솑</button>
	</form>
</body>
</html>





🐣 응닡 (output)

  • ν΄λΌμ΄μ–ΈνŠΈκ°€ ν•΄λ‹Ή νŽ˜μ΄μ§€λ₯Ό GET λ°©μ‹μœΌλ‘œ μš”μ²­ν•œ 경우 (비정상적인 μš”μ²­) ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 405 μ—λŸ¬μ½”λ“œ 전달 or URL μ£Όμ†Œ 전달 κ°€λŠ₯
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>좜λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<h2>${name }λ‹˜, ν™˜μ˜ν•©λ‹ˆλ‹€.</h2>
</body>
</html>





πŸ“™ Model (param)

  • μš”μ²­ 처리 λ©”μ†Œλ“œμ— HttpServletRequest μΈν„°νŽ˜μ΄μŠ€λ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ Front Controllerμ—κ²Œ ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ •λ³΄κ°€ μ €μž₯된 HttpServletRequest 객체λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
    • HttpServletRequest 객체의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ νŽ˜μ΄μ§€ μš”μ²­μ‹œ μ „λ‹¬λœ 값을 λ°˜ν™˜λ°›μ•„ 처리
    • public String result(HttpServletRequest request){}
  • μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νŽ˜μ΄μ§€ μš”μ²­μ‹œ μ „λ‹¬λ˜λŠ” κ°’μ˜ μ΄λ¦„μœΌλ‘œ μ›μ‹œν˜• λ˜λŠ” String 클래슀의 λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ Front Controller에 μ˜ν•΄ 전달값이 λ§€κ°œλ³€μˆ˜μ— μžλ™μœΌλ‘œ μ €μž₯λ˜μ–΄ 제곡
    • μ „λ‹¬κ°’μ˜ 이름과 λ§€κ°œλ³€μˆ˜μ˜ 이름이 같지 μ•Šμ„ 경우 String λ§€κ°œλ³€μˆ˜μ—λŠ” [null] μ €μž₯
    • μ›μ‹œν˜•μ˜ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λœ 값이 μ—†κ±°λ‚˜ μžλ£Œν˜•μ΄ λ§žμ§€ μ•Šμ„ 경우 400 μ—λŸ¬μ½”λ“œ λ°œμƒ
    • public String result(String food, Model model) {}
  • ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ μ›Ήν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜κΈ° μ „κ³Ό 후에 μ›ν•˜λŠ” λͺ…령을 μ‹€ν–‰ν•  수 μžˆλŠ” ν•„ν„°λ₯Ό [web.xml] νŒŒμΌμ— λ“±λ‘ν•˜μ—¬ μ‚¬μš© κ°€λŠ₯
    • ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­μ— λŒ€ν•΄ μš”μ²­μ •λ³΄κ°€ μ €μž₯된 λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ˜ λ¬Έμžν˜•μ‹μ„ λ³€κ²½ν•˜λŠ” ν•„ν„°κ°€ μ‚¬μš©λ˜λ„λ‘ [web.xml] νŒŒμΌμ„ μ΄μš©ν•˜μ—¬ μ„€μ • (인코딩 ν•„ν„° 적용)
    • ν•„ν„°λŠ” Front Controller 전에 μœ„μΉ˜λ˜μ–΄ μš”μ²­κ³Ό 응닡 κ΄€λ ¨ 정보 λ³€κ²½ κ°€λŠ₯ (WAS에 μ˜ν•΄ μ œμ–΄)
  • 전달값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ§€κ°œλ³€μˆ˜μ— @RequestParam μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš© (ꢌμž₯)
    • @RequestParam : 전달값을 λ§€κ°œλ³€μˆ˜μ— μ €μž₯ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • λ§€κ°œλ³€μˆ˜μ˜ 이름과 같은 μ΄λ¦„μ˜ 전달값이 μ—†λŠ” 경우 400 μ—λŸ¬ λ°œμƒ
    • λ§€κ°œλ³€μˆ˜μ— λ°˜λ“œμ‹œ 전달값이 μ €μž₯λ˜λ„λ‘ μ„€μ •ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • public String result(@RequestParam String food, Model model){}
  • value 속성 : μ „λ‹¬κ°’μ˜ 이름을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •

    • @RequestParam 속성을 μ‚¬μš©ν•˜μ—¬ μ „λ‹¬κ°’μ˜ μ΄λ¦„μœΌλ‘œ 값을 μ œκ³΅λ°›μ•„ λ§€κ°œλ³€μˆ˜μ— μ €μž₯ κ°€λŠ₯
    • μ „λ‹¬κ°’μ˜ 이름과 λ§€κ°œλ³€μˆ˜μ˜ 이름이 λ‹€λ₯Έ 경우 μ‚¬μš©
    • λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
    • public String result(@RequestParam(value = "foodname") String food, Model model) {}
  • required 속성 : false(λ―Έν•„μˆ˜) λ˜λŠ” true(ν•„μˆ˜ - κΈ°λ³Έ) 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •

    • 전달값이 λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λ˜μ–΄ μ €μž₯λ˜λŠ” ν•„μˆ˜ μ—¬λΆ€λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ 속성
  • defaultValue 속성 : 전달값과 λ§€κ°œλ³€μˆ˜μ΄ λ‹€λ₯Έ 경우 μ‚¬μš©λ  기본값을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •

    • 전달값이 μ—†λŠ” κ²½μš°μ—λ„ λ§€κ°œλ³€μˆ˜μ— null λŒ€μ‹  μ €μž₯될 κΈ°λ³Έκ°’μœΌλ‘œ μ‚¬μš©
    • public String result(@RequestParam(defaultValue = "된μž₯찌개") String food, Model model) {}


package xyz.itwill10.controller;

@Controller
public class ParamController {
	@RequestMapping(value = "/param", method = RequestMethod.GET)
	public String form() {
		return "param_form";
	}
	
	/*
	//μš”μ²­ 처리 λ©”μ†Œλ“œμ— HttpServletRequest μΈν„°νŽ˜μ΄μŠ€λ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ Front Controllerμ—κ²Œ
	//ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ •λ³΄κ°€ μ €μž₯된 HttpServletRequest 객체λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
	// => HttpServletRequest 객체의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ νŽ˜μ΄μ§€ μš”μ²­μ‹œ μ „λ‹¬λœ 값을 λ°˜ν™˜λ°›μ•„ 처리
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String result(HttpServletRequest request) throws UnsupportedEncodingException {
		String food=request.getParameter("food");
		request.setAttribute("food", food);
		return "param_display";
	}
	*/
	
	/*
	//μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νŽ˜μ΄μ§€ μš”μ²­μ‹œ μ „λ‹¬λ˜λŠ” κ°’μ˜ 이름과 같은 μ΄λ¦„μœΌλ‘œ String μžλ£Œν˜•μ˜
	//λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ Front Controller에 μ˜ν•΄ 전달값이 λ§€κ°œλ³€μˆ˜μ— μžλ™μœΌλ‘œ μ €μž₯λ˜μ–΄ 제곡
	// => μ „λ‹¬κ°’μ˜ 이름과 λ§€κ°œλ³€μˆ˜μ˜ 이름이 같지 μ•Šμ„ 경우 λ§€κ°œλ³€μˆ˜μ—λŠ” [null] μ €μž₯
	//ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ μ›Ήν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜κΈ° μ „κ³Ό 후에 μ›ν•˜λŠ” λͺ…령을 μ‹€ν–‰ν•  수 μžˆλŠ”
	//ν•„ν„°λ₯Ό [web.xml] νŒŒμΌμ— λ“±λ‘ν•˜μ—¬ μ‚¬μš© κ°€λŠ₯
	// => ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­μ— λŒ€ν•΄ μš”μ²­μ •λ³΄κ°€ μ €μž₯된 λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ˜ λ¬Έμžν˜•μ‹μ„
	//λ³€κ²½ν•˜λŠ” ν•„ν„°κ°€ μ‚¬μš©λ˜λ„λ‘ [web.xml] νŒŒμΌμ„ μ΄μš©ν•˜μ—¬ μ„€μ • - 인코딩 ν•„ν„° 적용
	// => ν•„ν„°λŠ” Front Controller 전에 μœ„μΉ˜λ˜μ–΄ μš”μ²­κ³Ό 응닡 κ΄€λ ¨ 정보 λ³€κ²½ κ°€λŠ₯ - WAS에 μ˜ν•΄ μ œμ–΄ 
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String result(String food, Model model) {
		model.addAttribute("food", food);
		return "param_display";
	}
	*/
	
	/*
	//전달값을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ§€κ°œλ³€μˆ˜μ— @RequestParam μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš©
	//@RequestParam : 전달값을 λ§€κ°œλ³€μˆ˜μ— μ €μž₯ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	// => λ§€κ°œλ³€μˆ˜μ˜ 이름과 같은 μ΄λ¦„μ˜ 전달값이 μ—†λŠ” 경우 400 μ—λŸ¬ λ°œμƒ
	// => λ§€κ°œλ³€μˆ˜μ— λ°˜λ“œμ‹œ 전달값이 μ €μž₯λ˜λ„λ‘ μ„€μ •ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String result(@RequestParam String food, Model model) {
		model.addAttribute("food", food);
		return "param_display";
	}
	*/
	
	/*
	//value 속성 : μ „λ‹¬κ°’μ˜ 이름을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => @RequestParam 속성을 μ‚¬μš©ν•˜μ—¬ μ „λ‹¬κ°’μ˜ μ΄λ¦„μœΌλ‘œ 값을 μ œκ³΅λ°›μ•„ λ§€κ°œλ³€μˆ˜μ— μ €μž₯ κ°€λŠ₯
	// => μ „λ‹¬κ°’μ˜ 이름과 λ§€κ°œλ³€μˆ˜μ˜ 이름이 λ‹€λ₯Έ 경우 μ‚¬μš©
	// => λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
	//required 속성 : false(λ―Έν•„μˆ˜) λ˜λŠ” true(ν•„μˆ˜ - κΈ°λ³Έ) 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => 전달값이 λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λ˜μ–΄ μ €μž₯νšŒλŠ” ν•„μˆ˜ μ—¬λΆ€λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ 속성 
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String result(@RequestParam(value = "foodname", required = true) String food, Model model) {
		model.addAttribute("food", food);
		return "param_display";
	}
	*/
	
	//defaultValue 속성 : 전달값과 λ§€κ°œλ³€μˆ˜μ΄ λ‹€λ₯Έ 경우 μ‚¬μš©λ  기본값을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => 전달값이 μ—†λŠ” κ²½μš°μ—λ„ λ§€κ°œλ³€μˆ˜μ— null λŒ€μ‹  μ €μž₯될 κΈ°λ³Έκ°’μœΌλ‘œ μ‚¬μš©
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String result(@RequestParam(value = "foodname", defaultValue = "된μž₯찌개") String food, Model model) {
		model.addAttribute("food", food);
		return "param_display";
	}
}





πŸ“Œ web.xml (filter)

  • filter : ν•„ν„° 클래슀λ₯Ό ν•„ν„°(Filter)둜 λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ

    • ν•„ν„°(Filter) : μ›Ήν”„λ‘œκ·Έλž¨ μ‹€ν–‰ μ „κ³Ό 후에 싀행될 λͺ…령을 제곡
    • filter-name : ν•„ν„°λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ
    • filter-class : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 객체둜 μƒμ„±λ˜μ–΄ 싀행될 ν•„ν„° 클래슀λ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ
    • CharacterEncodingFilter : λ¦¬ν€˜μŠ€νŠΈ 메세지 λͺΈμ²΄λΆ€μ˜ μ €μž₯된 값에 λŒ€ν•œ λ¬Έμžν˜•νƒœλ₯Ό λ³€κ²½ν•˜κΈ° μœ„ν•œ ν•„ν„° 클래슀
    • init-param μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ CharacterEncodingFilter ν΄λž˜μŠ€μ— μ˜ν•΄ λ³€κ²½ν•˜κ³ μž ν•˜λŠ” λ¬Έμžν˜•νƒœ(캐릭터셋)을 제곡
  • filter-mapping : ν•„ν„°κ°€ μ‹€ν–‰λ˜κΈ° μœ„ν•œ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μš”μ²­ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ

    • filter-name : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 싀행될 ν•„ν„°μ˜ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ
    • url-pattern : ν•„ν„°κ°€ μ‹€ν–‰λ˜κΈ° μœ„ν•œ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μš”μ²­ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- filter : ν•„ν„° 클래슀λ₯Ό ν•„ν„°(Filter)둜 λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- => ν•„ν„°(Filter) : μ›Ήν”„λ‘œκ·Έλž¨ μ‹€ν–‰ μ „κ³Ό 후에 싀행될 λͺ…령을 제곡 -->
	<filter>
		<!-- filter-name : ν•„ν„°λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<filter-name>encodingFilter</filter-name>
		<!-- filter-class : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 객체둜 μƒμ„±λ˜μ–΄ 싀행될 ν•„ν„° ν΄λž˜μŠ€μ„ μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<!-- CharacterEncodingFilter : λ¦¬ν€˜μŠ€νŠΈ 메세지 λͺΈμ²΄λΆ€μ˜ μ €μž₯된 값에 λŒ€ν•œ λ¬Έμžν˜•νƒœλ₯Ό λ³€κ²½ν•˜κΈ° μœ„ν•œ ν•„ν„° 클래슀 -->
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<!-- init-param μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ CharacterEncodingFilter ν΄λž˜μŠ€μ— μ˜ν•΄ λ³€κ²½ν•˜κ³ μž
		ν•˜λŠ” λ¬Έμžν˜•νƒœ(캐릭터셋)을 제곡  -->
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	
	<!-- filter-mapping : ν•„ν„°κ°€ μ‹€ν–‰λ˜κΈ° μœ„ν•œ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μš”μ²­ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ λ“±λ‘ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
	<filter-mapping>
		<!-- filter-name : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ 싀행될 ν•„ν„°μ˜ μ‹λ³„μžλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<filter-name>encodingFilter</filter-name>
		<!-- url-pattern : ν•„ν„°κ°€ μ‹€ν–‰λ˜κΈ° μœ„ν•œ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μš”μ²­ URL μ£Όμ†Œμ˜ νŒ¨ν„΄μ„ μ„€μ •ν•˜κΈ° μœ„ν•œ μ—˜λ¦¬λ¨ΌνŠΈ -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>






🐣 응닡 (input)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>μž…λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<form action="param" method="post">
		<!-- λ‚΄κ°€ μ’‹μ•„ν•˜λŠ” μŒμ‹ : <input type="text" name="food"> -->
		λ‚΄κ°€ μ’‹μ•„ν•˜λŠ” μŒμ‹ : <input type="text" name="foodname">
		<button type="submit">μž…λ ₯μ™„λ£Œ</button>
	</form>
</body>
</html>





🐣 응닡 (output)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>좜λ ₯νŽ˜μ΄μ§€</h1>
	<hr>
	<h2>였늘의 점심은 [${food }]둜 κ²°μ • ν•˜κ² μŠ΅λ‹ˆλ‹€.</h2>
</body>
</html>





πŸ“™ @ModelAttribute (param)

  • @RequestParam μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 전달값과 같은 μ΄λ¦„μ˜ λ§€κ°œλ³€μˆ˜μ— 전달값을 μ œκ³΅λ°›μ•„ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©
    • @RequestParam μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ™€ 같은 μ΄λ¦„μœΌλ‘œ μ „λ‹¬λ˜λŠ” 값이 μ—†λŠ” 경우 400 μ—λŸ¬μ½”λ“œ λ°œμƒ
    • public String join(@RequestParam String id, @RequestParam String passwd){}
  • @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 전달값과 같은 μ΄λ¦„μ˜ λ§€κ°œλ³€μˆ˜μ— 전달값을 μ œκ³΅λ°›μ•„ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©
    • @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ €μž₯된 값을 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°(View) 제곡
    • @ModelAttribute : 객체(κ°’)을 λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • λ©”μ†Œλ“œμ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λ©”μ†Œλ“œμ˜ λ°˜ν™˜κ°’μ„ μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ μš”μ²­ 처리 클래슀의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œ 제곡
    • λ§€κ°œλ³€μˆ˜μ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λ§€κ°œλ³€μˆ˜μ— μ €μž₯된 값을 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œ 제곡
    • value 속성 : λ·°(JSP)μ—μ„œ 속성값을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ 속성λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
    • public String join(@ModelAttribute(value = "id") String id, @ModelAttribute("passwd") String passwd){}
  • DTO 클래슀둜 객체λ₯Ό μƒμ„±ν•˜μ—¬ 전달값을 λ§€κ°œλ³€μˆ˜λ‘œ μ œκ³΅λ°›μ•„ 객체의 ν•„λ“œκ°’μœΌλ‘œ μ €μž₯
    • Member member=new Member(); member.setId(id); model.addAttribute("member", member);
  • Model.addAttribute(Object attributeValue) : Model 객체에 속성λͺ…κ³Ό 속성값(객체)을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ (Request Scope)
    • μ†μ„±κ°’μ˜ μžλ£Œν˜•(ν΄λž˜μŠ€μ΄λ¦„)을 속성λͺ…μœΌλ‘œ μžλ™ μ„€μ • (μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜)
    • μ†μ„±κ°’μœΌλ‘œ μ›μ‹œκ°’(Wrapper 객체) λ˜λŠ” String 객체 μ €μž₯ λΆˆκ°€
    • model.addAttribute(member);
  • μš”μ²­ νŽ˜μ΄μ§€μ˜ 전달값이 μžˆλŠ” 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜ μžλ£Œν˜•μ„ VO 클래슀(DTO 클래슀)둜 μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” VO 클래슀둜 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬ν•˜μ—¬ μ €μž₯
    • VO 클래슀의 κ°μ²΄λŠ” μ „λ‹¬κ°’μ˜ 이름과 같은 ν•„λ“œμ— 전달값을 μ œκ³΅λ°›μ•„ μ €μž₯
  • β˜… Command 객체 : 전달값을 μ œκ³΅λ°›μ•„ ν•„λ“œμ— μ €μž₯된 객체둜 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—μ„œ μ‚¬μš©λ  수 μžˆλ„λ‘ 제곡
    • @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 객체λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•΄ λ·°μ—κ²Œ 제곡 (μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜)
    • @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜ μƒλž΅ κ°€λŠ₯
    • value 속성을 μƒλž΅ν•˜λ©΄ Command 객체의 μžλ£Œν˜•(클래슀)이 속성λͺ…μœΌλ‘œ μžλ™ μ„€μ •
    • public String join(@ModelAttribute Member member) {}
    • @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ˜ value 속성을 μ‚¬μš©ν•˜μ—¬ λ·°μ—κ²Œ 제곡될 μ†μ„±κ°’μ˜ 속성λͺ…을 λ³€κ²½ κ°€λŠ₯
  • μš”μ²­ νŽ˜μ΄μ§€μ˜ 전달값이 μžˆλŠ” 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜ μžλ£Œν˜•μ„ Map μΈν„°νŽ˜μ΄μŠ€λ‘œ μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” Map 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬ν•˜μ—¬ μ €μž₯
    • Map 객체의 μ—”νŠΈλ¦¬μ—λŠ” μ „λ‹¬κ°’μ˜ 이름을 맡킀(MapKey)둜 μ œκ³΅λ°›κ³  전달값을 맡값(MapValue)둜 μ œκ³΅λ°›μ•„ μ €μž₯
    • Map μΈν„°νŽ˜μ΄μŠ€λ‘œ μž‘μ„±λœ λ§€κ°œλ³€μˆ˜μ—λŠ” λ°˜λ“œμ‹œ @RequestParam μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•΄μ•Όλ§Œ Map 객체의 λ§΅κ°’μœΌλ‘œ 전달값을 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
    • public String join(@RequestParam Map<String, Object> map, Model model) {}


package xyz.itwill10.controller;

@Controller
public class JoinController {
	@RequestMapping(value = "join", method=RequestMethod.GET)
	public String join() {
		return "join_form";
	}
	
	/*
	//전달값을 μ œκ³΅λ°›μ•„ Request Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  속성값을 좜λ ₯ν•˜κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜ 
	//뷰이름을 λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => 전달값과 같은 μ΄λ¦„μ˜ λ§€κ°œλ³€μˆ˜μ— 전달값을 μ œκ³΅λ°›μ•„ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©
	// => @RequestParam μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ™€ 같은 μ΄λ¦„μœΌλ‘œ μ „λ‹¬λ˜λŠ” 값이 μ—†λŠ” 경우 400 μ—λŸ¬μ½”λ“œ λ°œμƒ
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@RequestParam String id, @RequestParam String passwd
			,@RequestParam String name,@RequestParam String email, Model model) {
		model.addAttribute("id", id);
		model.addAttribute("passwd", passwd);
		model.addAttribute("name", name);
		model.addAttribute("email", email);
		return "join_display";
	}
	*/
	
	/*
	//전달값을 μ œκ³΅λ°›μ•„ Request Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  속성값을 좜λ ₯ν•˜κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜ 
	//뷰이름을 λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => 전달값과 같은 μ΄λ¦„μ˜ λ§€κ°œλ³€μˆ˜μ— 전달값을 μ œκ³΅λ°›μ•„ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©
	// => @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ €μž₯된 값을 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°(View) 제곡
	//@ModelAttribute : 객체(κ°’)을 λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	// => λ©”μ†Œλ“œμ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λ©”μ†Œλ“œμ˜ λ°˜ν™˜κ°’μ„ μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬
	//μš”μ²­ 처리 클래슀의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œ 제곡
	// => λ§€κ°œλ³€μˆ˜μ— @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λ§€κ°œλ³€μˆ˜μ— μ €μž₯된 값을 μ†μ„±κ°’μœΌλ‘œ 
	//μ €μž₯ν•˜μ—¬ μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œ 제곡
	//value 속성 : λ·°(JSP)μ—μ„œ 속성값을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ 속성λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
	//@ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λ§€κ°œλ³€μˆ˜μ™€ 같은 μ΄λ¦„μ˜ 전달값이 없어도 400 μ—λŸ¬μ½”λ“œ λ―Έλ°œμƒ
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@ModelAttribute("id") String id, @ModelAttribute("passwd") String passwd
			,@ModelAttribute("name") String name,@ModelAttribute("email") String email) {
		return "join_display";
	}
	*/
	
	/*
	//전달값을 λ§€κ°œλ³€μˆ˜λ‘œ μ œκ³΅λ°›μ•„ 객체의 ν•„λ“œκ°’μœΌλ‘œ μ €μž₯ν•˜κ³  객체λ₯Ό Request Scope μ†μ„±κ°’μœΌλ‘œ
	//μ €μž₯ν•˜κ³  속성값을 좜λ ₯ν•˜κΈ° μœ„ν•œ JSP λ¬Έμ„œμ˜ 뷰이름을 λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@RequestParam String id, @RequestParam String passwd
			,@RequestParam String name,@RequestParam String email, Model model) {
		Member member=new Member();
		member.setId(id);
		member.setPasswd(passwd);
		member.setName(name);
		member.setEmail(email);
		
		//model.addAttribute("member", member);
		//Model.addAttribute(Object attributeValue) : Model 객체에 속성λͺ…κ³Ό 속성값(객체)을  
		//μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ - Request Scope
		// => μ†μ„±κ°’μ˜ μžλ£Œν˜•(클래슀)을 속성λͺ…μœΌλ‘œ μžλ™ μ„€μ • - μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜
		// => μ†μ„±κ°’μœΌλ‘œ μ›μ‹œκ°’(Wrapper 객체) λ˜λŠ” String 객체 μ €μž₯ λΆˆκ°€
		model.addAttribute(member);
		
		return "join_display";
	}
	*/

	/*
	//μš”μ²­ νŽ˜μ΄μ§€μ˜ 전달값이 μžˆλŠ” 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜ μžλ£Œν˜•μ„ VO 클래슀(DTO 클래슀)둜
	//μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” VO 클래슀둜 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬ν•˜μ—¬ μ €μž₯
	// => VO 클래슀의 κ°μ²΄λŠ” μ „λ‹¬κ°’μ˜ 이름과 같은 ν•„λ“œμ— 전달값을 μ œκ³΅λ°›μ•„ μ €μž₯
	//Command 객체 : 전달값을 μ œκ³΅λ°›μ•„ ν•„λ“œμ— μ €μž₯된 객체둜 μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ μš”μ²­ 처리 
	//λ©”μ†Œλ“œμ˜ λ·°μ—μ„œ μ‚¬μš©λ  수 μžˆλ„λ‘ 제곡
	// => @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 객체λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•΄ λ·°μ—κ²Œ 제곡 - μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜
	// => @ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜ μƒλž΅ κ°€λŠ₯
	// => value 속성을 μƒλž΅ν•˜λ©΄ Command 객체의 μžλ£Œν˜•(클래슀)이 속성λͺ…μœΌλ‘œ μžλ™ μ„€μ •
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@ModelAttribute Member member) {
		return "join_display";
	}
	*/
	
	/*
	//@ModelAttribute μ–΄λ…Έν…Œμ΄μ…˜μ˜ value 속성을 μ‚¬μš©ν•˜μ—¬ λ·°μ—κ²Œ 제곡될 μ†μ„±κ°’μ˜ 속성λͺ…을 λ³€κ²½ κ°€λŠ₯
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@ModelAttribute(value = "mem") Member member, Model model) {
		if(member.getId().equals("abc123")) {//아이디가 μ€‘λ³΅λœ 경우
			model.addAttribute("message", "이미 μ‚¬μš©μ€‘μΈ 아이디λ₯Ό μž…λ ₯ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
			return "join_form";//μž…λ ₯νŽ˜μ΄μ§€ 이동
		}
		return "join_display";
	}
	*/

	//μš”μ²­ νŽ˜μ΄μ§€μ˜ 전달값이 μžˆλŠ” 경우 μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜ μžλ£Œν˜•μ„ Map μΈν„°νŽ˜μ΄μŠ€λ‘œ
	//μž‘μ„±ν•˜λ©΄ Front ControllerλŠ” Map 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬ν•˜μ—¬ μ €μž₯
	// => Map κ°μ²΄λŠ” μ „λ‹¬κ°’μ˜ 이름을 맡킀(MapKey - String)둜 μ„€μ •ν•˜μ—¬ 전달값을 맡값
	//(MapValue - String)둜 μ œκ³΅λ°›μ•„ μ—”νŠΈλ¦¬λ‘œ μΆ”κ°€
	// => Map μΈν„°νŽ˜μ΄μŠ€λ‘œ μž‘μ„±λœ λ§€κ°œλ³€μˆ˜μ—λŠ” λ°˜λ“œμ‹œ @RequestParam μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•΄μ•Όλ§Œ
	//Map 객체의 λ§΅κ°’μœΌλ‘œ 전달값을 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
	@RequestMapping(value = "join", method=RequestMethod.POST)
	public String join(@RequestParam Map<String, String> map, Model model) {
		model.addAttribute("mem", map);
		return "join_display";
	}
}





🌸 VO class (Member)

  • νšŒμ›μ •λ³΄λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ 클래슀 (VO 클래슀) : 값을 μ €μž₯ν•  λͺ©μ μ˜ 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 클래슀
    • DAO 클래슀의 λ©”μ†Œλ“œμ—μ„œ μ‚¬μš©λ  경우 DTO 클래슀의 κΈ°λŠ₯ 제곡
    • 전달값을 μ œκ³΅λ°›μ•„ μ €μž₯ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ°˜λ“œμ‹œ μ „λ‹¬κ°’μ˜ 이름과 ν•„λ“œμ˜ 이름이 같도둝 μž‘μ„±

package xyz.itwill10.dto;

import lombok.Data;

@Data
public class Member {
	private String id;
	private String passwd;
	private String name;
	private String email;
}





🐣 응닡 (input)


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>νšŒμ›κ°€μž…</h1>
	<hr>
	<form action="join" method="post">
	<table>
		<tr>
			<td>아이디</td>
			<td><input type="text" name="id" value="${mem.id }"></td>
		</tr>
		<tr>
			<td>λΉ„λ°€λ²ˆν˜Έ</td>
			<td><input type="password" name="passwd" value="${mem.passwd }"></td>
		</tr>
		<tr>
			<td>이름</td>
			<td><input type="text" name="name" value="${mem.name }"></td>
		</tr>
		<tr>
			<td>이메일</td>
			<td><input type="text" name="email" value="${mem.email }"></td>
		</tr>
		<tr>
			<td colspan="2"><button type="submit">νšŒμ›κ°€μž…</button></td>
		</tr>
	</table>
	</form>
	<p style="color: red;">${message }</p>
</body>
</html>





🐣 응닡 (output)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>νšŒμ›κ°€μž…ν™•μΈ</h1>
	<hr>
	<%-- 
	<ul>
		<li>아이디 = ${id }</li>
		<li>λΉ„λ°€λ²ˆν˜Έ = ${passwd }</li>
		<li>이름 = ${name }</li>
		<li>이메일 = ${email }</li>
	</ul>
	--%>
	
	<%-- 
	<ul>
		<li>아이디 = ${member.id }</li>
		<li>λΉ„λ°€λ²ˆν˜Έ = ${member.passwd }</li>
		<li>이름 = ${member.name }</li>
		<li>이메일 = ${member.email }</li>
	</ul>
	--%>
	
	<ul>
		<li>아이디 = ${mem.id }</li>
		<li>λΉ„λ°€λ²ˆν˜Έ = ${mem.passwd }</li>
		<li>이름 = ${mem.name }</li>
		<li>이메일 = ${mem.email }</li>
	</ul>
</body>
</html>





πŸ“™ Model (session)

  • 전달값을 μ œκ³΅λ°›μ•„ 인증 처리 ν›„ κΆŒν•œ κ΄€λ ¨ 정보λ₯Ό Session Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³  둜그인 성곡 메세지λ₯Ό 좜λ ₯ν•˜λŠ” JSP λ¬Έμ„œμ˜ 뷰이름을 λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ

    • 전달값을 String λ§€κ°œλ³€μˆ˜μ— ν•˜λ‚˜μ”© μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
    • Session Scope 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•΄ λ§€κ°œλ³€μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ HttpSession 객체λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš©
    • public String login(@RequestParam String id, HttpSession session, Model model) {}
    • μ—λŸ¬ κ΄€λ ¨ 정보λ₯Ό Request Scope 속성값 μ €μž₯ (μž…λ ₯νŽ˜μ΄μ§€(JSP)μ—μ„œ 좜λ ₯ κ°€λŠ₯)
    • Request Scope : ν˜„μž¬ 싀행쀑인 μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ ν¬μ›Œλ“œ μ΄λ™λ˜λŠ” λ·°μ—μ„œλ§Œ 속성값 μ œκ³΅λ°›μ•„ μ‚¬μš©
    • model.addAttribute("message", "아이디 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έμ„ 잘λͺ» μž…λ ₯ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
    • 인증 성곡 (κΆŒν•œ κ΄€λ ¨ 정보λ₯Ό Session Scope 속성값 μ €μž₯)
    • Session Scope : λ™μΌν•œ μ„Έμ…˜μ„ μ‚¬μš©ν•˜λŠ” λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ 속성값을 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯ (같은 ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­)
    • session.setAttribute("loginId", id);
  • 전달값을 Command 객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš©

    • public String login(@ModelAttribute Member member, HttpSession session, Model model) {}


package xyz.itwill10.controller;

@Controller
public class LoginController {
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "login_form";
	}
	
	/*
	//전달값을 μ œκ³΅λ°›μ•„ 인증 처리 ν›„ κΆŒν•œ κ΄€λ ¨ 정보λ₯Ό Session Scope μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜κ³ 
	//둜그인 성곡 메세지λ₯Ό 좜λ ₯ν•˜λŠ” JSP λ¬Έμ„œμ˜ 뷰이름을 λ°˜ν™˜ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => 전달값을 String λ§€κ°œλ³€μˆ˜μ— ν•˜λ‚˜μ”© μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯ 
	// => Session Scope 속성값을 μ €μž₯ν•˜κΈ° μœ„ν•΄ λ§€κ°œλ³€μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ HttpSession 객체λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš©
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@RequestParam String id, @RequestParam String passwd
			, HttpSession session, Model model) {
		if(!id.equals("abc123") || !passwd.equals("123456")) {//인증 μ‹€νŒ¨
			//μ—λŸ¬ κ΄€λ ¨ 정보λ₯Ό Request Scope 속성값 μ €μž₯ - μž…λ ₯νŽ˜μ΄μ§€(JSP)μ—μ„œ 좜λ ₯ κ°€λŠ₯
			//Request Scope : ν˜„μž¬ 싀행쀑인 μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ ν¬μ›Œλ“œ μ΄λ™λ˜λŠ” λ·°μ—μ„œλ§Œ μ†μ„±κ°’λ§Œ μ œκ³΅λ°›μ•„ μ‚¬μš©
			model.addAttribute("message", "아이디 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έμ„ 잘λͺ» μž…λ ₯ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
			model.addAttribute("id",id);
			return "login_form";//μž…λ ₯νŽ˜μ΄μ§€ 이동
		}
		
		//인증 성곡 - κΆŒν•œ κ΄€λ ¨ 정보λ₯Ό Session Scope 속성값 μ €μž₯
		//Session Scope : λ™μΌν•œ μ„Έμ…˜μ„ μ‚¬μš©ν•˜λŠ” λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ 속성값을 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
		session.setAttribute("loginId", id);
		
		return "login_display";
	}
	*/
	
	//전달값을 Command 객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš©
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@ModelAttribute Member member, HttpSession session, Model model) {
		if(!member.getId().equals("abc123") || !member.getPasswd().equals("123456")) {//인증 μ‹€νŒ¨
			model.addAttribute("message", "아이디 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έμ„ 잘λͺ» μž…λ ₯ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.");
			return "login_form";//μž…λ ₯νŽ˜μ΄μ§€ 이동
		}
		session.setAttribute("loginId", member.getId());
		return "login_display";
	}
}





🐣 응닡 (input)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>둜그인</h1>
	<hr>
	<form action="login" method="post">
	<table>
		<tr>
			<td>아이디</td>
			<%-- <td><input type="text" name="id" value="${id }"></td> --%>
			<td><input type="text" name="id" value="${member.id }"></td>
		</tr>
		<tr>
			<td>λΉ„λ°€λ²ˆν˜Έ</td>
			<td><input type="password" name="passwd"></td>
		</tr>
		<tr>
			<td colspan="2"><button type="submit">둜그인</button></td>
		</tr>
	</table>
	</form>
	<p style="color: red;">${message }</p>
</body>
</html>





🐣 응닡 (output)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>둜그인 성곡</h1>
	<hr>
	<h2>${loginId }λ‹˜, ν™˜μ˜ν•©λ‹ˆλ‹€.</h2>
</body>
</html>





πŸŽ€ @SessionAttributes

  • @SessionAttributes : Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ μ €μž₯λ˜λŠ” 속성값을 ν˜„μž¬ μš”μ²­ 처리 클래슀(Controller클래슀)의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—κ²Œ μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜

    • Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ μ €μž₯λ˜λŠ” 속성값을 Request Scope 속성값이 μ•„λ‹Œ μ œν•œμ μΈ Session Scope μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
    • λ³€κ²½ μ²˜λ¦¬μ‹œ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ λ³€κ²½ν•  객체λ₯Ό κ²€μƒ‰ν•˜μ§€ μ•Šκ³  λ·°μ—μ„œλŠ” 값을 μ „λ‹¬ν•˜μ§€ μ•Šλ„λ‘ μ œν•œμ μΈ Session Scope 속성값 μ‚¬μš©
    • value 속성 : μ œν•œμ μΈ Session Scope μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜ν•˜κΈ° μœ„ν•œ 속성λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
  • 아이디λ₯Ό 전달받아 νšŒμ›μ •λ³΄λ₯Ό λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μš”μ²­ 처리 λ©”μ†Œλ“œ (μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 제곡된 νšŒμ›μ •λ³΄λ₯Ό μž…λ ₯νƒœκ·Έμ˜ μ΄ˆκΈ°κ°’μœΌλ‘œ μ‚¬μš©λ˜λ„λ‘ λ·° μž‘μ„±)
    • Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ κ²€μƒ‰λœ νšŒμ›μ •λ³΄λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°μ—κ²Œ 제곡
    • μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œλ§Œ 속성값을 μ œκ³΅ν•˜μ—¬ 좜λ ₯ 처리 (Request Scope)
    • μ†μ„±κ°’μœΌλ‘œ μ €μž₯된 객체의 μžλ£Œν˜•(클래슀의 이름 - μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜)이 μžλ™μœΌλ‘œ 속성λͺ…μœΌλ‘œ μ„€μ •
    • @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ Model 객체둜 μ €μž₯된 속성값은 μ œν•œμ μΈ Session Scope μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜λ˜μ–΄ ν˜„μž¬ μš”μ²­ 처리 클래슀의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—κ²Œ 속성값 제곡
    • model.addAttribute(hewon);//속성λͺ… μƒλž΅ κ°€λŠ₯
  • νšŒμ›μ •λ³΄λ₯Ό 전달받아 λ³€κ²½λœ νšŒμ›μ •λ³΄λ₯Ό λ·°μ—κ²Œ μ œκ³΅ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
    • μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜μ—λŠ” Command 객체둜 @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ 제곡된 νšŒμ›μ •λ³΄λ₯Ό 전달받아 μ €μž₯ν•˜κ³  λ³€κ²½κ°’μœΌλ‘œ ν•„λ“œκ°’λ§Œ λ³€κ²½ν•΄ μ‚¬μš©
    • μš”μ²­ 처리 λ©”μ†Œλ“œμ— SessionStatus μΈν„°νŽ˜μ΄μŠ€λ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ SessionStatus 객체λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
    • SessionStatus 객체 : @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ 제곡된 μ œν•œμ μΈ Session Scope μ†μ„±κ°’μ˜ μƒνƒœμ •λ³΄λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ 객체


package xyz.itwill10.controller;

@Controller
//@SessionAttributes : Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ μ €μž₯λ˜λŠ” 속성값을 ν˜„μž¬ μš”μ²­ 처리 클래슀(Controller
//클래슀)의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—κ²Œ μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
// => Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ μ €μž₯λ˜λŠ” 속성값을 Request Scope 속성값이 μ•„λ‹Œ μ œν•œμ μΈ Session Scope
//μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
// => λ³€κ²½ μ²˜λ¦¬μ‹œ ν˜ΈμΆœλ˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ λ³€κ²½ν•  객체λ₯Ό κ²€μƒ‰ν•˜μ§€ μ•Šκ³  λ·°μ—μ„œλŠ” 값을 
//μ „λ‹¬ν•˜μ§€ μ•Šλ„λ‘ μ œν•œμ μΈ Session Scope 속성값 μ‚¬μš©
//value 속성 : μ œν•œμ μΈ Session Scope μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜ν•˜κΈ° μœ„ν•œ 속성λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
// => λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
@SessionAttributes("hewon")
public class SessionController {
	//아이디λ₯Ό 전달받아 νšŒμ›μ •λ³΄λ₯Ό κ²€μƒ‰ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ - Service 클래슀의 λ©”μ†Œλ“œ
	private Hewon getHewon(String id) {
		Hewon hewon=new Hewon();
		hewon.setId(id);
		hewon.setName("홍길동");
		hewon.setEmail("abc@itwill.xyz");
		return hewon;
	}
	
	//아이디λ₯Ό 전달받아 νšŒμ›μ •λ³΄λ₯Ό λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 제곡된 νšŒμ›μ •λ³΄κ°€ 좜λ ₯λ˜λ„λ‘ λ·° μž‘μ„±
	@RequestMapping("/hewon_view")
	public String view(@RequestParam(defaultValue = "abc123") String id, Model model) {
		//Service 클래슀의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ 아이디λ₯Ό 전달해 ν•΄λ‹Ή μ•„μ΄λ””μ˜ νšŒμ›μ •λ³΄λ₯Ό 
		//κ²€μƒ‰ν•˜μ—¬ λ°˜ν™˜λ°›μ•„ μ €μž₯
		Hewon hewon=getHewon(id);
		
		//Model 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ κ²€μƒ‰λœ νšŒμ›μ •λ³΄λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ν•˜μ—¬ λ·°μ—κ²Œ 제곡 
		// => μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ·°μ—κ²Œλ§Œ 속성값을 μ œκ³΅ν•˜μ—¬ 좜λ ₯ 처리 - Request Scope
		//model.addAttribute("hewon", hewon);
		//μ†μ„±κ°’μœΌλ‘œ μ €μž₯된 객체의 μžλ£Œν˜•(클래슀의 이름 - μ²«λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ λ³€ν™˜)이 μžλ™μœΌλ‘œ 속성λͺ…μœΌλ‘œ μ„€μ •
		//@SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ Model 객체둜 μ €μž₯된 속성값은 μ œν•œμ μΈ Session Scope 
		//μ†μ„±κ°’μœΌλ‘œ λ³€ν™˜λ˜μ–΄ ν˜„μž¬ μš”μ²­ 처리 클래슀의 λͺ¨λ“  μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—κ²Œ 속성값 제곡
		model.addAttribute(hewon);//속성λͺ… μƒλž΅ κ°€λŠ₯  
		
		return "hewon_view";
	}
	
	/*
	//아이디λ₯Ό 전달받아 νšŒμ›μ •λ³΄λ₯Ό λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => μš”μ²­ 처리 λ©”μ†Œλ“œμ— μ˜ν•΄ 제곡된 νšŒμ›μ •λ³΄λ₯Ό μž…λ ₯νƒœκ·Έμ˜ μ΄ˆκΈ°κ°’μœΌλ‘œ μ‚¬μš©λ˜λ„λ‘ λ·° μž‘μ„±
	@RequestMapping(value="/hewon_update", method = RequestMethod.GET)
	public String update(@RequestParam(defaultValue = "abc123") String id, Model model) {
		Hewon hewon=getHewon(id);
		model.addAttribute(hewon);
		return "hewon_update";
	}
	*/
	
	//@SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ 제곡된 νšŒμ›μ •λ³΄λ₯Ό μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ μ‚¬μš© κ°€λŠ₯
	// => 아이디λ₯Ό 전달받아 νšŒμ›μ •λ³΄λ₯Ό κ²€μƒ‰ν•˜λŠ” λͺ…λ Ήκ³Ό λ·°μ—κ²Œ μ œκ³΅ν•˜κΈ° μœ„ν•œ 속성값 μ €μž₯ λΆˆν•„μš”
	@RequestMapping(value="/hewon_update", method = RequestMethod.GET)
	public String update() {
		return "hewon_update";
	}

	//νšŒμ›μ •λ³΄λ₯Ό 전달받아 λ³€κ²½λœ νšŒμ›μ •λ³΄λ₯Ό λ·°μ—κ²Œ μ œκ³΅ν•˜λŠ” μš”μ²­ 처리 λ©”μ†Œλ“œ
	// => μš”μ²­ 처리 λ©”μ†Œλ“œμ˜ λ§€κ°œλ³€μˆ˜μ—λŠ” Command 객체둜 @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄
	//제곡된 νšŒμ›μ •λ³΄λ₯Ό 전달받아 μ €μž₯ν•˜κ³  λ³€κ²½κ°’μœΌλ‘œ ν•„λ“œκ°’λ§Œ λ³€κ²½ν•΄ μ‚¬μš© 
	//μš”μ²­ 처리 λ©”μ†Œλ“œμ— SessionStatus μΈν„°νŽ˜μ΄μŠ€λ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ SessionStatus 객체λ₯Ό
	//μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
	// => SessionStatus 객체 : @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ 제곡된 μ œν•œμ μΈ Session
	//Scope μ†μ„±κ°’μ˜ μƒνƒœμ •λ³΄λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ 객체
	@RequestMapping(value="/hewon_update", method = RequestMethod.POST)
	public String update(@ModelAttribute Hewon hewon, SessionStatus status) {
		//SessionStatus.setComplete() : @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ— μ˜ν•΄ 제곡된 μ œν•œμ μΈ
		//Session Scope 속성값을 μ œκ±°ν•˜λŠ” λ©”μ†Œλ“œ
		status.setComplete();
		return "hewon_result";
	}
}





🐣 hewon_view.jsp

  • νšŒμ›μ •λ³΄ λ³€κ²½νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•  수 μžˆλŠ” 링크 제곡 (아이디 전달)

  • @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 경우 아이디λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ νšŒμ›μ •λ³΄ λ³€κ²½ νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νšŒμ›μ •λ³΄λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>νšŒμ›μ •λ³΄</h1>
	<hr>
	<ul>
		<li>아이디 = ${hewon.id }</li>
		<li>이름 = ${hewon.name }</li>
		<li>이메일 = ${hewon.email }</li>
	</ul>
	
	<%-- <button type="button" onclick="location.href='hewon_update?id=${hewon.id}';">νšŒμ›μ •λ³΄λ³€κ²½</button> --%>
	<%-- @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 경우 아이디λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ νšŒμ›μ •λ³΄ λ³€κ²½
	νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νšŒμ›μ •λ³΄λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯ --%>
	<button type="button" onclick="location.href='hewon_update';">νšŒμ›μ •λ³΄λ³€κ²½</button>
</body>
</html>





🐣 hewon_update.jsp

  • νšŒμ›μ •λ³΄λ₯Ό λ³€κ²½ν•˜κΈ° μœ„ν•΄ hidden νƒ€μž…μ˜ μž…λ ₯νƒœκ·Έλ‘œ 아이디 전달

    • 아이디λ₯Ό μž…λ ₯νƒœκ·Έμ˜ μ΄ˆκΈ°κ°’μœΌλ‘œ μ„€μ •ν•˜μ—¬ read-only 속성을 μ‚¬μš©ν•΄ 전달 κ°€λŠ₯
  • @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 경우 아이디λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ νšŒμ›μ •λ³΄ λ³€κ²½νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νšŒμ›μ •λ³΄λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>νšŒμ›μ •λ³΄λ³€κ²½</h1>
	<hr>
	<form action="hewon_update" method="post">
	<!-- νšŒμ›μ •λ³΄λ₯Ό λ³€κ²½ν•˜κΈ° μœ„ν•΄ hidden νƒ€μž…μ˜ μž…λ ₯νƒœκ·Έλ‘œ 아이디 전달 -->
	<!-- => 아이디λ₯Ό μž…λ ₯νƒœκ·Έμ˜ μ΄ˆκΈ°κ°’μœΌλ‘œ μ„€μ •ν•˜μ—¬ read-only 속성을 μ‚¬μš©ν•΄ 전달 κ°€λŠ₯ -->
	<%-- @SessionAttributes μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ 경우 아이디λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ νšŒμ›μ •λ³΄ λ³€κ²½
	νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ—μ„œ νšŒμ›μ •λ³΄λ₯Ό μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯ --%>
	<%-- <input type="hidden" name="id" value="${hewon.id }"> --%>
	<table>
		<tr>
			<td>아이디</td>
			<td>${hewon.id }</td>
		</tr>
		<tr>
			<td>이름</td>
			<td><input type="text" name="name" value="${hewon.name }"></td>
		</tr>
		<tr>
			<td>이메일</td>
			<td><input type="text" name="email" value="${hewon.email }"></td>
		</tr>
		<tr>
			<td colspan="2"><button type="submit">νšŒμ›λ³€κ²½</button></td>
		</tr>
	</table>	
	</form>
</body>
</html>






πŸ“™ Model (redirect)

  • Front ControllerλŠ” μ œκ³΅λ°›μ€ 뷰이름을 InternalResourceViewResolver 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ JSP λ¬Έμ„œλ‘œ λ³€ν™˜ν•˜κ³  JSP λ¬Έμ„œλ‘œ ν¬μ›Œλ“œ μ΄λ™ν•˜μ—¬ 응닡 처리

    • ν¬μ›Œλ“œ 이동 : μ„œλ²„ λ‚΄λΆ€μ—μ„œ ν˜„μž¬ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μŠ€λ ˆλ“œλ₯Ό λ‹€λ₯Έ μ›Ήν”„λ‘œκ·Έλž¨μœΌλ‘œ μ΄λ™ν•˜μ—¬ λͺ…λ Ή μ‹€ν–‰
    • ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†ŒλŠ” λ³€κ²½λ˜μ§€ μ•ŠμœΌλ©° Request Scope 속성값을 객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
  • 뷰이름에 redirect 접두사λ₯Ό μ‚¬μš©ν•˜μ—¬ URL μ£Όμ†Œλ₯Ό λ°˜ν™˜ν•˜λ©΄ Front ControllerλŠ” λ°˜ν™˜λ°›μ€ λ·°μ΄λ¦„μ˜ URL μ£Όμ†Œλ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 전달 (λ¦¬λ‹€μ΄λ ‰νŠΈ 이동)

    • ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ URL μ£Όμ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬ 응닡 μ²˜λ¦¬ν•˜λ©΄ ν΄λΌμ΄μ–ΈνŠΈλŠ” ν•΄λ‹Ή URL μ£Όμ†Œλ‘œ λΈŒλΌμš°μ €μ˜ μ£Όμ†Œλ₯Ό λ³€κ²½ν•˜κ³  μ„œλ²„μ— URL μ£Όμ†Œμ˜ νŽ˜μ΄μ§€ μš”μ²­ 처리
    • λ¦¬λ‹€μ΄λ ‰νŠΈ 이동 : ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ URL μ£Όμ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬ νŽ˜μ΄μ§€λ₯Ό μž¬μš”μ²­ν•˜μ—¬ μ›Ήν”„λ‘œκ·Έλž¨μ˜ λͺ…λ Ή μ‹€ν–‰
    • ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†Œ λ³€κ²½λ˜μ§€λ§Œ Request Scope 속성값 μ‚¬μš© λΆˆκ°€λŠ₯
    • return "redirect:/redirect_move";
    • Model 객체에 μ €μž₯된 속성값은 λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λœ νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ μ‚¬μš© λΆˆκ°€λŠ₯
  • RedirectAttributes 객체 : λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λ˜λŠ” νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ μ‚¬μš© κ°€λŠ₯ν•œ Request Scope 속성값을 μ œκ³΅ν•˜λŠ” 객체

    • RedirectAttributes.addFlashAttribute(String attributeName, Object attributeValue) : 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜μ—¬ λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λ˜λŠ” νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—κ²Œ 속성값을 μ œκ³΅ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ (Request Scope)
    • public String redirect(RedirectAttributes attributes) {attributes.addFlashAttribute("name", "μž„κΊ½μ •");}


package xyz.itwill10.controller;

@Controller
public class RedirectController {
	@RequestMapping("/forward_move")
	public String forward(Model model) {
		//Model 객체λ₯Ό μ΄μš©ν•˜μ—¬ λ·°(View)μ—κ²Œ μ œκ³΅ν•  객체λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ €μž₯ - Request Scope
		model.addAttribute("name", "홍길동");
		//λ·°λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 뷰이름(ViewName) λ°˜ν™˜
		// => Front ControllerλŠ” μ œκ³΅λ°›μ€ 뷰이름을 InternalResourceViewResolver 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ 
		//JSP λ¬Έμ„œλ‘œ λ³€ν™˜ν•˜κ³  JSP λ¬Έμ„œλ‘œ ν¬μ›Œλ“œ μ΄λ™ν•˜μ—¬ 응닡 처리
		//ν¬μ›Œλ“œ 이동 : μ„œλ²„ λ‚΄λΆ€μ—μ„œ ν˜„μž¬ μ›Ήν”„λ‘œκ·Έλž¨μ˜ μŠ€λ ˆλ“œλ₯Ό λ‹€λ₯Έ μ›Ήν”„λ‘œκ·Έλž¨μœΌλ‘œ μ΄λ™ν•˜μ—¬ λͺ…λ Ή μ‹€ν–‰
		// => ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†ŒλŠ” λ³€κ²½λ˜μ§€ μ•ŠμœΌλ©° Request Scope 속성값을 객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš© κ°€λŠ₯
		return "display_forward";
	}
	
	/*
	@RequestMapping("/redirect_move")
	public String redirect(Model model) {
		model.addAttribute("name", "μž„κΊ½μ •");
		return "display_redirect";
	}
	
	@RequestMapping("/redirect")
	public String redirect() {
		//뷰이름에 redirect 접두사λ₯Ό μ‚¬μš©ν•˜μ—¬ URL μ£Όμ†Œλ₯Ό λ°˜ν™˜ν•˜λ©΄ Front ControllerλŠ”
		//λ°˜ν™˜λ°›μ€ λ·°μ΄λ¦„μ˜ URL μ£Όμ†Œλ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 전달 - λ¦¬λ‹€μ΄λ ‰νŠΈ 이동
		// => ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ URL μ£Όμ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬ 응닡 μ²˜λ¦¬ν•˜λ©΄ ν΄λΌμ΄μ–ΈνŠΈλŠ” ν•΄λ‹Ή URL μ£Όμ†Œλ‘œ
		//λΈŒλΌμš°μ €μ˜ μ£Όμ†Œλ₯Ό λ³€κ²½ν•˜κ³  μ„œλ²„μ— URL μ£Όμ†Œμ˜ νŽ˜μ΄μ§€ μš”μ²­ 처리
		//λ¦¬λ‹€μ΄λ ‰νŠΈ 이동 : ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ URL μ£Όμ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬ νŽ˜μ΄μ§€λ₯Ό μž¬μš”μ²­ν•˜μ—¬ μ›Ήν”„λ‘œκ·Έλž¨μ˜ λͺ…λ Ή μ‹€ν–‰
		// => ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­ URL μ£Όμ†Œ λ³€κ²½λ˜μ§€λ§Œ Request Scope 속성값 μ‚¬μš© λΆˆκ°€λŠ₯
		return "redirect:/redirect_move";
	}
	*/
	
	@RequestMapping("/redirect_move")
	public String redirect() {
		return "display_redirect";
	}
	
	/*
	@RequestMapping("/redirect")
	public String redirect(Model model) {
		//Model 객체에 μ €μž₯된 속성값은 λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λœ νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ μ‚¬μš© λΆˆκ°€λŠ₯
		model.addAttribute("name", "μž„κΊ½μ •");//Request Scope
		return "redirect:/redirect_move";
	}
	*/

	//RedirectAttributes 객체 : λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λ˜λŠ” νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€ λ·°μ—μ„œ μ‚¬μš©
	//κ°€λŠ₯ν•œ Request Scope 속성값을 μ œκ³΅ν•˜λŠ” 객체  
	@RequestMapping("/redirect")
	public String redirect(RedirectAttributes attributes) {
		//RedirectAttributes.addFlashAttribute(String attributeName, Object attributeValue)
		// => 속성λͺ…κ³Ό 속성값을 μ €μž₯ν•˜μ—¬ λ¦¬λ‹€μ΄λ ‰νŠΈ μ΄λ™λ˜λŠ” νŽ˜μ΄μ§€μ˜ μš”μ²­ 처리 λ©”μ†Œλ“œμ™€
		//λ·°μ—κ²Œ 속성값을 μ œκ³΅ν•˜κΈ° μœ„ν•œ λ©”μ†Œλ“œ - Request Scope
		attributes.addFlashAttribute("name", "μž„κΊ½μ •");
		return "redirect:/redirect_move";
	}
}





🐣 응닡 (forword)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>ν¬μ›Œλ“œ 이동</h1>
	<hr>
	<h2>${name }λ‹˜, ν™˜μ˜ν•©λ‹ˆλ‹€.</h2>
</body>
</html>





🐣 응닡 (redirect)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>λ¦¬λ‹€μ΄λ ‰νŠΈ 이동</h1>
	<hr>
	<h2>${name }λ‹˜, ν™˜μ˜ν•©λ‹ˆλ‹€.</h2>
</body>
</html>





πŸ“• servlet-context.xml (resource)

  • resources : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ„ Front Controllerκ°€ 직접 응닡 μ²˜λ¦¬λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ
    • ν΄λΌμ΄μ–ΈνŠΈμ˜ λ¦¬μ†ŒμŠ€ 파일 μš”μ²­μ— λŒ€ν•œ 응닡 처리λ₯Ό μœ„ν•΄ μ‚¬μš©ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ
    • mapping 속성 : ν΄λΌμ΄μ–ΈνŠΈμ— μ˜ν•΄ μš”μ²­λ˜λŠ” λ¦¬μ†ŒμŠ€ 파일의 μš”μ²­ URL μ£Όμ†Œ νŒ¨ν„΄μ„ μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • [**] νŒ¨ν„΄λ¬Έμžλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ‚¬μš©ν•œ 경우 ν˜„μž¬ 폴더 및 ν•˜μœ„ ν΄λ”μ˜ λ¦¬μ†ŒμŠ€ 파일 μš”μ²­ κ°€λŠ₯
    • location 속성 : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ κ²€μƒ‰ν•˜κΈ° μœ„ν•œ 폴더λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • <resources mapping="/images/**" location="/resources/images/" />

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


	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<!-- resources : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ„ Front Controllerκ°€ 직접 응닡 μ²˜λ¦¬λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- => ν΄λΌμ΄μ–ΈνŠΈμ˜ λ¦¬μ†ŒμŠ€ 파일 μš”μ²­μ— λŒ€ν•œ 응닡 처리λ₯Ό μœ„ν•΄ μ‚¬μš©ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ -->
	<!-- mapping 속성 : ν΄λΌμ΄μ–ΈνŠΈμ— μ˜ν•΄ μš”μ²­λ˜λŠ” λ¦¬μ†ŒμŠ€ 파일의 μš”μ²­ URL μ£Όμ†Œ νŒ¨ν„΄μ„ μ†μ„±κ°’μœΌλ‘œ μ„€μ • -->
	<!-- => [**] νŒ¨ν„΄λ¬Έμžλ₯Ό μ†μ„±κ°’μœΌλ‘œ μ‚¬μš©ν•œ 경우 ν˜„μž¬ 폴더 및 ν•˜μœ„ ν΄λ”μ˜ λ¦¬μ†ŒμŠ€ 파일 μš”μ²­ κ°€λŠ₯ -->
	<!-- location 속성 : ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— μ˜ν•΄ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ κ²€μƒ‰ν•˜κΈ° μœ„ν•œ 폴더λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ • -->
	<!-- <resources mapping="/resources/**" location="/resources/" /> -->
	<resources mapping="/images/**" location="/resources/images/" />
	<resources mapping="/css/**" location="/resources/css/" />


</beans:beans>





πŸ“™ Model (resource)


package xyz.itwill10.controller;

@Controller
public class ResourceController {
	@RequestMapping("/resource")
	public String resource() {
		return "resource_display";
	}
}





🐣 응닡 (img)

  • λ¦¬μ†ŒμŠ€ 파일(Resource File) : ν΄λΌμ΄μ–ΈνŠΈκ°€ μ›Ήλ¬Έμ„œ(HTML λ¬Έμ„œ)λ₯Ό ν•΄μ„ν•˜μ—¬ μ‹€ν–‰ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ 정보λ₯Ό μ œκ³΅ν•˜λŠ” μ„œλ²„μ— μ €μž₯된 파일
    • CSS 파일, JavaScript Source 파일, λ©€ν‹°λ―Έλ””μ–΄ 파일 λ“±
    • λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ WEB-INF 폴더에 μ €μž₯ν•  경우 ν΄λΌμ΄μ–ΈνŠΈλŠ” λ¦¬μ†ŒμŠ€ 파일 μš”μ²­ λΆˆκ°€λŠ₯
    • WEB-INF 폴더 : μ›Ήμ„œλ²„μ—μ„œ μ‹€ν–‰λ˜λŠ” ν”„λ‘œκ·Έλž¨(μ„œλΈ”λ¦Ώ)μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯ν•œ 폴더 (ν΄λΌμ΄μ–ΈνŠΈμ—κ²ŒλŠ” 은닉화 처리)
  • SpringMvc ν”„λ‘œκ·Έλž¨μ—μ„œλŠ” ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­μ„ Front Controllerλ₯Ό μ΄μš©ν•˜μ—¬ μ²˜λ¦¬λ˜λ„λ‘ μ„€μ •

    • 문제점 : ν΄λΌμ΄μ–ΈνŠΈκ°€ μ„œλ²„μ˜ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μš”μ²­ν•  경우 Front Controllerκ°€ μš”μ²­μ„ λ°›μ•„ λ¦¬μ†ŒμŠ€ 파일의 μš”μ²­ URL μ£Όμ†Œμ™€ λ§€ν•‘λœ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό κ²€μƒ‰ν•˜μ§€λ§Œ μ—†μœΌλ―€λ‘œ 404 μ—λŸ¬μ½”λ“œ λ°œμƒ
    • Front Controller을 νŠΉμ • ν™•μž₯자둜 νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•˜λŠ” 경우 μ œμ™Έ (*.do)
    • 해결법) ν΄λΌμ΄μ–ΈνŠΈκ°€ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μš”μ²­ν•œ 경우 Front Controllerκ°€ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ§€ μ•Šκ³  직접 μ‘λ‹΅λ˜λ„λ‘ Spring Bean Configuration File μ„€μ • β†’ servlet-context.xml
  • λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μƒλŒ€κ²½λ‘œλ‘œ ν‘œν˜„ν•˜μ—¬ μ²˜λ¦¬ν•œ 경우 νŽ˜μ΄μ§€μ˜ μš”μ²­ URL μ£Όμ†Œ κ²½λ‘œμ™€ λ¦¬μ†ŒμŠ€ 파일의 κ²½λ‘œκ°€ λ‹€λ₯Έ 경우 404 μ—λŸ¬μ½”λ“œ λ°œμƒ (λ¦¬μ†ŒμŠ€ νŒŒμΌμ€ μ ˆλŒ€κ²½λ‘œλ‘œ ν‘œν˜„ν•˜λŠ” 것을 ꢌμž₯)

    • <img src="images/Koala.jpg" width="200">
  • μ»¨ν…μŠ€νŠΈ 이름이 변경될 경우 μ»¨ν…μŠ€νŠΈ κ²½λ‘œκ°€ λ³€κ²½λ˜μ–΄ 404 μ—λŸ¬ μ½”λ“œ λ°œμƒ

    • μ»¨ν…μŠ€νŠΈ κ²½λ‘œλŠ” 직접 μ§€μ •ν•˜μ§€ μ•Šκ³  객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš©
    • <img src="/spring/images/Koala.jpg" width="200">
  • μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό EL의 pageContext λ‚΄μž₯객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš©

    • <img src="${pageContext.request.contextPath }/images/Koala.jpg">
  • μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό Core νƒœκ·Έ 라이브러리의 URL νƒœκ·Έλ‘œ μ œκ³΅λ°›μ•„ μ‚¬μš©

    • <img src="<c:url value="/images/Koala.jpg"/>" width="200">
  • μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό Spring νƒœκ·Έ 라이브러리의 URL νƒœκ·Έλ‘œ μ œκ³΅λ°›μ•„ μ‚¬μš©

    • <img src="<spring:url value="/images/Koala.jpg"/>" width="200">


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
<link href="<c:url value="/css/style.css"/>" type="text/css" rel="stylesheet">
</head>
<body>
	<h1 class="text">λ¦¬μ†ŒμŠ€ 파일</h1>
	<hr>
	<%-- λ¦¬μ†ŒμŠ€ 파일(Resource File) : ν΄λΌμ΄μ–ΈνŠΈκ°€ μ›Ήλ¬Έμ„œ(HTML λ¬Έμ„œ)λ₯Ό ν•΄μ„ν•˜μ—¬ μ‹€ν–‰ν•˜κΈ° 
	μœ„ν•΄ ν•„μš”ν•œ	정보λ₯Ό μ œκ³΅ν•˜λŠ” μ„œλ²„μ— μ €μž₯된 파일 --%>
	<%-- => CSS 파일, JavaScript Source 파일, λ©€ν‹°λ―Έλ””μ–΄ 파일 λ“± --%>
	<%-- => λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ WEB-INF 폴더에 μ €μž₯ν•  경우 ν΄λΌμ΄μ–ΈνŠΈλŠ” λ¦¬μ†ŒμŠ€ 파일 μš”μ²­ λΆˆκ°€λŠ₯  --%>
	<%-- WEB-INF 폴더 : μ›Ήμ„œλ²„μ—μ„œ μ‹€ν–‰λ˜λŠ” ν”„λ‘œκ·Έλž¨(μ„œλΈ”λ¦Ώ)μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯ν•œ 폴더 - ν΄λΌμ΄μ–ΈνŠΈμ—κ²ŒλŠ” 은닉화 처리 --%>
	
	<%-- SpringMvc ν”„λ‘œκ·Έλž¨μ—μ„œλŠ” ν΄λΌμ΄μ–ΈνŠΈμ˜ λͺ¨λ“  μš”μ²­μ„ Front Controllerλ₯Ό μ΄μš©ν•˜μ—¬ μ²˜λ¦¬λ˜λ„λ‘ μ„€μ • --%>
	<%-- 문제점)ν΄λΌμ΄μ–ΈνŠΈκ°€ μ„œλ²„μ˜ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μš”μ²­ν•  경우 Front Controllerκ°€ μš”μ²­μ„ λ°›μ•„ λ¦¬μ†ŒμŠ€ 
	파일의 μš”μ²­ URL μ£Όμ†Œμ™€ λ§€ν•‘λœ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό κ²€μƒ‰ν•˜μ§€λ§Œ μ—†μœΌλ―€λ‘œ 404 μ—λŸ¬μ½”λ“œ λ°œμƒ --%>
	<%-- => Front Controller을 νŠΉμ • ν™•μž₯자둜 νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•˜λŠ” 경우 μ œμ™Έ --%>
	<%-- 해결법)ν΄λΌμ΄μ–ΈνŠΈκ°€ λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μš”μ²­ν•œ 경우 Front Controllerκ°€ μš”μ²­ 처리 λ©”μ†Œλ“œλ₯Ό 
	ν˜ΈμΆœν•˜μ§€ μ•Šκ³  직접 μ‘λ‹΅λ˜λ„λ‘ Spring Bean Configuration File μ„€μ • - servlet-context.xml --%>
	
	<%-- λ¦¬μ†ŒμŠ€ νŒŒμΌμ„ μƒλŒ€κ²½λ‘œλ‘œ ν‘œν˜„ν•˜μ—¬ μ²˜λ¦¬ν•œ 경우 νŽ˜μ΄μ§€μ˜ μš”μ²­ URL μ£Όμ†Œ κ²½λ‘œμ™€ λ¦¬μ†ŒμŠ€ 
	파일의 κ²½λ‘œκ°€ λ‹€λ₯Έ 경우 404 μ—λŸ¬μ½”λ“œ λ°œμƒ - λ¦¬μ†ŒμŠ€ νŒŒμΌμ€ μ ˆλŒ€κ²½λ‘œλ‘œ ν‘œν˜„ν•˜λŠ” 것을 ꢌμž₯ --%>
	<%-- <img src="resources/Koala.jpg" width="200"> --%>
	<img src="images/Koala.jpg" width="200">
	
	<%-- μ»¨ν…μŠ€νŠΈ 이름이 변경될 경우 μ»¨ν…μŠ€νŠΈ κ²½λ‘œκ°€ λ³€κ²½λ˜μ–΄ 404 μ—λŸ¬ μ½”λ“œ λ°œμƒ --%>
	<%-- => μ»¨ν…μŠ€νŠΈ κ²½λ‘œλŠ” 직접 μ§€μ •ν•˜μ§€ μ•Šκ³  객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš© --%>
	<img src="/spring/images/Koala.jpg" width="200">
	
	<%-- μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό EL의 pageContext λ‚΄μž₯객체둜 μ œκ³΅λ°›μ•„ μ‚¬μš© --%>
	<img src="${pageContext.request.contextPath }/images/Koala.jpg" width="200">

	<%-- μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό Core νƒœκ·Έ 라이브러리의 URL νƒœκ·Έλ‘œ μ œκ³΅λ°›μ•„ μ‚¬μš© --%>
	<img src="<c:url value="/images/Koala.jpg"/>" width="200">

	<%-- μ»¨ν…μŠ€νŠΈ 경둜λ₯Ό Spring	νƒœκ·Έ 라이브러리의 URL νƒœκ·Έλ‘œ μ œκ³΅λ°›μ•„ μ‚¬μš© --%>
	<img src="<spring:url value="/images/Koala.jpg"/>" width="200">
</body>
</html>





profile
Study Log πŸ“‚

0개의 λŒ“κΈ€