質問編集履歴

2

HistoryDelete\.javaを追記いたしました。

2017/06/19 07:50

投稿

cayribaka
cayribaka

スコア19

test CHANGED
File without changes
test CHANGED
@@ -384,6 +384,130 @@
384
384
 
385
385
  }
386
386
 
387
+ ↓HistoryDelete.java
388
+
389
+ ``````Java
390
+
391
+ package servlet;
392
+
393
+
394
+
395
+ import java.awt.Frame;
396
+
397
+ import java.io.IOException;
398
+
399
+ import java.util.List;
400
+
401
+
402
+
403
+ import javax.servlet.ServletException;
404
+
405
+ import javax.servlet.annotation.WebServlet;
406
+
407
+ import javax.servlet.http.HttpServlet;
408
+
409
+ import javax.servlet.http.HttpServletRequest;
410
+
411
+ import javax.servlet.http.HttpServletResponse;
412
+
413
+ import javax.servlet.http.HttpSession;
414
+
415
+ import javax.swing.JOptionPane;
416
+
417
+
418
+
419
+ import model.HistoryDeleteLogic;
420
+
421
+
422
+
423
+ @WebServlet("/HistoryDelete")
424
+
425
+ public class HistoryDelete extends HttpServlet {
426
+
427
+ private static final long serialVersionUID = 1L;
428
+
429
+
430
+
431
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
432
+
433
+ HttpSession session = request.getSession(); // セッションを取得するための準備
434
+
435
+ int logId; // ログインIDを保存しておくための変数
436
+
437
+ StringBuffer urlBuffer = request.getRequestURL(); // 変数 urlBuffer に現在のページのURLを格納する(ただし、?以降のパラメータ情報は取得できない)
438
+
439
+ String url = urlBuffer.append("?").append(request.getQueryString()).toString(); // 先ほど取得したURL文字列の後ろに、現在のパラメータ情報をドッキングする
440
+
441
+ int indexOfParam = url.indexOf("="); // パラメータ付きURLかどうかを調べる(=マークがあるかどうか)
442
+
443
+ if(session.getAttribute("loginUser") != null && indexOfParam != -1) {
444
+
445
+ logId = Integer.parseInt(url.substring(indexOfParam + 1)); // =マークから先を切り取り、取得した文字列を変数 logId に格納する(int型へキャスト)
446
+
447
+ HistoryDeleteLogic hdl=new HistoryDeleteLogic();
448
+
449
+ List<Integer> list = hdl.act(logId);
450
+
451
+ int result;
452
+
453
+ Frame frame = new Frame();
454
+
455
+ Object[] options = { "OK", "CANCEL" };
456
+
457
+ frame.setAlwaysOnTop(true);
458
+
459
+ result = JOptionPane.showOptionDialog(frame, "選択した履歴を削除してよろしいですか?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
460
+
461
+ switch (result) {
462
+
463
+ case JOptionPane.OK_OPTION:
464
+
465
+ hdl.execute(logId);
466
+
467
+ Frame frame2 = new Frame();
468
+
469
+ Object[] options2 = { "OK" };
470
+
471
+ frame2.setAlwaysOnTop(true);
472
+
473
+ JOptionPane.showOptionDialog(frame2, "削除しました", "Information", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options2, options2[0]);
474
+
475
+ case JOptionPane.NO_OPTION:
476
+
477
+ case JOptionPane.CANCEL_OPTION:
478
+
479
+ default:
480
+
481
+ session.setAttribute("year", list.get(0));
482
+
483
+ session.setAttribute("month", list.get(1));
484
+
485
+ session.setAttribute("weightHistoryLength", null);
486
+
487
+ response.sendRedirect("/bodyMassIndex/History");
488
+
489
+ break;
490
+
491
+ }
492
+
493
+ }
494
+
495
+ else {
496
+
497
+ response.sendRedirect("/bodyMassIndex/");
498
+
499
+ }
500
+
501
+
502
+
503
+ }
504
+
505
+
506
+
507
+ }
508
+
509
+
510
+
387
511
  ```
388
512
 
389
513
 
@@ -392,6 +516,8 @@
392
516
 
393
517
 
394
518
 
519
+
520
+
395
521
  やりたいこととして、**データが一つしかない時に、削除ボタンを押して、削除後にリロードすると、データが何も表示されない状態で、表示させたいと考えております。**
396
522
 
397
523
 

1

servlet,jspを追記いたしました。見難いかとおもいます。すみません

2017/06/19 07:50

投稿

cayribaka
cayribaka

スコア19

test CHANGED
File without changes
test CHANGED
@@ -1,11 +1,3 @@
1
- たびたびお世話になっております。
2
-
3
- Java初心者です。
4
-
5
- 現在、体重管理アプリケーションなるものを自作しており、DBはpgAdmin4を使用しております。
6
-
7
-
8
-
9
1
  年月をweb上で指定すると、DBから該当の体重データを表示するというものです.
10
2
 
11
3
  問題が以下の画面のような場合において発生します。
@@ -22,20 +14,26 @@
22
14
 
23
15
  ・削除時のServlet(HistoryDelete.java)は以下のようにコーディングしております。
24
16
 
17
+
18
+
19
+
20
+
21
+ ↓History.java
22
+
25
- ```java
23
+ ``````Java
26
24
 
