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

Spring/Spring Quick Start

Model 2 ์•„ํ‚คํ…์ณ๋กœ ๊ฒŒ์‹œํŒ ๊ฐœ๋ฐœ (Controller์˜ ๋“ฑ์žฅ)

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


Model 2 ์•„ํ‚คํ…์ฒ˜ ๊ตฌ์กฐ

ํ•ต์‹ฌ์€ Controller์˜ ๋“ฑ์žฅ์ด๋‹ค.

๊ธฐ์กด Model 1์—์„œ๋Š” JSP๊ฐ€ Controller์™€ View ๊ธฐ๋Šฅ ๋ชจ๋‘๋ฅผ ๋‹ด๋‹นํ–ˆ๋Š”๋ฐ

Model 2 JSP๋Š” ๋””์ž์ธ๊ณผ ๊ฐ™์€ View๋ฅผ ๋‹ด๋‹นํ•˜๊ณ , Servlet ํด๋ž˜์Šค๊ฐ€ Controller๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

MVC ์•„ํ‚คํ…์ณ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด Controller์ด๋‹ค. Controller๋ฅผ ์„ฑ๋Šฅ๊ณผ ์œ ์ง€๋ณด์ˆ˜์˜ ํŽธ์˜์„ฑ์„ ๊ณ ๋ คํ•˜์—ฌ ์ž˜ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฌด์—‡๋ณด๋‹ค ์ค‘์š”ํ•˜๋‹ค.

 

์ •๋ฆฌํ•˜๋ฉด ์ด๋ ‡๋‹ค.

 

  • ์ธ์ฝ”๋”ฉ ์ž‘์—…์„ DispatcherServlet ํด๋ž˜์Šค์—์„œ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์ธ์ฝ”๋”ฉ์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” DispatcherServlet ํด๋ž˜์Šค๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค. ๊ธ€ ๋“ฑ๋ก, ์ˆ˜์ • ์ž‘์—…์—์„œ ๋” ์ด์ƒ ํ•œ๊ธ€์ด ๊นจ์ง€๋Š” ์ผ์€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • get ๋ฐฉ์‹์ด๋“  post ๋ฐฉ์‹์ด๋“  process() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

  • HttpSession์€ ๋ธŒ๋ผ์šฐ์ €๋‹น ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์— ํ•˜๋‚˜์”ฉ ์œ ์ง€๋˜๋Š” ๊ฐ์ฒด์ด๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์„ธ์…˜์ด ๋งŽ์•„์ง€๊ณ  ์„œ๋ฒ„์—๊ฒŒ ๋ถ€๋‹ด์„ ์ฃผ๊ฒŒ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ณดํ†ต ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋Š” HttpSession์ด ์•„๋‹Œ HttpServletRequest ๊ฐ์ฒด์— ์ €์žฅํ•˜์—ฌ ๊ณต์œ ํ•œ๋‹ค. HttpServletRequest ๊ฐ์ฒด๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์š”์ฒญ์„ ์ „์†กํ•  ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋˜๊ณ , ์‘๋‹ต ๋ฉ”์„ธ์ง€๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์— ์ „์†ก๋˜๋ฉด ๋ฐ”๋กœ ์‚ญ์ œ๋˜๋Š” 1ํšŒ์„ฑ ๊ฐ์ฒด์ด๋‹ค.


DispatcherServlet.java

package org.jueun.view.controller;

import org.jueun.biz.board.BoardVO;
import org.jueun.biz.board.impl.BoardDAO;
import org.jueun.biz.user.UserVO;
import org.jueun.biz.user.impl.UserDAO;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


