Spring

book.jsp

Birthmark 2019. 2. 24. 17:32
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
<%@ page language="java" 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
.mainwrapp {
    width: 900px;
    margin: 0 auto;
    text-align: center;
}
table {
    width: 800px;
    margin: 0 auto;
    border-collapse: collapse;
}
td {
    border-bottom: 1px solid black;
    border-top: 1px solid black;
}
.seq {
    width: 50px;
}
.name {
    width: 100px;
}
.regdate {
    width: 200px;
}
.review {
    width: 400px;
    text-align: left;
}
</style>
<script src="resources/jquery-3.1.1.min.js"></script>
<script>
    $(function() {
        $("#regist").on('click', regist);
        init();
        
        $("#name").on('keyup',function(){
            
            var id = $("#name").val();
            $.ajax({
                url : "checkID",
                type : "post",
                data : {name : id},
                success : function (data) {
                    
                    if(data == 1){
                        $("#msg").text("이미 등록된 이름입니다.");
                        $("#msg").css("color","red");
                    }
                    else if(data == 0){
                        $("#msg").text("사용 가능한 이름입니다.");
                    }
                }
                
            });
        
        });
 
    });
 
    //모든 데이터 읽어옴
    function init() {
        // Code Here
 
 
        $(document).ready(function() {
 
            $.ajax({
                // Code Here
            
                url : "reviewSelect",
                type : "post",
                success : function(data) {
 
                    output(data);
 
                }
 
            });
 
        });
 
    }
 
    // 서평 등록 
 
    function regist() {
        var name = $("#name").val();
        var review = $("#review").val();
 
        if (name.length == 0 || review.length == 0) {
            alert("데이터를 입력해 주세요");
            return;
        }
 
        var reviewData = {
            'name' : name,
            'review' : review
        }
 
        $.ajax({
            // Code Here
            data : reviewData,
            url : "insertReview",
            type : "post",
            success : function(data) {
                if (data == 1) {
                    $("#name").val("");
                    $("#review").val("");
                    alert("등록완료");
 
                } else {
                    $("#name").val("");
                    $("#review").val("");
                    alert("등록실패");
 
                }
                init();
            }
        });
 
    }
 
    function output(resp) {
        var data = '<table>';
 
        $
                .each(
                        resp,
                        function(index, item) {
                            data += '<tr class="reviewtr" data-sno="'+ item.seq +'" >';
                            data += '    <td class="seq">' + item.seq + '</td>';
                            data += '    <td class="name">' + item.name + '</td>';
                            data += '    <td class="regdate">' + item.regdate
                                    + '</td>';
                            data += '    <td class="review">' + item.review
                                    + '</td>';
                            data += '    <td><input type="button" onclick="javascript:del(this.id)" class="delbtn" id="'
                                    + item.seq + '"value="삭제" /></td>';
                            data += '</tr>';
 
                        });
        // Code Here
        data += '</table>';
        $("#reviewDiv").html(data);
 
    }
 
    // 서평 삭제
    function del(num) {
        var seq = { 'seq' : num} 
        $.ajax({
            // Code Here
            data : seq ,
            url : "deleteReview",
            type : "post",
            success : function name(data) {
                if (data == 1) {
 
                    alert("삭제완료");
 
                } else {
 
                    alert("삭제실패");
 
                }
                init();
            }
 
        });
 
    }
</script>
</head>
<body>
    <div class="mainwrapp">
        <h2>[ 오늘의 책 ]</h2>
        <p>"오늘의 책"의 서평을 등록해 주세요.</p>
 
        <img src="resources/book.jpg" />
        <p>설민석의 조선왕조실록 : 설민석 (지은이) | 최준석 (그림) | 세계사 | 2016-07-25</p>
        <form>
            이름 : <input type="text" id="name" name="name" placeholder="이름" />&nbsp;
            <p id="msg"></p>
            서평 : <input type="text" id="review" name="review"
                style="width: 400px;" placeholder="서평" /
                <input type="button" id="regist" value="등록" />
        </form>
        <div id="reviewDiv"></div>
        
 
    </div>
 
 
</body>
</html>
cs