27
25
  package servlet;
28
26
 
29
27
 
30
28
 
31
- import java.awt.Frame;
32
-
33
29
  import java.io.IOException;
34
30
 
35
31
  import java.util.List;
36
32
 
37
33
 
38
34
 
35
+ import javax.servlet.RequestDispatcher;
36
+
39
37
  import javax.servlet.ServletException;
40
38
 
41
39
  import javax.servlet.annotation.WebServlet;
@@ -48,17 +46,19 @@
48
46
 
49
47
  import javax.servlet.http.HttpSession;
50
48
 
51
- import javax.swing.JOptionPane;
49
+
52
-
53
-
54
-
50
+
55
- import model.HistoryDeleteLogic;
51
+ import model.HistoryLogic;
52
+
56
-
53
+ import model.User;
54
+
57
-
55
+ import model.WeightHistory;
58
-
56
+
57
+
58
+
59
- @WebServlet("/HistoryDelete")
59
+ @WebServlet("/History")
60
-
60
+
61
- public class HistoryDelete extends HttpServlet {
61
+ public class History extends HttpServlet {
62
62
 
63
63
  private static final long serialVersionUID = 1L;
64
64
 
@@ -66,65 +66,45 @@
66
66
 
67
67
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68
68
 
69
- HttpSession session = request.getSession(); // セッションを取得するための準備
69
+ HttpSession session = request.getSession();
70
-
71
- int logId; // ログインIDを保存しておくための変数
70
+
72
-
73
- StringBuffer urlBuffer = request.getRequestURL(); // 変数 urlBuffer に現在のページのURLを格納する(ただし、?以降のパラメータ情報は取得できない)
74
-
75
- String url = urlBuffer.append("?").append(request.getQueryString()).toString(); // 先ほど取得したURL文字列の後ろに、現在のパラメータ情報をドッキングする
76
-
77
- int indexOfParam = url.indexOf("="); // パラメータ付きURLかどうかを調べる(=マークがあるかどうか)
78
-
79
- if(session.getAttribute("loginUser") != null && indexOfParam != -1) {
80
-
81
- logId = Integer.parseInt(url.substring(indexOfParam + 1)); // =マークから先を切り取り、取得した文字列を変数 logId に格納する(int型へキャスト)
82
-
83
- HistoryDeleteLogic hdl=new HistoryDeleteLogic();
84
-
85
- List<Integer> list = hdl.act(logId);
86
-
87
- int result;
88
-
89
- Frame frame = new Frame();
90
-
91
- Object[] options = { "OK", "CANCEL" };
92
-
93
- frame.setAlwaysOnTop(true);
94
-
95
- result = JOptionPane.showOptionDialog(frame, "選択した履歴を削除してよろしいですか?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
96
-
97
- switch (result) {
98
-
99
- case JOptionPane.OK_OPTION:
100
-
101
- hdl.execute(logId);
102
-
103
- Frame frame2 = new Frame();
104
-
105
- Object[] options2 = { "OK" };
106
-
107
- frame2.setAlwaysOnTop(true);
108
-
109
- JOptionPane.showOptionDialog(frame2, "削除しました", "Information", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options2, options2[0]);
110
-
111
- case JOptionPane.NO_OPTION:
112
-
113
- case JOptionPane.CANCEL_OPTION:
114
-
115
- default:
116
-
117
- session.setAttribute("year", list.get(0));
118
-
119
- session.setAttribute("month", list.get(1));
120
-
121
- session.setAttribute("weightHistoryLength", null);
71
+ session.setAttribute("weightHistoryLength", null);
72
+
122
-
73
+ User user = (User) session.getAttribute("loginUser");
74
+
75
+
76
+
77
+ if(user != null) {
78
+
79
+ if(session.getAttribute("year") != null) {
80
+
81
+ if(!session.getAttribute("year").equals("")) {
82
+
83
+ String year = session.getAttribute("year").toString();
84
+
85
+ String month = session.getAttribute("month").toString();
86
+
87
+ HistoryLogic historyLogic = new HistoryLogic();
88
+
89
+ List<WeightHistory> weightHistoryList = historyLogic.execute(user.getUserId(), Integer.parseInt(year), Integer.parseInt(month));
90
+
91
+ if(!weightHistoryList.isEmpty()) {
92
+
123
- response.sendRedirect("/bodyMassIndex/History");
93
+ session.setAttribute("historiesLength",weightHistoryList);
124
-
125
- break;
94
+
126
-
127
- }
95
+ }
96
+
97
+ session.setAttribute("year", "");
98
+
99
+ session.setAttribute("month", "");
100
+
101
+ }
102
+
103
+ }
104
+
105
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/history.jsp");
106
+
107
+ requestDispatcher.forward(request, response);
128
108
 
129
109
  }
130
110
 
@@ -134,80 +114,284 @@
134
114
 
135
115
  }
136
116
 
137
-
138
-
139
117
  }
140
118
 
141
119
 
142
120
 
121
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
122
+
123
+ // リクエストパラメータの取得
124
+
125
+ request.setCharacterEncoding("utf-8");
126
+
127
+ String year = request.getParameter("year");
128
+
129
+ String month = request.getParameter("month");
130
+
131
+ HttpSession session = request.getSession();
132
+
133
+ session.setAttribute("weightHistoryLength", null);
134
+
135
+ User user = (User) session.getAttribute("loginUser");
136
+
137
+
138
+
139
+ try {
140
+
141
+ if(!(year.equals("-1") || month.equals("-1"))) {
142
+
143
+ HistoryLogic historyLogic = new HistoryLogic();
144
+
145
+ List<WeightHistory> weightHistoryList = historyLogic.execute(user.getUserId(), Integer.parseInt(year), Integer.parseInt(month));
146
+
147
+
148
+
149
+ if(!weightHistoryList.isEmpty()) {
150
+
151
+ session.setAttribute("historiesLength",weightHistoryList);
152
+
153
+ }
154
+
155
+ else {
156
+
157
+ request.setAttribute("errorMessage", "対象の年月にデータが入力されていません。");
158
+
159
+ }
160
+
161
+ }
162
+
163
+ else if(year.equals("-1")) {
164
+
165
+ request.setAttribute("errorMessage", "年が選択されていません。");
166
+
167
+ }
168
+
169
+ else if(month.equals("-1")) {
170
+
171
+ request.setAttribute("errorMessage", "月が選択されていません。");
172
+
173
+ }
174
+
175
+ else {
176
+
177
+ request.setAttribute("errorMessage", "なぜかダメです。");
178
+
179
+ }
180
+
181
+ RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/history.jsp");
182
+
183
+ requestDispatcher.forward(request, response);
184
+
185
+ }
186
+
187
+ catch(Exception e) {
188
+
189
+ e.printStackTrace();
190
+
191
+ request.setAttribute("errorMessage", "進捗、ダメです。");
192
+
193
+ }
194
+
195
+ }
196
+
143
197
  }