// ๊ธ€ ๋“ฑ๋ก, ์ˆ˜์ • ์ž‘์—…์—์„œ ๋” ์ด์ƒ ํ•œ๊ธ€์ด ๊นจ์ง€๋Š” ์ผ์€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
// ์ธ์ฝ”๋”ฉ ์ž‘์—…์„ DispatcherServlet ํด๋ž˜์Šค์—์„œ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์ธ์ฝ”๋”ฉ์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” DispatcherServlet ํด๋ž˜์Šค๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.
public class DispatcherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public DispatcherServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        process(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("EUC-KR");
        process(request, response);
    }
    // get ๋ฐฉ์‹์ด๋“  post ๋ฐฉ์‹์ด๋“  process() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
    private void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ path ์ •๋ณด๋ฅผ ์ถ”์ถœํ•œ๋‹ค.
        String uri = request.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/"));
        System.out.println(path);

        // 2. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ path์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ ํ•œ๋‹ค.
        if (path.equals("/login.do")) {
            System.out.println("๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ");
            // 1. ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ
            String id = request.getParameter("id");
            String password = request.getParameter("password");

            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            UserVO vo = new UserVO();
            vo.setId(id);
            vo.setPassword(password);

            UserDAO userDAO = new UserDAO();
            UserVO user = userDAO.getUser(vo);

            // 3. ํ™”๋ฉด ๋„ค๋น„๊ฒŒ์ด์…˜
            if (user != null) {
                response.sendRedirect("getBoardList.do");
            } else {
                response.sendRedirect("login.jsp");
            }
        } else if (path.equals("/logout.do")) {
            System.out.println("๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌ");
            // 1. ๋ธŒ๋ผ์šฐ์ €์™€ ์—ฐ๊ฒฐ๋œ ์„ธ์…˜ ๊ฐ์ฒด๋ฅผ ๊ฐ•์ œ ์ข…๋ฃŒํ•œ๋‹ค.
            HttpSession session = request.getSession();
            session.invalidate();

            // 2. ์„ธ์…˜ ์ข…๋ฃŒ ํ›„, ๋ฉ”์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
            response.sendRedirect("login.jsp");

        } else if (path.equals("/insertBoard.do")) {
            System.out.println("๊ธ€ ๋“ฑ๋ก ์ฒ˜๋ฆฌ");
            // 1. ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ
            // request.setCharacterEncoding("EUC-KR");
            String title = request.getParameter("title");
            String writer = request.getParameter("writer");
            String content = request.getParameter("content");

            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            BoardVO vo = new BoardVO();
            vo.setTitle(title);
            vo.setWriter(writer);
            vo.setContent(content);

            BoardDAO boardDAO = new BoardDAO();
            boardDAO.insertBoard(vo);

            // 3. ํ™”๋ฉด ๋„ค๋น„๊ฒŒ์ด์…˜
            response.sendRedirect("getBoardList.do");

        } else if (path.equals("/updateBoard.do")) {
            System.out.println("๊ธ€ ์ˆ˜์ • ์ฒ˜๋ฆฌ");
            // 1. ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ
            // request.setCharacterEncoding("EUC-KR");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String seq = request.getParameter("seq");

            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            BoardVO vo = new BoardVO();
            vo.setTitle(title);
            vo.setContent(content);
            vo.setSeq(Integer.parseInt(seq));

            BoardDAO boardDAO = new BoardDAO();
            boardDAO.updateBoard(vo);

            // 3. ํ™”๋ฉด ๋„ค๋น„๊ฒŒ์ด์…˜
            response.sendRedirect("getBoardList.do");

        } else if (path.equals("/deleteBoard.do")) {
            System.out.println("๊ธ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ");
            // 1. ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ
            String seq = request.getParameter("seq");

            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            BoardVO vo = new BoardVO();
            vo.setSeq(Integer.parseInt(seq));

            BoardDAO boardDAO = new BoardDAO();
            boardDAO.deleteBoard(vo);

            // 3. ํ™”๋ฉด ๋„ค๋น„๊ฒŒ์ด์…˜
            response.sendRedirect("getBoardList.do");

        } else if (path.equals("/getBoard.do")) {
            System.out.println("๊ธ€ ์ƒ์„ธ ์กฐํšŒ ์ฒ˜๋ฆฌ");
            // 1. ๊ฒ€์ƒ‰ํ•  ๊ฒŒ์‹œ๊ธ€ ๋ฒˆํ˜ธ ์ถ”์ถœ
            String seq = request.getParameter("seq");

            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            BoardVO vo = new BoardVO();
            vo.setSeq(Integer.parseInt(seq));

            BoardDAO boardDAO = new BoardDAO();
            BoardVO board = boardDAO.getBoard(vo);

            // 3. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์„ธ์…˜์— ์ €์žฅํ•˜๊ณ  ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
            HttpSession session = request.getSession();
            session.setAttribute("board", board);
            response.sendRedirect("getBoard.jsp");

        } else if (path.equals("/getBoardList.do")) {
            System.out.println("๊ธ€ ๋ชฉ๋ก ๊ฒ€์ƒ‰ ์ฒ˜๋ฆฌ");
            // 1. ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ •๋ณด ์ถ”์ถœ(๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์€ ๋‚˜์ค‘์— ๊ตฌํ˜„)
            // 2. DB ์—ฐ๋™ ์ฒ˜๋ฆฌ
            BoardVO vo = new BoardVO();
            BoardDAO boardDAO = new BoardDAO();
            List<BoardVO> boardList = boardDAO.getBoardList(vo);

            // 3. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์„ธ์…˜์— ์ €์žฅํ•˜๊ณ  ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
            HttpSession session = request.getSession();
            session.setAttribute("boardList", boardList);
            response.sendRedirect("getBoardList.jsp");
        }
    }
}