๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring/Spring Quick Start

์–ด๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ•œ Spring MVC ๊ฒŒ์‹œํŒ ๊ฐœ๋ฐœ

 

Spring Quick Start(์ฑ…)์˜ Day4 class01~02 ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ธฐ๋กํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์‹ค์Šตํ•œ ์ฝ”๋“œ์˜ ๋งํฌ์ž…๋‹ˆ๋‹ค.


1. ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด์— @Controller ๋“ฑ๋กํ•˜๊ธฐ

@Controller๋กœ ๋“ฑ๋ก๋œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์Šค์บ”๋˜๊ธฐ ์œ„ํ•ด

์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ(webapp/WEB-INF/presentation-layer.xml)์—

<context:component-scan>์œผ๋กœ ํŒจํ‚ค์ง€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

<context:component-scan base-package="com.springbook.view"> </context:component-scan>

์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์— @Component๋ฅผ ์ƒ์†ํ•œ @Controller๋ฅผ ๋“ฑ๋กํ•œ๋‹ค.


2. @RequestMapping ์‚ฌ์šฉํ•˜๋ฉด์„œ Controller ํ†ตํ•ฉํ•˜๊ธฐ

@RequestMapping

์Šคํ”„๋ง์—์„œ๋Š” @RequestMapping์„ ์ด์šฉํ•˜์—ฌ HandlerMapping ์„ค์ •์„ ๋Œ€์ฒดํ•œ๋‹ค.

HandlerMapping ์€ ์ด์ „์— ์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ์—์„œ ์ง€์ •ํ–ˆ์—ˆ๋‹ค.

value ์†์„ฑ์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋Œ€๋ถ€๋ถ„ ์ƒ๋žตํ•œ๋‹ค.

๊ฐ ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ํƒ€์ž…์€ String์œผ๋กœ ํ†ต์ผํ•˜์˜€๊ณ  ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋Š” Model์— ์ €์žฅํ–ˆ๋‹ค. ์ด๋Š” ModelAndView์™€ ๋™์ผํ•˜๊ฒŒ JSP ํ™”๋ฉด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

package com.springbook.view.board;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.springbook.biz.board.BoardVO;
import com.springbook.biz.board.impl.BoardDAO;

@Controller
@SessionAttributes("board")
public class BoardController {
	
	// ๊ฒ€์ƒ‰ ์กฐ๊ฑด ๋ชฉ๋ก ์„ค์ •
	@ModelAttribute("conditionMap")
	public Map<String, String> searchConditionMap(){
		Map<String, String> conditionMap = new HashMap<String, String>();
		conditionMap.put("์ œ๋ชฉ", "TITLE");
		conditionMap.put("๋‚ด์šฉ", "CONTENT");
		return conditionMap;
	}
	
	// ๊ธ€ ๋“ฑ๋ก
	@RequestMapping(value = "/insertBoard.do")
	public String insertBoard(BoardVO vo, BoardDAO boardDAO) { 
    // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋ฉด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ „๋‹ฌํ•ด์ค€๋‹ค.
		boardDAO.insertBoard(vo);
		return "getBoardList.do";
	}

	// ๊ธ€ ์ˆ˜์ •
	@RequestMapping("/updateBoard.do")
	public String updateBoard(@ModelAttribute("board") BoardVO vo, BoardDAO boardDAO) {
		System.out.println("๋ฒˆํ˜ธ : " + vo.getSeq());
		System.out.println("์ œ๋ชฉ : " + vo.getTitle());
		System.out.println("์ž‘์„ฑ์ž : " + vo.getWriter());
		System.out.println("๋‚ด์šฉ : " + vo.getContent());
		System.out.println("๋“ฑ๋ก์ผ : " + vo.getRegDate());
		System.out.println("์กฐํšŒ์ˆ˜ : " + vo.getCnt());
		
		boardDAO.updateBoard(vo);
		return "getBoardList.do";
	}

	// ๊ธ€ ์‚ญ์ œ
	@RequestMapping("/deleteBoard.do")
	public String deleteBoard(BoardVO vo, BoardDAO boardDAO) {
		boardDAO.deleteBoard(vo);
		return "getBoardList.do";
	}

	// ๊ธ€ ์ƒ์„ธ ์กฐํšŒ
	@RequestMapping("/getBoard.do")
	public String getBoard(BoardVO vo, BoardDAO boardDAO, Model model) {
		model.addAttribute("board", boardDAO.getBoard(vo)); // Model ์ •๋ณด ์ €์žฅ
		return "getBoard.jsp"; // View ์ด๋ฆ„ ๋ฆฌํ„ด
	}