144
198
 
145
199
 
146
200
 
147
201
  ```
148
202
 
149
- ・削除時のLogic(HistoryDeleteLogic.java)は以下のようにコーディングしております。
150
-
151
- ```java
203
+
204
+
205
+ ↓history.jsp
206
+
207
+ ```jsp
208
+
209
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
210
+
211
+ pageEncoding="UTF-8"%>
212
+
213
+ <%@ page import="java.util.List" %>
214
+
215
+ <%@ page import="model.WeightHistory" %>
216
+
217
+ <!DOCTYPE html">
218
+
219
+ <html>
220
+
221
+ <head>
222
+
223
+ <link rel="stylesheet" type="text/css" href="css/menu.css"/>
224
+
225
+ <link rel="stylesheet" type="text/css" href="css/history.css?20170619"/>
226
+
227
+ <meta charset="UTF-8">
228
+
229
+
230
+
231
+ <title>体重管理アプリケーション</title>
232
+
233
+ <script src="js/lib/jquery-3.2.1.min.js"></script>
234
+
235
+ <script src="js/menu.js"></script>
236
+
237
+ </head>
238
+
239
+ <body onload="selectColorChange(2);">
240
+
241
+ <jsp:include page="menu.jsp"/>
242
+
243
+ <div id="div1">
244
+
245
+ <div id="div2">
246
+
247
+ <form action="/bodyMassIndex/History" method="post">
248
+
249
+ <div id="errorMessage"><p><%= request.getAttribute("errorMessage") != null ? request.getAttribute("errorMessage") : " " %></p></div>
250
+
251
+
252
+
253
+ <!-- 年のセレクトボックスの作成 -->
254
+
255
+ <select name="year"id="box1">
256
+
257
+ <option value="-1">年</option>
258
+
259
+ <option value="2017">2017</option>
260
+
261
+ <option value="2018">2018</option>
262
+
263
+ <option value="2019">2019</option>
264
+
265
+ <option value="2020">2020</option>
266
+
267
+ </select>
268
+
269
+
270
+
271
+ <!-- 月のセレクトボックスの作成 -->
272
+
273
+ <select name="month"id="box2" style="margin-left:50px">
274
+
275
+ <option value="-1">月</option>
276
+
277
+ <option value="1">1月</option>
278
+
279
+ <option value="2">2月</option>
280
+
281
+ <option value="3">3月</option>
282
+
283
+ <option value="4">4月</option>
284
+
285
+ <option value="5">5月</option>
286
+
287
+ <option value="6">6月</option>
288
+
289
+ <option value="7">7月</option>
290
+
291
+ <option value="8">8月</option>
292
+
293
+ <option value="9">9月</option>
294
+
295
+ <option value="10">10月</option>
296
+
297
+ <option value="11">11月</option>
298
+
299
+ <option value="12">12月</option>
300
+
301
+ </select>
302
+
303
+
304
+
305
+ <!-- ボタンの作成 -->
306
+
307
+
308
+
309
+ <input type="submit" value="表示する" id="button2">
310
+
311
+ </form>
312
+
313
+ <a href="/bodyMassIndex/Record"><input type="button" value="記録する" id="button1"/></a>
314
+
315
+ </div>
316
+
317
+
318
+
319
+ <div id="div3">
320
+
321
+ <h3 id="text1"><pre> 日付 体重 </pre></h3>
322
+
323
+ <% List<WeightHistory> historyList = (List<WeightHistory>)session.getAttribute("historiesLength");
324
+
325
+ if (historyList != null) {
326
+
327
+ for(int i = 0; i < historyList.size(); i++) { %>
328
+
329
+ <span id="line">
330
+
331
+ <pre><%= (historyList.get(i).getTargetDate().getYear() + 1900) + "/" + (historyList.get(i).getTargetDate().getMonth() + 1) + "/" + historyList.get(i).getTargetDate().getDate() %> <%=(int) historyList.get(i).getWeight() %><span id="kgwasshoi">kg</span> <a href="Record?logId=<%= historyList.get(i).getLogId() %>" id="tag1">編集</a> <a href="HistoryDelete?logId=<%= historyList.get(i).getLogId() %>" id="tag2">削除</a></pre>
332
+
333
+ </span>
334
+
335
+ <% }
336
+
337
+ } %>
338
+
339
+ </div>
340
+
341
+ </div>
342
+
343
+ </body>
344
+
345
+ </html>
346
+
347
+ ```
348
+
349
+ ↓HistoryLogic.java
350
+
351
+ ```Java
152
352
 
153
353
  package model;
154
354
 
155
-
156
-
157
- import java.util.ArrayList;
158
-
159
- import java.util.Calendar;
355
+ import java.sql.Date;
160
356
 
161
357
  import java.util.List;
162
358
 
163
-
164
-
165
359
  import dao.WeightHistoryDAO;
166
360
 
167
361
 
168
362
 
169
- public class HistoryDeleteLogic {
363
+ public class HistoryLogic {
364
+
365
+
366
+
170
-
367
+ public List<WeightHistory> execute(int userId, int targetYear, int targetMonth) {
368
+
171
-
369
+ WeightHistoryDAO dao = new WeightHistoryDAO();
370
+
172
-
371
+ return dao.weightHistoryFindAll(userId, targetYear, targetMonth);
372
+
373
+ }
374
+
375
+
376
+
173
- public boolean execute(int logId){
377
+ public WeightHistory execute(int userId,Date targetDate){
174
378
 
175
379
  WeightHistoryDAO dao=new WeightHistoryDAO();
176
380
 
177
- return dao.delete(logId);
381
+ return dao.weightHistoryFind(userId, targetDate);
178
382
 
179
383
  }
180
384
 
181
-
182
-
183
- public List<Integer> act(int logId) {
184
-
185
- WeightHistoryDAO dao = new WeightHistoryDAO();
186
-
187
- WeightHistory weightHistory = dao.weightHistoryFind(logId);
188
-
189
- java.util.Date date = new java.util.Date(weightHistory.getTargetDate().getTime());
190
-
191
- List<Integer> list = new ArrayList<Integer>();
192
-
193
- Calendar cal = Calendar.getInstance();
194
-
195
- cal.setTime(date);
196
-
197
- list.add(cal.get(Calendar.YEAR));
198
-
199
- list.add(cal.get(Calendar.MONTH) + 1);
200
-
201
- return list;
202
-
203
- }
204
-
205
385
  }
206
386
 
207
-
208
-
209
387
  ```
210
388
 
389
+
390
+
391
+
392
+
393
+
394
+
211
395
  やりたいこととして、**データが一つしかない時に、削除ボタンを押して、削除後にリロードすると、データが何も表示されない状態で、表示させたいと考えております。**
212
396
 
213
397