JSP ๊ฐ๋ฐ๋ฐฉ๋ฒ๋ก Model1์ผ๋ก ๋ง๋ ๊ฒ์ํ์ JSP ๊ฐ๋ฐ๋ฐฉ๋ฒ๋ก Model2๋ก ๋ณํํ๊ธฐ
๊ฐ๋ฐ๋ฐฉ๋ฒ๋ก Model2๋ Model1๊ณผ ๋ค๋ฅด๊ฒ ์๋ธ๋ฆฟ์ ๊ฐ์ง๊ณ ์๋ค. ๊ทผ๋ฐ ์ด๋ ์ด ์๋ธ๋ฆฟ์์ ๋ก์ง์ ์ฒ๋ฆฌํด์ฃผ๋ ๊ฒ ์๋๋ผ ์์ฒญ์ ๋ธ๋ผ์ฐ์ ์์ ์ฒ์ ๋ฐ์ ๊ทธ ์์ฒญ์ ์๋ฐ ์๋น์ค ํน์ ์ปจํธ๋กค๋ฌ ๋ ์ด์ด๋ก ๋ณด๋ด์ค๋ค. ๊ทธ๋ฆฌ๊ณ JSP๋ ๋ทฐ๋ง ์ญํ ์ ํด์ฃผ๋ JSP๋ง์ ์ฐ๊ฒ ๋๊ณ ๋ก์ง์ด ์๋ JSP๋ ๋ชจ๋ ์๋น์ค ํด๋์ค์ ๋ก์ง์ผ๋ก ๋ฃ๋๋ค.
( JSP ๊ฐ๋ฐ๋ฐฉ๋ฒ๋ก Model1์ผ๋ก ๋ง๋ ๊ฒ์ํ์ (22.11.23)๊ฒ์๊ธ ์ฐธ๊ณ )
M : Board.java
V : boardAddForm.jsp, boardList.jsp, boardEdit.jsp, boardRead.jsp
C : BoardServlet.java
BoardService.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import org.json.simple.JSONObject;
import com.ezen.web.hello.Board;
public class BoardService
{
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;
private String viewPath = "/WEB-INF/jsp/model2";
private String fpath = "D:/test/boardList.ser";
public BoardService(){}
public BoardService(HttpServletRequest request,
HttpServletResponse response)
{
this.request = request;
this.response = response;
this.session = request.getSession();
}
public String exec()
{
String cmd = request.getParameter("cmd");
if(cmd==null || cmd.equals("list"))
{
List<Board> list = getList();
request.setAttribute("list", list);
return viewPath+"/boardList.jsp";
}
else if(cmd.equals("addform"))
{
return viewPath+"/boardAddForm.jsp";
}
else if(cmd.equals("add"))
{
Board board = createBoardFromParam();
boolean added = add(board);
Map<String, Object> map = new HashMap<>();
map.put("added", added);
sendJSONStr(map);
}
else if(cmd.equals("read"))
{
Board board = createBoardFromParam();
board = read(board);
request.setAttribute("board", board);
String uid = (String)session.getAttribute("uid");
String author = board.getAuthor();
boolean owned = uid.equals(author);
request.setAttribute("owned", owned);
return viewPath+"/boardRead.jsp";
}
else if(cmd.equals("edit"))
{
Board board = createBoardFromParam();
board = find(board);
request.setAttribute("b", board);
return viewPath+"/boardEdit.jsp";
}
else if(cmd.equals("update"))
{
Board board = createBoardFromParam();
boolean updated = update(board);
Map<String, Object> map = new HashMap<>();
map.put("updated", updated);
sendJSONStr(map);
}
else if(cmd.equals("delete"))
{
Board board = createBoardFromParam();
boolean deleted = delete(board);
Map<String, Object> map = new HashMap<>();
map.put("deleted", deleted);
sendJSONStr(map);
}
else if(cmd.equals("find"))
{
Board board = createBoardFromParam();
board = find(board);
request.setAttribute("board", board);
return viewPath+"/boardRead.jsp";
}
return null;
}
private Board createBoardFromParam()
{
String sNum = request.getParameter("num");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
Board board = new Board();
if(sNum!=null) board.setNum( Integer.valueOf(sNum));
board.setTitle(title);
board.setContents(contents);
return board;
}
private void sendJSONStr(Map<String, Object> map)
{
JSONObject jsObj = new JSONObject(map);
String jsStr = jsObj.toJSONString();
try {
PrintWriter out = response.getWriter();
out.print(jsStr);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean add(Board board)
{
List<Board> list = deserialize();
int num = 1;
if(list.size()>0) {
Board last = list.get(list.size()-1);
num = last.getNum() + 1;
}
board.setNum(num);
board.setAuthor((String) session.getAttribute("uid"));
java.util.Date today = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(today.getTime());
board.setRegDate(sqlDate);
boolean added = list.add(board);
return added && serialize(list);
}
public List<Board> getList()
{
return deserialize();
}
public Board find(Board key)
{
List<Board> list = deserialize();
if(list.contains(key)) {
return list.get(list.indexOf(key));
}
return null;
}
public Board read(Board key)
{
String cmd = request.getParameter("cmd");
Board found = find(key);
if(cmd.equals("find")) {
return found;
}
found.setHit(found.getHit()+1);
List<Board> list = deserialize();
list.get(list.indexOf(found)).setHit(found.getHit());
serialize(list);
return found;
}
public boolean update(Board key)
{
List<Board> list = deserialize();
if(list.contains(key)) {
Board found = list.get(list.indexOf(key));
found.setTitle(key.getTitle());
found.setContents(key.getContents());
return serialize(list);
}
return false;
}
public boolean delete(Board key)
{
List<Board> list = deserialize();
if(list.contains(key)) {
boolean deleted = list.remove(key);
return deleted && serialize(list);
}
return false;
}
private boolean serialize(List<Board> list)
{
File f = new File(fpath);
try {
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(f));
oout.writeObject(list);
oout.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private List<Board> deserialize()
{
File f = new File(fpath);
List<Board> list = null;
if(!f.exists()) {
list = new ArrayList<Board>();
}else {
try {
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(f));
list = (List<Board>)oin.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpSession getSession() {
return session;
}
public void setSession(HttpSession session) {
this.session = session;
}
}
|
cs |
26~92ํ : ์ปจํธ๋กค๋ฌ์ ํด๋นํ๋ ๋ก์ง์ผ๋ก ์๋ธ๋ฆฟ์ ์ฝ๋ฉํ์ง ์๊ณ ์๋น์ค ํด๋์ค์ ์ฝ๋ฉํ์ฌ ์๋ธ๋ฆฟ์ ์ญํ ์ ๋จ์ํ ํ์๋ค.
94~106 ํ : ์์ฒญ์์ ๋์ด์ค๋ num, title,contents๋ฅผ board ๊ฐ์ฒด์ ๋ด๊ณ ๊ทธ board ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํด์ค๋ค. ์ฐธ์กฐ ๋ณ์๋ null๋ก์ ์ ์ฅ์ด ๊ฐ๋ฅํ์ง๋ง Integer.valueOf(sNum)๋ ๊ธฐ๋ณธ ๋ณ์์ธ int ๋ณ์๊ฐ ๋์ค๊ณ int ๋ณ์๋ null๊ฐ์ ์ ์ฅ ๋ชปํ๊ธฐ์ if๋ฌธ์ ์จ์ ํํํ๋ค.
108~119 ํ : sendJSONStr() ๋ฉ์๋๋ ๊ธฐ์กด์ ์๋ ๋ก์ง์ ๋ด๋นํ๋ JSP์ ๋ก์ง์ ์ฎ๊ฒจ ๋์๋ค. ์์ฃผ ์ฐ์ด๋ ๋ก์ง์ด๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ๋ฉ์๋๋ก ๋นผ๋จ๋ค. ์ฌ๊ธฐ์ ๋์จ json๋ฌธ์์ด์ VIEW๋ฅผ ๋ด๋นํ๋ JSP์ ํจ์์์ success์ function์ ์๋ต์ผ๋ก ๋ค์ด ๊ฐ๊ฒ ๋๋ค.
boardAddForm.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
|
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ์ถ๊ฐ ํผ</title>
<style type="text/css">
main { width:fit-content; margin:1em auto;}
h3 { text-align: center;}
div.btn { width:fit-content; margin:0.5em auto; }
form { border:1px solid black; padding:1em; width:fit-content;}
form>div {margin:1em;}
label {display:inline-block; width:3em; padding-right:1em; text-align:right;}
div.contents { position: relative; }
label[for=contents] {position:relative; top:-2em;}
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function addBoard()
{
var obj = $('#addForm').serialize();
$.ajax({
url : 'board',
method: 'post',
data: obj,
cache : false,
dataType:'json',
success:function(res){
alert(res.added ? '์ถ๊ฐ ์ฑ๊ณต' : '์ถ๊ฐ ์คํจ');
location.href="board?cmd=list";
},
error : function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
</head>
<body>
<main>
<h3>๊ฒ์๊ธ ์
๋ ฅ</h3>
<form id="addForm" action="board" method="post" onsubmit="return addBoard();">
<input type="hidden" name="cmd" value="add">
<div>
<label for="title">์ ๋ชฉ</label>
<input id="title" type="text" name="title" value="๊ฒ์ํ ํ
์คํธ">
</div>
<div class="contents">
<label for="contents">๋ด์ฉ</label>
<textarea id="contents" name="contents" rows="5" cols="50"></textarea>
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">์ ์ฅ</button>
</div>
</form>
</main>
</body>
</html>
|
cs |
boardList.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
|
<%@page import="com.ezen.web.hello.*"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ๋ชฉ๋ก</title>
<style type="text/css">
main { width:fit-content; margin:1em auto; }
main h3{ text-align: center; }
table { border:1px solid black; border-spacing:0; border-collapse: collapse;
padding:0.5em;
}
td,th { border-bottom:1px dashed black; padding:0.3em 1em;
border-right: 1px solid black;
}
th { background-color:rgb(210,210, 255);}
a {text-decoration: none;}
div.links { width:fit-content; margin:1em auto; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
</head>
<body>
<main>
<%@ include file="/WEB-INF/jsp/user_inc.jsp" %>
<h3>๊ฒ์๊ธ ๋ชฉ๋ก</h3>
<table>
<tr><th>๋ฒํธ</th><th>์ ๋ชฉ</th><th>์์ฑ์</th><th>์์ฑ์ผ</th><th>ํํธ์</th></tr>
<c:forEach var="b" items="${list}">
<tr>
<td>${b.num}</td>
<td>
<a href="board?cmd=read&num=${b.num}">
${b.title}</a>
</td>
<td>${b.author}</td>
<td>${b.regDate}</td>
<td>${b.hit}</td>
</tr>
</c:forEach>
</table>
<div class="links">
[<a href="board?cmd=addform">๊ธ์ฐ๊ธฐ</a>]
</div>
</main>
</body>
</html>
|
cs |
boardRead.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
68
69
70
71
|
<%@page import="com.ezen.web.hello.Board"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ์ฝ๊ธฐ</title>
<style type="text/css">
main{width:fit-content; margin:1em auto; }
main h3{text-align: center;}
label { display:inline-block; padding:0.2em 1em; border-bottom:1px solid black;
text-align: right; width:3em; margin-right:1em; background-color:rgb(200,255,255);
}
.container {width:fit-content; padding:1em; border:1px solid black;}
a { text-decoration: none;}
.links{margin:1em auto; width:fit-content; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
$(function(){
var owned = ${owned};
if(!owned){
//$('#owner_only').css('visibility', 'hidden');
$('#owner_only').css('display', 'none');
}
});
</script>
<script type="text/javascript">
function deleteBoard(num)
{
if(!confirm('์ ๋ง๋ก ํ์ฌ ๊ธ์ ์ญ์ ํ์๊ฒ ์ด์?')) return;
$.ajax({
url:'board',
method:'post',
cache:false,
data:{"cmd":"delete", "num":num},
dataType:'json',
success:function(res){
alert(res.deleted ? '์ญ์ ์ฑ๊ณต':'์ญ์ ์คํจ');
location.href='board?cmd=list';
},
error : function(xhr, status, err){
alert('์๋ฌ:' + err);
}
});
}
</script>
</head>
<body>
<main>
<h3>๊ฒ์๊ธ ์ฝ๊ธฐ</h3>
<div class="container">
<div><label>๊ธ๋ฒํธ</label>${board.num}</div>
<div><label>์ ๋ชฉ</label>${board.title}</div>
<div><label>์์ฑ์</label>${board.author}</div>
<div><label>์์ฑ์ผ</label>${board.regDate}</div>
<div><label>ํํธ์</label>${board.hit}</div>
<div><label>๋ด์ฉ</label>${board.contents}</div>
</div>
<div class="links">
<span id="owner_only">
[<a href="board?cmd=edit&num=${board.num}">์์ </a>]
[<a href="javascript:deleteBoard(${board.num});">์ญ์ </a>]
</span>
[<a href="board?cmd=list">๋ชฉ๋ก</a>]
</div>
</main>
</body>
</html>
|
cs |
boardEdit.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<%@ page import="com.ezen.web.hello.Board"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ์์ ํผ</title>
<style type="text/css">
main { width:fit-content; margin:1em auto;}
main h3{text-align: center;}
table { border:1px solid black; padding:1em; border-spacing: 0; border-collapse: collapse;
width:630px;
}
th { background-color:rgb(200,255,255); border-right:1px solid black;}
th, td {border-bottom:1px solid black; padding:0.4em;}
td#title { width:400px; }
input {width:400px;}
textarea {width:520px; height:100px;}
div.btn { margin:1em auto; width:fit-content;}
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function updateBoard()
{
var obj = $('#editForm').serialize();
if(!confirm('ํ์ฌ์ ๋ด์ฉ์ผ๋ก ์์ ํ์๊ฒ ์ด์?')) return;
$.ajax({
url : 'board',
method:'post',
cache:false,
data:obj,
dataType:'json',
success:function(res){
alert(res.updated ? '์์ ์ฑ๊ณต':'์์ ์คํจ');
if(res.updated)
location.href='board?cmd=find&num=${b.num}';
},
error:function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
return false;
}
function deleteBoard(num)
{
if(!confirm('ํ์ฌ ๋ด์ฉ์ ์ญ์ ํ์๊ฒ ์ด์?')) return;
$.ajax({
url : 'board',
method:'post',
cache:false,
data:{"cmd":"delete", "num":num },
dataType:'json',
success:function(res){
alert(res.deleted ? '์ญ์ ์ฑ๊ณต':'์ญ์ ์คํจ');
if(res.deleted)
location.href='board';
},
error:function(xhr,status,err){
alert('์๋ฌ:' + err);
}
});
}
</script>
</head>
<body>
<main>
<h3>๊ฒ์๊ธ ์์ </h3>
<div class="container">
<form id="editForm" onsubmit="return updateBoard();">
<input type="hidden" name="cmd" value="update">
<input type="hidden" name="num" value="${b.num}">
<table>
<tr><th>๊ธ๋ฒํธ</th><td id="num">${b.num}</td><th>์ ๋ชฉ</th><td id="title" colspan="3">
<input type="text" id="title" name="title" value="${b.title}">
</td></tr>
<tr><th>์์ฑ์</th><td>${b.author}</td><th>์์ฑ์ผ</th><td>${b.regDate}</td><th>Hit</th><td>${b.hit}</td></tr>
<tr><th>๋ด์ฉ</th><td colspan="5">
<textarea id="contents" name="contents">${b.contents}</textarea>
</td></tr>
</table>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">์์ </button>
<button type="button" onclick="deleteBoard(${b.num});">์ญ์ </button>
</div>
</form>
</div>
</main>
</body>
</html>
|
cs |
JSP์ ์๋ ์คํฌ๋ฆฝํธ ๋ฆฟ์ ์จ์ ํํํ ์๋ฐ ์ฝ๋๋ค๋ JSTL๊ณผ EL์ ์จ์ ๊ฐ๋จํ๊ฒ ํํํ์๋ค.
BoardServlet.java
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
|
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/board")
public class BoardServlet extends HttpServlet
{
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String uid = (String)session.getAttribute("uid");
if(uid==null) {
session.setAttribute("url", request.getRequestURL().toString());
try {
response.sendRedirect("login/userController.jsp?cmd=loginform");
} catch (IOException e) {
e.printStackTrace();
}
return;
}
String viewPath = new BoardService(request, response).exec();
if(viewPath!=null) {
getServletContext().getRequestDispatcher(viewPath).forward(request, response);
}
}
}
|
cs |
๋๊ธ