	// ๊ธ€ ๋ชฉ๋ก ๊ฒ€์ƒ‰
	@RequestMapping("/getBoardList.do")
	public String getBoardList(BoardVO vo, BoardDAO boardDAO, Model model) {		
		// 3๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋Š”๋ฐ ๋งˆ์ง€๋ง‰์€ Model์— ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์“ด๋‹ค.
		model.addAttribute("boardList", boardDAO.getBoardList(vo));																
		return "getBoardList.jsp"; // View ์ด๋ฆ„ ๋ฆฌํ„ด
	}
}

@ModelAttribute ๋ฉ”์†Œ๋“œ

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ @ModelAttribute ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋œ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

@ModelAttribute๊ฐ€ ์„ค์ •๋œ ๋ฉ”์†Œ๋“œ๋Š” @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋œ ๋ฉ”์†Œ๋“œ๋ณด๋‹ค ๋จผ์ € ํ˜ธ์ถœ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ ๋ฆฌํ„ด๋œ ๊ฐ์ฒด๋Š” ์ž๋™์œผ๋กœ Model์— ์ €์žฅ๋˜์–ด View ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@ModelAttribute ๋งค๊ฐœ๋ณ€์ˆ˜

Command ๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ๋•Œ @ModelAttribute๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

updateBoard()์—์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑํ•˜๋Š” Command ๊ฐ์ฒด์˜ ์ด๋ฆ„์€ ํด๋ž˜์Šค ์ด๋ฆ„์˜ ์ฒซ ๊ธ€์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ "boardVO"๋กœ ์ž๋™์œผ๋กœ ์„ค์ •๋œ๋‹ค. ์ด๋ฅผ @ModelAttribute("board")๋กœ "board"๋กœ ๋ฐ”๊ฟจ๋‹ค.

@SessionAttributes

BoardController์˜ ๋ฐ”๋กœ ์œ„์— @SessionAttributes("board")๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๋‹ค.

Model์— "board"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์„ธ์…˜(HttpSession)์—๋„ ์ž๋™์œผ๋กœ ์ €์žฅํ•˜๋ผ๋Š” ์„ค์ •์ด๋‹ค.

๐Ÿฐ ์–ด๋…ธํ…Œ์ด์…˜ ๋™์ž‘ ์„ค๋ช…

getBoard() ์‹คํ–‰ ์‹œ, 1. Model์— "board"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ BoardVO ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋˜๊ณ  2. @SessionAttributes("board")์— ์˜ํ•ด ์„ธ์…˜์—๋„ "board"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ BoardVO ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ๋‹ค.

์ดํ›„ update() ์‹คํ–‰ ์‹œ, @ModelAttribute("board")์— ์˜ํ•ด ์„ธ์…˜์— board๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์„ธ์…˜์—์„œ ๊บผ๋‚ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋œ vo ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ์ดํ›„ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ vo ๊ฐ์ฒด์— ํ• ๋‹นํ•œ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆ˜์ • ์ •๋ณด(title, content) ๊ฐ’๋งŒ ์ƒˆ๋กญ๊ฒŒ ํ• ๋‹น๋˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์„ธ์…˜์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ง„๋œ๋‹ค.

 

์š”์ฒญ ๋ฐฉ์‹(GET/POST)

@RequestMapping ์š”์ฒญ ๋ฐฉ์‹(GET/POST) ์— ๋”ฐ๋ผ ์ˆ˜ํ–‰๋  ๋ฉ”์†Œ๋“œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

package com.springbook.view.user;

// import ์ƒ๋žต

@Controller
public class LoginController {

	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public String loginView(@ModelAttribute("user") UserVO vo) {
		System.out.println("๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™...");
		vo.setId("test");
		vo.setPassword("test123");
		return "login.jsp";
	}

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(UserVO vo, UserDAO userDAO, HttpSession session) {
		UserVO user = userDAO.getUser(vo);
		if (user != null) {
			session.setAttribute("userName", user.getName());
			return "getBoardList.do";
		} else
			return "login.jsp";
	}
}

Servlet API

๋˜ํ•œ ์Šคํ”„๋ง MVC์—์„œ๋Š” Controller ๋ฉ”์†Œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‹ค์–‘ํ•œ Servlet API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•œ๋‹ค.

login()์—์„œ HttpSession ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.

 

๐Ÿ’ ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ณผ์ •

์ด ํ›„ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ธ€ ๋“ฑ๋ก ์ •๋ณด๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ž…๋ ฅํ•˜๊ณ  ์„œ๋ฒ„์— "insertBoard.do" ์š”์ฒญ์„ ์ „๋‹ฌํ–ˆ์„ ๋•Œ ๊ณผ์ •์€ ์ด๋ ‡๋‹ค. 

 

๐Ÿง€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” @Controller๊ฐ€ ๋ถ™์€ ๋ชจ๋“  ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

๐Ÿง€ ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” BoardVO ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

