質問編集履歴

3

説明変更

2020/06/30 03:54

投稿

aoinosuke
aoinosuke

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,8 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 本の情報登録データを作っているのですが、
3
+ 情報登録データを作っているのですが、
4
-
4
+
5
- MultipartFileで画像登録したいです!
5
+ MultipartFileで画像登録をnew,createで作成時に一緒に挿入したいです!
6
6
 
7
7
 
8
8
 
@@ -258,15 +258,7 @@
258
258
 
259
259
 
260
260
 
261
- //Optional<Book> book = bookService.findOne(id);
261
+
262
-
263
- //model.addAttribute("book", book);
264
-
265
- //とやってもうまくいかない
266
-
267
- //Optionalに含まれるオブジェクトをアンラップする必要がある。
268
-
269
- //参考url https://ja.coder.work/so/java/2151854
270
262
 
271
263
  @GetMapping("{id}")
272
264
 

2

情報を抜粋ではなく、全て表示

2020/06/30 03:54

投稿

aoinosuke
aoinosuke

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- ユーザーの情報登録データを作っているのですが、
3
+ の情報登録データを作っているのですが、
4
4
 
5
5
  MultipartFileで画像登録したいです!
6
6
 
@@ -14,29 +14,25 @@
14
14
 
15
15
 
16
16
 
