質問編集履歴

1

複数ファイルにつき記述が縦長になってます。

2022/11/28 05:40

投稿

Yudon
Yudon

スコア0

test CHANGED
File without changes
test CHANGED
@@ -14,15 +14,59 @@
14
14
 
15
15
  ### 発生している問題・エラーメッセージ
16
16
 
17
- ```
18
17
  エラーメッセージはありません。
19
18
  元のデータを更新したいが、実際には新しくデータが追加され、元あったデータが残ってしまいます。
20
19
  以下、原因の部分がありそうなクラスファイルのソースを貼りました。
21
- ```
22
20
 
23
21
  ### 該当のソースコード
24
22
 
25
23
  ```java
24
+ package com.example.quiz.entity;
25
+
26
+ import org.springframework.data.annotation.Id;
27
+
28
+ import lombok.AllArgsConstructor;
29
+ import lombok.Data;
30
+ import lombok.NoArgsConstructor;
31
+
32
+ /** quizテーブル用:Entity */
33
+ @Data
34
+ @NoArgsConstructor
35
+ @AllArgsConstructor
36
+ public class Quiz {
37
+ /** 識別ID */
38
+ @Id
39
+ private Integer id;
40
+ /** クイズの内容 */
41
+ private String question;
42
+ /** クイズの解答 */
43
+ private Boolean answer;
44
+ /** 作成者 */
45
+ private String author;
46
+ }
47
+ package com.example.quiz.service;
48
+
49
+ import java.util.Optional;
50
+
51
+ import com.example.quiz.entity.Quiz;
52
+
53
+ public interface QuizService {
54
+ /** クイズ情報を全件取得する */
55
+ Iterable<Quiz> selectAll();
56
+ /** クイズ情報を、idをキーに1件取得します */
57
+ Optional<Quiz> selectOneById(Integer id);
58
+ /** クイズ情報をランダムに取得します */
59
+ Optional<Quiz> selectOneRandomQuiz();
60
+ /** クイズの正解/不正解を判定します */
61
+ Boolean checkQuiz(Integer id,Boolean myAnswer);
62
+ /** クイズを登録します */
63
+ void insertQuiz(Quiz quiz);
64
+ /** クイズ更新します */
65
+ void updateQuiz(Quiz quiz);
66
+ /** クイズを削除します */
67
+ void deleteQuizById(Integer id);
68
+ }
69
+
26
70
  package com.example.quiz.controller;
27
71
 
28
72
  import java.util.Optional;
@@ -153,6 +197,96 @@
153
197
  form.setNewQuiz(false);
154
198
  return form;
155
199
  }
200
+ crud.html
201
+ <!DOCTYPE html>
202
+ <html xmlns:th="http://www.thymeleaf.org">
203
+ <head>
204
+ <meta charset="UTF-8">
205
+ <title>「〇×クイズ」アプリ:CRUD</title>
206
+ </head>
207
+ <body>
208
+ <h1>「〇×クイズ」アプリ:CRUD</h1>
209
+ <h3 th:text="${title}">タイトル</h3>
210
+ <!-- /* 登録・更新完了コメント*/ -->
211
+ <p th:if="${complete}" th:text="${complete}" style= "color:blue"></p>
212
+ <!--/* ▼▼▼ Form ▼▼▼ */-->
213
+ <form method="POST"
214
+ th:action="${quizForm.newQuiz}? @{/quiz/insert} : @{/quiz/update}"
215
+ th:object="${quizForm}">
216
+ <label>クイズの内容:</label><br>
217
+ <textarea rows="5" cols="50" th:field="*{question}"></textarea>
218
+ <br>
219
+ <div th:if="${#fields.hasErrors('question')}" th:errors="*{question}"
220
+ style="color:red"></div>
221
+ <label>クイズの解答</label><br>
222
+ <input type="radio" value="true" th:field="*{answer}">「〇」
223
+ <input type="radio" value="false" th:field="*{answer}">「×」
224
+ <br>
225
+ <div th:if="${#fields.hasErrors('answer')}" th:errors="*{answer}"
226
+ style="color:red"></div>
227
+ <label>作成者:</label><br>
228
+ <input type="text" th:field="*{author}" />
229
+ <br>
230
+ <div th:if="${#fields.hasErrors('author')}" th:errors="*{answer}"
231
+ style="color=red"></div>
232
+ <input th:if="${id}" type="hidden" th:field="${id}">
233
+ <input type="submit" value="送信">
234
+ </form>
235
+ <!--/* △△△ Form △△△*/-->
236
+ <br>
237
+ <!--/* ========== ここまで上部エリア ========== */-->
238
+ <hr>
239
+ <!--/* ========== ここから下部エリア ========== */-->
240
+ <!--/* ▼▼▼ 新規登録時のみ表示 ▼▼▼ */-->
241
+ <div th:if="${quizForm.newQuiz}" style="margin: 10px">
242
+ <h3>登録クイズ一覧:<a th:href="@{/quiz/play}">プレイ</a><br></h3>
243
+ <!--/* 削除完了コメント */-->
244
+ <p th:if="${delcomplete}" th:text="${delcomplete}" style="color:blue"></p>
245
+ <p th:if="${msg}" th:text="${msg}" style="color:red"></p>
246
+ <!--/* ▼▼▼ クイズ情報が1件でもあれば表示 ▼▼▼ */-->
247
+ <table border="1" th:unless="${#lists.isEmpty(list)}"
248
+ style="table-layout: fixed;">
249
+ <tr>
250
+ <th>ID</th>
251
+ <th>内容</th>
252
+ <th>解答</th>
253
+ <th>作成者</th>
254
+ <th>編集</th>
255
+ <th>削除</th>
256
+ </tr>
257
+ <tr th:each="obj : ${list}" align="center">
258
+ <td th:text="${obj.id}"></td>
259
+ <td th:text="${obj.question}" align="left"></td>
260
+ <td th:text="${obj.answer} == true?'〇':'×'"></td>
261
+ <td th:text="${obj.author}"></td>
262
+ <!--/* 編集ボタン */-->
263
+ <td>
264
+ <form method="GET" th:action="@{/quiz/{id}(id=${obj.id})}">
265
+ <input type="submit" value="編集">
266
+ </form>
267
+ </td>
268
+ <!--/* 削除ボタン */-->
269
+ <td>
270
+ <form method="POST" th:action="@{/quiz/delete}">
271
+ <input type="hidden" name="id" th:value="${obj.id}">
272
+ <input type="submit" value="削除">
273
+ </form>
274
+ </td>
275
+ </tr>
276
+ </table>
277
+ <!--/* △△△ クイズ情報が1件でもあれば表示 △△△ */-->
278
+ <!--/* ▼▼▼ クイズ情報が1件もない場合表示 ▼▼▼ */-->
279
+ <p th:if="${#lists.isEmpty(list)}">登録されているクイズはありません。</p>
280
+ <!--/* △△△ クイズ情報が1件もない場合表示 △△△ */-->
281
+ </div>
282
+ <!--/* △△△ 新規登録字のみ表示 △△△ */-->
283
+ <!--/* ▼▼▼ 新規登録時でない時に表示 ▼▼▼ */-->
284
+ <p th:unless="${quizForm.newQuiz}">
285
+ <a href="#" th:href="@{/quiz}">CRUD画面へ戻る</a>
286
+ </p>
287
+ <!--/* △△△ 新規登録時でない時に表示 △△△ */-->
288
+ </body>
289
+ </html>
156
290
  ```
157
291
 
158
292
  ### 試したこと