๐Ÿง€ BoardVO ํด๋ž˜์Šค์˜ Setter ๋ฉ”์†Œ๋“œ๋“ค์„ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ(title, writer, content) ๊ฐ’๋“ค์„ BoardVO ๊ฐ์ฒด์— ์ €์žฅํ•œ๋‹ค. (Setter ์ธ์ ์…˜)

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ form tag ์•ˆ์˜ name="writer" ๊ฐ’์ด

Command ๊ฐ์ฒด์˜ Setter ๋ฉ”์†Œ๋“œ ์ด๋ฆ„ ์ฆ‰ setWriter()์™€ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•ด์•ผ ์ž๋™์œผ๋กœ ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค.

๐Ÿง€ insertBoard() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์ด๋ฏธ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’๋“ค์ด ์„ค์ •๋œ BoardVO ๊ฐ์ฒด๊ฐ€ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค.

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ๊ณผ VO ๊ฐ์ฒด ์ƒ์„ฑ, ๊ทธ๋ฆฌ๊ณ  ๊ฐ’ ์„ค์ •์€ ๋ชจ๋‘ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.


3. JSP์—์„œ Command ๊ฐ์ฒด ์‚ฌ์šฉ

Command ๊ฐ์ฒด๋Š” Controller ๋ฉ”์†Œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ VO ๊ฐ์ฒด์ด๋‹ค.

Command ๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ JSP์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด "${...}" ๊ตฌ๋ฌธ์„ ์ด์šฉํ•œ๋‹ค.

@ModelAttribute("user")๋กœ Command ๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ–ˆ๊ธฐ์— "${user.id}" ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

[login.jsp]

<%@page contentType="text/html; charset=EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>๋กœ๊ทธ์ธ</title>
</head>
<body>
<center>
	<h1>๋กœ๊ทธ์ธ</h1>
	<hr>
	<form action="login.do" method="post">
		<table border="1" cellpadding="0" cellspacing="0">
			<tr>
				<td bgcolor="orange">์•„์ด๋””</td>
				<td><input type="text" name="id" value="${user.id }" /></td>
			</tr>
			<tr>
				<td bgcolor="orange">๋น„๋ฐ€๋ฒˆํ˜ธ</td>
				<td><input type="password" name="password" value="${user.password }"/></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="๋กœ๊ทธ์ธ" /></td>
			</tr>
		</table>
	</form>
	<hr>
</center>
</body>
</html>

user.id, user.password๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

[getBoardList.jsp]

<%@page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>๊ธ€ ๋ชฉ๋ก</title>
</head>
<body>
<center>
	<h1>๊ธ€ ๋ชฉ๋ก</h1>
	<h3>${userName }๋‹˜!
		๊ฒŒ์‹œํŒ์— ์˜ค์‹ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค...<a href="logout.do">Log-out</a>
	</h3>
	<!-- ๊ฒ€์ƒ‰ ์‹œ์ž‘ -->
	<form action="getBoardList.do" method="post">
		<table border="1" cellpadding="0" cellspacing="0" width="700">
			<tr>
				<td align="right">
					<select name="searchCondition">
						<c:forEach items="${conditionMap }" var="option">
						<option value="${option.value }">${option.key }
							</c:forEach>
					</select>
					<input name="searchKeyword" type="text" />
					<input type="submit" value="๊ฒ€์ƒ‰" /></td>
			</tr>
		</table>
	</form>
	<!-- ๊ฒ€์ƒ‰ ์ข…๋ฃŒ -->
	<table border="1" cellpadding="0" cellspacing="0" width="700">
		<tr>
			<th bgcolor="orange" width="100">๋ฒˆํ˜ธ</th>
			<th bgcolor="orange" width="200">์ œ๋ชฉ</th>
			<th bgcolor="orange" width="150">์ž‘์„ฑ์ž</th>
			<th bgcolor="orange" width="150">๋“ฑ๋ก์ผ</th>
			<th bgcolor="orange" width="100">์กฐํšŒ์ˆ˜</th>
		</tr>
		<c:forEach items="${boardList }" var="board">
			<tr>
				<td>${board.seq }</td>
				<td align="left"><a href="getBoard.do?seq=${board.seq }">
						${board.title }</a></td>
				<td>${board.writer }</td>
				<td>${board.regDate }</td>
				<td>${board.cnt }</td>
			</tr>
		</c:forEach>
	</table>
	<br> <a href="insertBoard.jsp">์ƒˆ๊ธ€ ๋“ฑ๋ก</a>
</center>
</body>
</html>

conditionMap์€ BoardController์—์„œ @ModelAttribute("conditionMap")๋กœ ์ง€์ •ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
userName์€ LoginController์˜ login() ์ค‘ session.setAttribute("userName", user.getName());๋กœ ์ง€์ •๋˜์–ด์„œ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ”„๋กœ์ ํŠธ ์ „์ฒด๋Š” ์ตœ์ƒ๋‹จ์˜ ๊นƒํ—™ ๋งํฌ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.