빠에야는 개발중

스프링 퀵 스타트 : chap 3-1 본문

공부/스프링

스프링 퀵 스타트 : chap 3-1

빠에야좋아 2018. 1. 29. 02:35

Model1 아키텍처로 게시판 개발

스프링 MVC를 사용하기 전에 어떤 식으로 아키텍처가 발전해왔는지를 알아보기 위해 구식 아키텍처부터 사용해보기로 했다. 이런 작업은 이전에도 한적이 있었는데 도중에 어떤 이유로 실패했기 때문에 다시 해보는 것이다. (무슨 이유였는지는 잊어버렸다…) 우선 model1부터 시작해보겠다.


Model1은 jsp에서 컨트롤러와 뷰를 모두 담당하는 것이다. 사실 컨트롤러라는 개념이 나중에 나왔으므로 반대로 말한 것이지만 이해하기 편하니까 이렇게 말하겠다. 요청을 받고 데이터를 조작하여 렌더링하는 것까지 모두 jsp에서 이루어지는 모델인데 굉장히, 굉장히 보기 불편하고 짜기도 귀찮게 생겼다. 책을 보고 그대로 옮겨서 타이핑하는 작업임에도 재미없고 지루함을 느꼈다. 그리고 구조 자체가 뭔가 덕지덕지 붙여서 날림으로 만든 것 같다는 느낌을 많이 받았다.


기본적인 CRUD와 로그인, 로그아웃을 수행하는 게시판을 만들었다. 로그인과 로그아웃은 세션관리 그런거 없이 그냥 입력값만 비교하는게 전부다. 그리고 회원가입도 없다(…). 대부분이 반복적인 작업이기 때문에 대충 코드가 어떤 식으로 생겼는지 몇가지 예시를 보이는 것으로 마무리하겠다.


login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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_proc.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td bgcolor="orange">아이디</td>
        <td><input type="text" name="id" /></td>
    </tr>
    <tr>
        <td bgcolor="orange">비밀번호</td>
        <td><input type="password" name="password" /></td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="로그인" />
        </td>
    </tr>
</table>
</form>
<hr>
</center>
</body>
</html>

cs


login_proc.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page import="com.springbook.biz.user.impl.UserDAO" %>
<%@ page import="com.springbook.biz.user.UserVO" %>
<%@ page contentType="text/html; charset=utf-8" %>
 
<%
    String id = request.getParameter("id") ;
    String password = request.getParameter("password");
    
    UserVO vo = new UserVO();
    vo.setId(id);
    vo.setPassword(password);
    
    UserDAO userDAO = new UserDAO();
    UserVO user = userDAO.getUser(vo);
    
    if(user != null) {
        response.sendRedirect("getBoardList.jsp");
    } else {
        response.sendRedirect("login.jsp");
    }
%>
cs


getBoardList.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<%@ page import="java.util.List" %>
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page contentType="text/html; charset=utf-8" %>

<%
    BoardDAO boardDAO = new BoardDAO();
    List<BoardVO> boardList = boardDAO.getBoardList();
%>
 
<!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>테스트님 환영합니다.<a href="logout_proc.jsp">Log-out</a></h3>
 
<!-- 검색 -->
<form action="getBoardList.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
    <td align="right">
        <select name="searchCondition">
            <option value="TITLE">제목
            <option value="CONTENT">내용
        </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>
 
<% for(BoardVO board : boardList ) { %>
<tr>
    <td><%= board.getSeq() %></td>
    <td align="left"><a href="getBoard.jsp?seq=<%= board.getSeq() %>"><%= board.getTitle() %></a>
    </td>
    <td><%= board.getWriter() %></td>
    <td><%= board.getRegDate() %></td>
    <td><%= board.getCnt() %></td>
</tr>
<% } %>
</table>
<br>
<a href="insertBoard.jsp">새글 등록</a>
</center>
</body>
</html>
cs


이처럼 기본적인 html+jsp로 구성된 뷰 페이지와 로직을 담당하는 jsp 프로시저 페이지를 따로 두는 방식이다. html과 자바 코드가 혼재되어 있어서 매우 보기 불편하고 프로시저에서 객체를 생성하는 코드는 가슴 어딘가가 턱하고 막히는 기분이 든다. 빨리 model2, 스프링 MVC로 넘어가고 싶다.


자세한 코드 및 변경사항은 [여기] 에서 볼 수 있다.



여담


위 코드를 짤 때 getBoard.jsp와 getBoardList.jsp 페이지를 로딩하면서 NullPointerException이 뜨는 것을 발견했다. 분명 책대로 따라했는데 왜 NPE를 뱉는지 멘탈붕괴에 빠져서 이것저것 시도를 해보다가 baordDAO를 jdbcTemplate에서 기존 jdbc로 롤백하는 방법으로 해결했다. jdbcTemplate이 리턴할때 RowMapper 객체를 생성하여 query 메소드를 실행하는데 이 과정에서 jsp 상에서는 할 수 없는 문제가 있는 듯 하다. 자바에서는 문제없이 잘 실행되었기 때문이다. 

'공부 > 스프링' 카테고리의 다른 글

스프링 퀵 스타트 : chap 4-1  (1) 2018.01.31
스프링 퀵 스타트 : chap 3-2  (1) 2018.01.30
스프링 퀵 스타트 : chap 2-2  (406) 2018.01.27
스프링 퀵 스타트 : chap 2-1  (418) 2018.01.26
스프링 퀵 스타트 : chap 1-2  (392) 2018.01.23
Comments