한동안 프로젝트가 많아서 포스팅을 못했내요
아직 스프링 프레임워크를 다루는데 서툴러서
한번더 스프링을 복습하고 있습니다
Model 1
2000년대 초까지 자바 기반 웹 어플리케이션 개발에 사용된 아키텍처 입니다
Model 1 아키텍처는 위의 구조와 같이 JSP 파일이 뷰+컨트롤러를 동시에 구성하고
모델은 JavaBean 이 구성하는 형태를 가지고 있습니다
하나의 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
64
65
66
67
|
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// 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. 응담 화면 구성
%>
<!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>
<a href="logout_proc.jsp">Log-out</a>
<hr>
<form action="updateBoard_proc.jsp" method="post">
<input name="seq" type="hidden" value="<%= board.getSeq() %>"/>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left"><input name="title" type="text" value="<%= board.getTitle() %>"/></td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left"><%= board.getWriter() %></td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left"><textarea rows="10" cols="40"><%= board.getContent() %></textarea></td>
</tr>
<tr>
<td bgcolor="orange">등록일</td>
<td align="left"><%= board.getRegDate() %></td>
</tr>
<tr>
<td bgcolor="orange">조회수</td>
<td align="left"><%= board.getCnt() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="글 수정"/>
</td>
</tr>
</table>
</form>
<hr>
<a href="insertBoard.jsp">글등록</a>
<a href="deleteBoard_proc.jsp?seq=<%= board.getSeq() %>">글삭제</a>
<a href="getBoardList.jsp">글목록</a>
</center>
</body>
</html>
|
위의 코드는 제가 스프링을 공부하며 예제를 따라서 구현한 Model 1 아키텍처의 일부입니다
JSP 파일 내부에 HTML과 JAVA가 함께 존재하고 있습니다
흔히 보는 PHP 파일의 구성과 비슷하여서 저는 이쪽이 익숙합니다
Model 2
Model 1 아키택처 이후로 등장해서 Controller 라는 개념이 도입되어
JSP 파일에서 JAVA 소스코드를 분리해낸 아키택처 입니다
이처럼 클라이언트의 요청을 Contoroller 가 받아서 DB 에서 데이터를 꺼내와
View 에 전해주는 방식입니다
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
|
<%@ page import="com.springbook.biz.board.impl.BoardDAO" %>
<%@ page import="com.springbook.biz.board.BoardVO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
BoardVO board = (BoardVO) session.getAttribute("board");
%>
<!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>
<a href="logout.do">Log-out</a>
<hr>
<form action="updateBoard.do" method="post">
<input name="seq" type="hidden" value="<%= board.getSeq() %>"/>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left"><input name="title" type="text" value="<%= board.getTitle() %>"/></td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left"><%= board.getWriter() %></td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left"><textarea rows="10" cols="40"><%= board.getContent() %></textarea></td>
</tr>
<tr>
<td bgcolor="orange">등록일</td>
<td align="left"><%= board.getRegDate() %></td>
</tr>
<tr>
<td bgcolor="orange">조회수</td>
<td align="left"><%= board.getCnt() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="글 수정"/>
</td>
</tr>
</table>
</form>
<hr>
<a href="insertBoard.jsp">글등록</a>
<a href="deleteBoard.do?seq=<%= board.getSeq() %>">글삭제</a>
<a href="getBoardList.do">글목록</a>
</center>
</body>
</html>
|
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
|
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
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("UTF-8");
process(request, response);
}
private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 클라이언트의 요청 path 정보를 추출한다.
String uri = request.getRequestURI();
String path = uri.substring(uri.lastIndexOf("/"));
System.out.println(path);
// 2. 클라이언트의 요청 path에 다라 적절히 분기처리 한다.
if (path.equals("/login.do")) {
~
} else if (path.equals("/logout.do")) {
~
} else if (path.equals("/insertBoard.do")) {
~
} else if (path.equals("/updateBoard.do")) {
~
} else if (path.equals("/deleteBoard.do")) {
~
} else if (path.equals("/getBoard.do")) {
// 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")) {
~
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.springbook.view.controller.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
위에서부터 차래로 View, Controller, Xml 입니다
먼저 Controller 를 생성한뒤 Xml 에 위와같이 등록해주면 *.do 의 URI 를 Controller 에 맵핑해 줍니다
그 후, 컨트롤어에서 DB 컨트롤을 해주며 만들어진 정보를 View 에 전송하고,
View 에서는 데이터를 사용하기만 하면 됩니다
이로 인해서 각자의 역할에 충실하게 코드가 간결해 지므로써
유지보수가 한층 수월하게 되었습니다
이 후, 컨트롤러를 모듈화하여 세분화 되어 나타난 형태가 MVC 패턴입니다
이 MVC 패턴으로 구성하여 Spring 프레임워크가 만들어 졌습니다
실제로는 어노테이션이라는 기법을 사용하여 코드를 좀 더 간결하고 깔끔하게 만들어서 사용합니다
위의 소스코드는 Model 1, Model 2를 이해하고자 소개해주신 코드 입니다
'Web Programming > Spring' 카테고리의 다른 글
의존성 관리 (0) | 2019.07.06 |
---|---|
<bean> 엘리먼트 속성 (0) | 2019.07.06 |
Spring 프레임워크 기본구조 (0) | 2019.06.22 |