17
- package com.example.demo.entity;
18
-
19
-
20
-
21
- import java.sql.Date;
22
-
23
-
24
-
25
- import org.springframework.web.multipart.MultipartFile;
26
-
27
-
28
-
29
17
  public class Book {
30
18
 
31
-
32
-
33
-
34
-
35
19
  private int id;
36
20
 
37
- private String name;
21
+ private String title;
22
+
38
-
23
+ private String author;
24
+
25
+ private String publisher;
26
+
27
+ private Date buyDate;
28
+
29
+ private Date releaseDate;
30
+
31
+ private String genre;
32
+
33
+ private String overView;
34
+
39
- private MultipartFile multipartFile;
35
+ private MultipartFile bookImage;
40
36
 
41
37
 
42
38
 
@@ -54,33 +50,293 @@
54
50
 
55
51
 
56
52
 
57
- public String getName() {
53
+ public String getTitle() {
58
-
54
+
59
- return name;
55
+ return title;
60
-
56
+
61
- }
57
+ }
62
-
58
+
63
- public void setName(String name) {
59
+ public void setTitle(String title) {
64
-
60
+
65
- this.name = name;
61
+ this.title = title;
66
-
62
+
67
- }
63
+ }
64
+
65
+
66
+
67
+ public String getAuthor() {
68
+
69
+ return author;
70
+
71
+ }
72
+
73
+ public void setAuthor(String author) {
74
+
75
+ this.author = author;
76
+
77
+ }
78
+
79
+
80
+
81
+ public String getPublisher() {
82
+
83
+ return publisher;
84
+
85
+ }
86
+
87
+ public void setPublisher(String publisher) {
88
+
89
+ this.publisher = publisher;
90
+
91
+ }
92
+
93
+
94
+
95
+ public Date getBuyDate() {
96
+
97
+ return buyDate;
98
+
99
+ }
100
+
101
+ public void setBuyDate(Date buyDate) {
102
+
103
+ this.buyDate = buyDate;
104
+
105
+ }
106
+
107
+
108
+
109
+ public Date getReleaseDate() {
110
+
111
+ return releaseDate;
112
+
113
+ }
114
+
115
+ public void setReleaseDate(Date releaseDate) {
116
+
117
+ this.releaseDate = releaseDate;
118
+
119
+ }
120
+
121
+
122
+
123
+ public String getGenre() {
124
+
125
+ return genre;
126
+
127
+ }
128
+
129
+ public void setGenre(String genre) {
130
+
131
+ this.genre = genre;
132
+
133
+ }
134
+
135
+
136
+
137
+ public String getOverView() {
138
+
139
+ return overView;
140
+
141
+ }
142
+
143
+ public void setOverView(String overView) {
144
+
145
+ this.overView = overView;
146
+
147
+ }
148
+
149
+
150
+
151
+ public MultipartFile getBookImage() {
152
+
153
+ return bookImage;
154
+
155
+ }
156
+
157
+
158
+
159
+ public void setBookImage(MultipartFile bookImage) {
160
+
161
+ this.bookImage = bookImage;
162
+
163
+ }
164
+
165
+
166
+
167
+
168
+
169
+ }
170
+
171
+
172
+
173
+ ```
174
+
175
+
176
+
177
+ ```
178
+
179
+ controller
180
+
181
+
182
+
183
+ @Controller
184
+
185
+ @RequestMapping("/books")
186
+
187
+ public class BookController {
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+ private final BookService bookService;
68
196
 
69
197
 
70
198
 
71
199
 
72
200
 
201
+ @Autowired
202
+
73
- public MultipartFile getMultipartFile() {
203
+ public BookController(BookService bookService) {
74
-
204
+
75
- return multipartFile;
205
+ this.bookService = bookService;
76
-
206
+
77
- }
207
+ }
208
+
209
+
210
+
78
-
211
+ @GetMapping
212
+
79
-
213
+ public String index(Model model) {
214
+
80
-
215
+ List<Book> book = bookService.findAll();
216
+
217
+ model.addAttribute("book", book);
218
+
219
+ return "books/index";
220
+
221
+ }
222
+
223
+
224
+
225
+
226
+
227
+ @GetMapping("new")
228
+
229
+ public String newBook(@RequestParam("bookImage") MultipartFile bookImage, Model model) {
230
+
231
+ bookImage.getOriginalFilename();
232
+
233
+ return "books/new";
234
+
235
+ }
236
+
237
+
238
+
239
+ @PostMapping
240
+
241
+ public String create(@ModelAttribute("book") @Validated Book book, BindingResult result, Model model) {
242
+
243
+ if (result.hasErrors()) {
244
+
245
+ return "books/new";
246
+
247
+ } else {
248
+
249
+ bookService.save(book);
250
+
251
+ return "redirect:/books";
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+
260
+
261
+ //Optional<Book> book = bookService.findOne(id);
262
+
263
+ //model.addAttribute("book", book);
264
+
265
+ //とやってもうまくいかない
266
+
267
+ //Optionalに含まれるオブジェクトをアンラップする必要がある。
268
+
269
+ //参考url https://ja.coder.work/so/java/2151854
270
+
271
+ @GetMapping("{id}")
272
+
273
+ public String show(@PathVariable int id, Model model) {
274
+
275
+ bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o));
276
+
277
+ return "books/show";
278
+
279
+ }
280
+
281
+
282
+
283
+
284
+
285
+ @GetMapping("{id}/edit")
286
+
287
+ public String edit(@PathVariable int id, @ModelAttribute("book") Book book, Model model) {
288
+
289
+ model.addAttribute("selectBooks",getGenre());
290
+
291
+ //上のshowメソッドと同じ
292
+
293
+ bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o));
294
+
295
+ return "books/edit";
296
+
297
+ }
298
+
299
+
300
+
301
+ @PostMapping("{id}")
302
+
303
+ public String update(@PathVariable int id, @ModelAttribute("book") @Validated Book book, BindingResult result, Model model){
304
+
305
+ if (result.hasErrors()) {
306
+
307
+ model.addAttribute("book", book);
308
+
309
+ return "edit";
310
+
311
+ } else {
312
+
313
+ book.setId(id);
314
+
315
+ bookService.update(book);
316
+
317
+ return "redirect:/books";
318
+
319
+ }
320
+
321
+ }
322
+
323
+
324
+
325
+ @DeleteMapping("{id}")
326
+
81
- public void setMultipartFile(MultipartFile multipartFile) {
327
+ public String delete(@PathVariable("id") int id) {
328
+
82
-
329
+ Book book = bookService.findOne(id).get();
330
+
331
+ if(book == null){
332
+
333
+ return "";
334
+
335
+ }
336
+
337
+ bookService.delete(id);
338
+
83
- this.multipartFile = multipartFile;
339
+ return "redirect:/books";
84
340
 
85
341
  }
86
342
 
@@ -94,78 +350,254 @@
94
350
 
95
351
  ```
96
352
 
97
- controller
98
-
99
-
100
-
101
- @GetMapping("new")
102
-
103
- public String newBook(@RequestParam("bookImage") MultipartFile bookImage, Model model) {
104
-
105
- model.addAttribute("selectBooks",getGenre());
106
-
107
- bookImage.getOriginalFilename();
108
-
109
- return "books/new";
110
-
111
- }
112
-
113
-
114
-
115
-
116
-
117
- @PostMapping
118
-
119
- public String create(@ModelAttribute("user") @Validated User user, BindingResult result, Model model) {
120
-
121
- if (result.hasErrors()) {
122
-
123
- return "users/new";
124
-
125
- } else {
126
-
127
- bookService.save(user);
128
-
129
- return "redirect:/users";
130
-
131
- }
132
-
133
- }
134
-
135
- ```
136
-
137
-
138
-
139
- ```
140
-
141
353
  html
142
354
 
143
355
 
144
356
 
145
- <form th:action="@{/users}" th:method="post" enctype="multipart/form-data">
357
+ <form th:action="@{/books}" th:method="post" enctype="multipart/form-data">
146
-
147
-
148
-
149
- <div class="form-group"></div>
358
+
359
+
360
+
361
+
150
362
 
151
363
  <input class="form-control" type="file" name="bookImage" />
152
364
 
365
+
366
+
367
+
368
+
369
+ <div class="form-group">
370
+
371
+ <label class="control-label">タイトル</label>
372
+
373
+ <input class="form-control" type="text" name="title" />
374
+
153
375
  </div>
154
376
 
155
-
156
-
157
377
  <div class="form-group">
158
378
 
159
- <label class="control-label">名前</label>
379
+ <label class="control-label">著者</label>
160
-
380
+
161
- <input class="form-control" type="text" name="name" />
381
+ <input class="form-control" type="text" name="author" />
162
382
 
163
383
  </div>
164
384
 
385
+ <div class="form-group">
386
+
387
+ <label class="control-label">出版社</label>
388
+
389
+ <input class="form-control" type="text" name="publisher" />
390
+
391
+ </div>
392
+
393
+ <div class="form-group">
394
+
395
+ <label class="control-label">購入日</label>
396
+
397
+ <input class="form-control" type="date" name="buyDate" />
398
+
399
+ </div>
400
+
401
+ <div class="form-group">
402
+
403
+ <label class="control-label">発売日</label>
404
+
405
+ <input class="form-control" type="date" name="releaseDate" />
406
+
407
+ </div>
408
+
409
+
410
+
411
+ <div class="form-group">
412
+
413
+ <label class="control-label">ジャンル</label>
414
+
415
+ <select name="genre">
416
+
417
+ <option value="">---</option>
418
+
419
+ <option th:each="book : ${selectBooks}" th:value="*{book.value}"
420
+
421
+ th:text="*{book.value}" th:selected="*{book.value} == *{selectBooks}" ></option>
422
+
423
+ </select>
424
+
425
+ </div>
426
+
427
+
428
+
429
+ <div class="form-group">
430
+
431
+ <label class="control-label">概要</label>
432
+
433
+ <input class="form-control" type="text" name="overView" />
434
+
435
+ </div>
436
+
437
+
438
+
165
439
  <button class="btn btn-default" type="submit">作成</button>
166
440
 
167
441
  </form>
168
442
 
443
+
444
+
445
+
446
+
447
+ <div class="pull-right">
448
+
449
+ <a class="btn btn-link" href="/books">一覧画面へ</a>
450
+
451
+ </div>
452
+
453
+ </div>
454
+
455
+ </body>
456
+
457
+ </html>
458
+
459
+ ```
460
+
461
+
462
+
463
+ ```
464
+
465
+ repository
466
+
467
+
468
+
469
+ @Repository
470
+
471
+ public class BookRepositoryImpl implements BookRepository{
472
+
473
+
474
+
475
+
476
+
477
+ @Autowired
478
+
479
+ private final JdbcTemplate jdbcTemplate;
480
+
481
+
482
+
483
+ @Autowired
484
+
485
+ public BookRepositoryImpl(JdbcTemplate jdbcTemplate) {
486
+
487
+ this.jdbcTemplate = jdbcTemplate;
488
+
489
+ }
490
+
491
+
492
+
493
+
494
+
495
+ @Override
496
+
497
+ public List<Book> findAll() {
498
+
499
+
500
+
501
+ String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view, book_image FROM Book";
502
+
503
+
504
+
505
+ return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
506
+
507
+
508
+
509
+ }
510
+
511
+
512
+
513
+
514
+
515
+ @Override
516
+
517
+ public Optional<Book> findOne(int id) {
518
+
519
+
520
+
521
+ String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view, book_image FROM book "
522
+
523
+ + "WHERE id = ?";
524
+
525
+
526
+
527
+ Map<String, Object> result = jdbcTemplate.queryForMap(sql, id);
528
+
529
+
530
+
531
+ Book book = new Book();
532
+
533
+ book.setId((int)result.get("id"));
534
+
535
+ book.setTitle((String)result.get("title"));
536
+
537
+ book.setAuthor((String)result.get("author"));
538
+
539
+ book.setPublisher((String)result.get("publisher"));
540
+
541
+ book.setBuyDate((Date)result.get("buy_date"));
542
+
543
+ book.setReleaseDate((Date)result.get("release_date"));
544
+
545
+ book.setGenre((String)result.get("Genre"));
546
+
547
+ book.setOverView((String)result.get("over_view"));
548
+
549
+ book.setBookImage((MultipartFile)result.get("book_image"));
550
+
551
+ System.out.println(result);
552
+
553
+ //bookをOptionalでラップする
554
+
555
+ Optional<Book> bookOpt = Optional.ofNullable(book);
556
+
557
+ return bookOpt;
558
+
559
+ }
560
+
561
+
562
+
563
+ @Override
564
+
565
+ public void save(Book book) {
566
+
567
+ jdbcTemplate.update("INSERT INTO book(title, author, publisher, buy_date, release_date, genre, over_view, book_image) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
568
+
569
+ book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(), book.getOverView(), book.getBookImage() );
570
+
571
+ }
572
+
573
+
574
+
575
+
576
+
577
+ @Override
578
+
579
+ public int update(Book book) {
580
+
581
+ return jdbcTemplate.update("UPDATE book SET title = ?, author = ?, publisher = ?, buy_date = ?, release_date = ?, genre = ?, over_view = ?, book_image = ? WHERE id = ?",
582
+
583
+ book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(), book.getOverView(), book.getId() );
584
+
585
+ }
586
+
587
+
588
+
589
+ @Override
590
+
591
+ public int delete(int id) {
592
+
593
+ return jdbcTemplate.update("DELETE FROM book WHERE id = ?", id);
594
+
595
+ }
596
+
597
+
598
+
599
+ }
600
+
169
601
  ```
170
602
 
171
603
 

1

html変更、newMethodに直接MultipartFileを記入

2020/06/30 03:50

投稿

aoinosuke
aoinosuke

スコア8

test CHANGED
File without changes
test CHANGED
@@ -98,21 +98,13 @@
98
98
 
99
99
 
100
100
 
101
- private String upload(@RequestParam("imgFile") MultipartFile multipartFile, Model model) {
101
+ @GetMapping("new")
102
102
 
103
- return multipartFile.getOriginalFilename();
103
+ public String newBook(@RequestParam("bookImage") MultipartFile bookImage, Model model) {
104
104
 
105
- }
105
+ model.addAttribute("selectBooks",getGenre());
106
106
 
107
-
108
-
109
-
110
-
111
- @GetMapping("new")
112
-
113
- public String newBook(Model model) {
107
+ bookImage.getOriginalFilename();
114
-
115
- model.addAttribute("imgFile",upload();
116
108
 
117
109
  return "books/new";
118
110
 
@@ -132,8 +124,6 @@
132
124
 
133
125
  } else {
134
126
 
135
- model.addAttribute("imgFile",upload();
136
-
137
127
  bookService.save(user);
138
128
 
139
129
  return "redirect:/users";
@@ -152,17 +142,13 @@
152
142
 
153
143
 
154
144
 
155
- <form th:action="@{/users}" th:method="post">
145
+ <form th:action="@{/users}" th:method="post" enctype="multipart/form-data">
156
146
 
157
147
 
158
148
 
159
- <div class="form-group">
149
+ <div class="form-group"></div>
160
150
 
161
- <label class="control-label">画像</label>
162
-
163
- <form method="POST" enctype="multipart/form-data"></form>
164
-
165
- <input class="form-control" type="file" name="imgFile" />
151
+ <input class="form-control" type="file" name="bookImage" />
166
152
 
167
153
  </div>
168
154
 
@@ -192,6 +178,14 @@
192
178
 
193
179
 
194
180
 
181
+ 編集後
182
+
183
+ Current request is not a multipart requestのエラー
184
+
185
+ やはり似たエラーなので原因はほぼ同じだと思っています。
186
+
187
+
188
+
195
189
  multipartFileこの引数がおかしいのかなと思ってますが解決に至りません。
196
190
 
197
191
  何方かわかりますか?