質問するログイン新規登録

質問編集履歴

2

全ファイル更新

2020/06/16 08:42

投稿

yt07
yt07

スコア7

title CHANGED
File without changes
body CHANGED
@@ -23,6 +23,7 @@
23
23
  import java.time.LocalDateTime;
24
24
  import java.util.List;
25
25
 
26
+ import javax.persistence.CascadeType;
26
27
  import javax.persistence.Column;
27
28
  import javax.persistence.Entity;
28
29
  import javax.persistence.GeneratedValue;
@@ -45,9 +46,15 @@
45
46
 
46
47
  @Column(length=1000) //テーブルの列を制御したい場合は@Columnをつける。フィールドの長さをデフォルトの255から1000にする。
47
48
  private String contents;
48
- //1対多の関係。ブログは複数のコメントを持つ。
49
+
50
+
49
- @OneToMany(mappedBy="blog") //mappedBy引数には「相手側が自分を参照する名前」を指定。
51
+ @OneToMany(mappedBy = "blog", cascade = CascadeType.ALL) //mappedBy引数には「相手側が自分を参照する名前」を指定。
50
52
  private List<Comment> comments;
53
+
54
+ public void addComment(Comment comment) {
55
+ comment.setBlog(this);
56
+ comments.add(comment);
57
+ }
51
58
  }
52
59
 
53
60
  ```
@@ -79,6 +86,10 @@
79
86
 
80
87
  }
81
88
 
89
+
90
+
91
+ }
92
+
82
93
  ```
83
94
  BlogRepository.java
84
95
  ```
@@ -110,8 +121,11 @@
110
121
  import java.util.List;
111
122
  import java.util.Optional;
112
123
 
124
+
125
+
113
126
  import org.springframework.beans.factory.annotation.Autowired;
114
127
  import org.springframework.stereotype.Controller;
128
+ import org.springframework.transaction.annotation.Transactional;
115
129
  import org.springframework.ui.Model;
116
130
  import org.springframework.web.bind.annotation.GetMapping;
117
131
  import org.springframework.web.bind.annotation.PathVariable;
@@ -156,18 +170,37 @@
156
170
  Comment comment = new Comment();
157
171
  comment.setBlog(blog.get());
158
172
  model.addAttribute("comment", comment);
159
- return "blog";
173
+ return "/blog";
174
+
160
175
  }
161
176
 
177
+
178
+ @Transactional
162
- @PostMapping("/comment")
179
+ @PostMapping("/blog/{blogId}/comment")
163
- public String createComment(Comment comment) {
180
+ public String createComment(@PathVariable("blogId") Integer id, Comment comment) {
164
181
  comment.setPostDateTime(LocalDateTime.now());
182
+
183
+ Optional<Blog> optionalBlog = blogRepository.findById(id);
184
+
185
+ Blog blog = optionalBlog.orElseThrow(RuntimeException::new);
186
+
187
+ blog.addComment(comment);
165
- commentRepository.save(comment);
188
+ blogRepository.save(blog);
189
+
166
- return "redirect:/blog/" + comment.getBlog().getId();
190
+ return "redirect:/blog/" + id;
167
191
  }
168
192
 
169
193
  }
170
194
 
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
171
204
  ```
172
205
  blog.html
173
206
  ```
@@ -193,7 +226,7 @@
193
226
  </th:block>
194
227
  </p>
195
228
 
196
- <form action="/comment" method="post" th:object="${comment}">
229
+ <form th:action="@{/blog/{blogId}/comment(blogId=${blog.id})}" method="post" th:object="${comment}">
197
230
  コメントをどうぞ<br>
198
231
  <input type="hidden" name="blog" th:value="*{blog.id}">
199
232
  <input type="text" size="40" th:field="*{text}">
@@ -211,6 +244,7 @@
211
244
  </body>
212
245
  </html>
213
246
 
247
+
214
248
  ```
215
249
  エラー内容
216
250
  ```
@@ -248,7 +282,7 @@
248
282
  </body>
249
283
  </html>
250
284
  ```
251
- form.html(blog投稿。blogの投稿は問題なくできていました)
285
+ form.html
252
286
  ```
253
287
  <!DOCTYPE html>
254
288
  <html xmlns:th="http://www.thymeleaf.org">

1

DB。blogテーブルとcommentテーブルの構造の追加。

2020/06/16 08:42

投稿

yt07
yt07

スコア7

title CHANGED
File without changes
body CHANGED
@@ -222,4 +222,59 @@
222
222
  Validation failed for object='comment'. Error count: 1
223
223
  org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
224
224
  Field error in object 'comment' on field 'blog': rejected value [4]; codes [typeMismatch.comment.blog,typeMismatch.blog,typeMismatch.com.example.demo.Blog,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [comment.blog,blog]; arguments []; default message [blog]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.example.demo.Blog' for property 'blog'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.demo.Blog' for property 'blog': no matching editors or conversion strategy found]
225
- ```
225
+ ```
226
+
227
+ ![blogテーブルとcommentテーブル](18cf444747b87d8aef17bdca48019ea1.png)
228
+
229
+ index.html(トプページ。配列でblogタイトルと日時を出し、aタグでidのページに行きます)
230
+ ```
231
+ <!DOCTYPE html>
232
+ <html xmlns:th="http://www.thymeleaf.org/">
233
+ <head>
234
+ <meta charset="UTF-8">
235
+ <title>Insert title here</title>
236
+ </head>
237
+ <body>
238
+ <p>
239
+ <a href="/form">新規作成</a>
240
+ </p>
241
+ <ul>
242
+ <li th:each="blog : ${blogs}" th:object="${blog}">
243
+ <a th:href="@{|/blog/*{id}|}" th:text="*{title}">タイトル</a>
244
+ <span th:text="*{postDateTime}">投稿日時</span>
245
+ </li>
246
+ </ul>
247
+
248
+ </body>
249
+ </html>
250
+ ```
251
+ form.html(blog投稿。blogの投稿は問題なくできていました)
252
+ ```
253
+ <!DOCTYPE html>
254
+ <html xmlns:th="http://www.thymeleaf.org">
255
+ <head>
256
+ <meta charset="UTF-8">
257
+ <title>新規ブログ</title>
258
+ </head>
259
+ <body>
260
+ <form action="/create" method="post" th:object="${blog}">
261
+ <div>
262
+ タイトル<br>
263
+ <input type="text" size="40" th:field="*{title}">
264
+ </div>
265
+ <div>
266
+ 内容<br>
267
+ <textarea rows="5" cols="60" th:field="*{contents}"></textarea>
268
+ </div>
269
+ <div>
270
+ <input type="submit">
271
+ </div>
272
+ </form>
273
+ </body>
274
+ </html>
275
+
276
+ ```
277
+ ![テスト](ce4295ec1a6e29a432816400866a2928.png)
278
+
279
+ 送信するとエラー
280
+ ![error](0f85df026ea809b5b752100b4f08413c.png)