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

質問編集履歴

3

説明変更

2020/06/30 03:54

投稿

aoinosuke
aoinosuke

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
- 本の情報登録データを作っているのですが、
2
+ 情報登録データを作っているのですが、
3
- MultipartFileで画像登録したいです!
3
+ MultipartFileで画像登録をnew,createで作成時に一緒に挿入したいです!
4
4
 
5
5
 
6
6
  ```
@@ -128,11 +128,7 @@
128
128
  }
129
129
 
130
130
 
131
- //Optional<Book> book = bookService.findOne(id);
131
+
132
- //model.addAttribute("book", book);
133
- //とやってもうまくいかない
134
- //Optionalに含まれるオブジェクトをアンラップする必要がある。
135
- //参考url https://ja.coder.work/so/java/2151854
136
132
  @GetMapping("{id}")
137
133
  public String show(@PathVariable int id, Model model) {
138
134
  bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o));

2

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

2020/06/30 03:54

投稿

aoinosuke
aoinosuke

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,23 +1,21 @@
1
1
  ### 前提・実現したいこと
2
- ユーザーの情報登録データを作っているのですが、
2
+ の情報登録データを作っているのですが、
3
3
  MultipartFileで画像登録したいです!
4
4
 
5
5
 
6
6
  ```
7
7
  entity
8
8
 
9
- package com.example.demo.entity;
10
-
11
- import java.sql.Date;
12
-
13
- import org.springframework.web.multipart.MultipartFile;
14
-
15
9
  public class Book {
16
-
17
-
18
10
  private int id;
19
- private String name;
11
+ private String title;
12
+ private String author;
13
+ private String publisher;
14
+ private Date buyDate;
15
+ private Date releaseDate;
16
+ private String genre;
17
+ private String overView;
20
- private MultipartFile multipartFile;
18
+ private MultipartFile bookImage;
21
19
 
22
20
  public int getId() {
23
21
  return id;
@@ -26,64 +24,282 @@
26
24
  this.id = id;
27
25
  }
28
26
 
29
- public String getName() {
27
+ public String getTitle() {
30
- return name;
28
+ return title;
31
29
  }
32
- public void setName(String name) {
30
+ public void setTitle(String title) {
33
- this.name = name;
31
+ this.title = title;
34
32
  }
35
33
 
36
-
37
- public MultipartFile getMultipartFile() {
34
+ public String getAuthor() {
38
- return multipartFile;
35
+ return author;
39
36
  }
37
+ public void setAuthor(String author) {
38
+ this.author = author;
39
+ }
40
40
 
41
- public void setMultipartFile(MultipartFile multipartFile) {
42
- this.multipartFile = multipartFile;
41
+ public String getPublisher() {
42
+ return publisher;
43
43
  }
44
+ public void setPublisher(String publisher) {
45
+ this.publisher = publisher;
46
+ }
44
47
 
48
+ public Date getBuyDate() {
49
+ return buyDate;
50
+ }
51
+ public void setBuyDate(Date buyDate) {
52
+ this.buyDate = buyDate;
53
+ }
54
+
55
+ public Date getReleaseDate() {
56
+ return releaseDate;
57
+ }
58
+ public void setReleaseDate(Date releaseDate) {
59
+ this.releaseDate = releaseDate;
60
+ }
61
+
62
+ public String getGenre() {
63
+ return genre;
64
+ }
65
+ public void setGenre(String genre) {
66
+ this.genre = genre;
67
+ }
68
+
69
+ public String getOverView() {
70
+ return overView;
71
+ }
72
+ public void setOverView(String overView) {
73
+ this.overView = overView;
74
+ }
75
+
76
+ public MultipartFile getBookImage() {
77
+ return bookImage;
78
+ }
79
+
80
+ public void setBookImage(MultipartFile bookImage) {
81
+ this.bookImage = bookImage;
82
+ }
83
+
84
+
45
85
  }
86
+
46
87
  ```
47
88
 
48
89
  ```
49
90
  controller
50
91
 
92
+ @Controller
93
+ @RequestMapping("/books")
94
+ public class BookController {
95
+
96
+
97
+
98
+ private final BookService bookService;
99
+
100
+
101
+ @Autowired
102
+ public BookController(BookService bookService) {
103
+ this.bookService = bookService;
104
+ }
105
+
106
+ @GetMapping
107
+ public String index(Model model) {
108
+ List<Book> book = bookService.findAll();
109
+ model.addAttribute("book", book);
110
+ return "books/index";
111
+ }
112
+
113
+
51
- @GetMapping("new")
114
+ @GetMapping("new")
52
115
  public String newBook(@RequestParam("bookImage") MultipartFile bookImage, Model model) {
53
- model.addAttribute("selectBooks",getGenre());
54
116
  bookImage.getOriginalFilename();
55
117
  return "books/new";
56
118
  }
57
119
 
58
-
59
120
  @PostMapping
60
- public String create(@ModelAttribute("user") @Validated User user, BindingResult result, Model model) {
121
+ public String create(@ModelAttribute("book") @Validated Book book, BindingResult result, Model model) {
61
122
  if (result.hasErrors()) {
62
- return "users/new";
123
+ return "books/new";
63
124
  } else {
64
- bookService.save(user);
125
+ bookService.save(book);
65
- return "redirect:/users";
126
+ return "redirect:/books";
66
127
  }
67
128
  }
129
+
130
+
131
+ //Optional<Book> book = bookService.findOne(id);
132
+ //model.addAttribute("book", book);
133
+ //とやってもうまくいかない
134
+ //Optionalに含まれるオブジェクトをアンラップする必要がある。
135
+ //参考url https://ja.coder.work/so/java/2151854
136
+ @GetMapping("{id}")
137
+ public String show(@PathVariable int id, Model model) {
138
+ bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o));
139
+ return "books/show";
140
+ }
141
+
142
+
143
+ @GetMapping("{id}/edit")
144
+ public String edit(@PathVariable int id, @ModelAttribute("book") Book book, Model model) {
145
+ model.addAttribute("selectBooks",getGenre());
146
+ //上のshowメソッドと同じ
147
+ bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o));
148
+ return "books/edit";
149
+ }
150
+
151
+ @PostMapping("{id}")
152
+ public String update(@PathVariable int id, @ModelAttribute("book") @Validated Book book, BindingResult result, Model model){
153
+ if (result.hasErrors()) {
154
+ model.addAttribute("book", book);
155
+ return "edit";
156
+ } else {
157
+ book.setId(id);
158
+ bookService.update(book);
159
+ return "redirect:/books";
160
+ }
161
+ }
162
+
163
+ @DeleteMapping("{id}")
164
+ public String delete(@PathVariable("id") int id) {
165
+ Book book = bookService.findOne(id).get();
166
+ if(book == null){
167
+ return "";
168
+ }
169
+ bookService.delete(id);
170
+ return "redirect:/books";
171
+ }
172
+
173
+ }
68
174
  ```
69
175
 
70
176
  ```
71
177
  html
72
178
 
73
- <form th:action="@{/users}" th:method="post" enctype="multipart/form-data">
179
+ <form th:action="@{/books}" th:method="post" enctype="multipart/form-data">
74
180
 
75
- <div class="form-group"></div>
181
+
76
182
  <input class="form-control" type="file" name="bookImage" />
183
+
184
+
185
+ <div class="form-group">
186
+ <label class="control-label">タイトル</label>
187
+ <input class="form-control" type="text" name="title" />
77
188
  </div>
189
+ <div class="form-group">
190
+ <label class="control-label">著者</label>
191
+ <input class="form-control" type="text" name="author" />
192
+ </div>
193
+ <div class="form-group">
194
+ <label class="control-label">出版社</label>
195
+ <input class="form-control" type="text" name="publisher" />
196
+ </div>
197
+ <div class="form-group">
198
+ <label class="control-label">購入日</label>
199
+ <input class="form-control" type="date" name="buyDate" />
200
+ </div>
201
+ <div class="form-group">
202
+ <label class="control-label">発売日</label>
203
+ <input class="form-control" type="date" name="releaseDate" />
204
+ </div>
78
205
 
79
206
  <div class="form-group">
80
- <label class="control-label">名前</label>
207
+ <label class="control-label">ジャンル</label>
208
+ <select name="genre">
209
+ <option value="">---</option>
81
- <input class="form-control" type="text" name="name" />
210
+ <option th:each="book : ${selectBooks}" th:value="*{book.value}"
211
+ th:text="*{book.value}" th:selected="*{book.value} == *{selectBooks}" ></option>
212
+ </select>
82
213
  </div>
214
+
215
+ <div class="form-group">
216
+ <label class="control-label">概要</label>
217
+ <input class="form-control" type="text" name="overView" />
218
+ </div>
219
+
83
220
  <button class="btn btn-default" type="submit">作成</button>
84
221
  </form>
222
+
223
+
224
+ <div class="pull-right">
225
+ <a class="btn btn-link" href="/books">一覧画面へ</a>
226
+ </div>
227
+ </div>
228
+ </body>
229
+ </html>
85
230
  ```
86
231
 
232
+ ```
233
+ repository
234
+
235
+ @Repository
236
+ public class BookRepositoryImpl implements BookRepository{
237
+
238
+
239
+ @Autowired
240
+ private final JdbcTemplate jdbcTemplate;
241
+
242
+ @Autowired
243
+ public BookRepositoryImpl(JdbcTemplate jdbcTemplate) {
244
+ this.jdbcTemplate = jdbcTemplate;
245
+ }
246
+
247
+
248
+ @Override
249
+ public List<Book> findAll() {
250
+
251
+ String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view, book_image FROM Book";
252
+
253
+ return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
254
+
255
+ }
256
+
257
+
258
+ @Override
259
+ public Optional<Book> findOne(int id) {
260
+
261
+ String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, genre, over_view, book_image FROM book "
262
+ + "WHERE id = ?";
263
+
264
+ Map<String, Object> result = jdbcTemplate.queryForMap(sql, id);
265
+
266
+ Book book = new Book();
267
+ book.setId((int)result.get("id"));
268
+ book.setTitle((String)result.get("title"));
269
+ book.setAuthor((String)result.get("author"));
270
+ book.setPublisher((String)result.get("publisher"));
271
+ book.setBuyDate((Date)result.get("buy_date"));
272
+ book.setReleaseDate((Date)result.get("release_date"));
273
+ book.setGenre((String)result.get("Genre"));
274
+ book.setOverView((String)result.get("over_view"));
275
+ book.setBookImage((MultipartFile)result.get("book_image"));
276
+ System.out.println(result);
277
+ //bookをOptionalでラップする
278
+ Optional<Book> bookOpt = Optional.ofNullable(book);
279
+ return bookOpt;
280
+ }
281
+
282
+ @Override
283
+ public void save(Book book) {
284
+ jdbcTemplate.update("INSERT INTO book(title, author, publisher, buy_date, release_date, genre, over_view, book_image) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
285
+ book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(), book.getOverView(), book.getBookImage() );
286
+ }
287
+
288
+
289
+ @Override
290
+ public int update(Book book) {
291
+ return jdbcTemplate.update("UPDATE book SET title = ?, author = ?, publisher = ?, buy_date = ?, release_date = ?, genre = ?, over_view = ?, book_image = ? WHERE id = ?",
292
+ book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getGenre(), book.getOverView(), book.getId() );
293
+ }
294
+
295
+ @Override
296
+ public int delete(int id) {
297
+ return jdbcTemplate.update("DELETE FROM book WHERE id = ?", id);
298
+ }
299
+
300
+ }
301
+ ```
302
+
87
303
  現在は
88
304
  Unresolved compilation problem: The method upload(MultipartFile, Model) in the type BookController is not applicable for the arguments ()
89
305
  こういったエラーが出ます。

1

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

2020/06/30 03:50

投稿

aoinosuke
aoinosuke

スコア8

title CHANGED
File without changes
body CHANGED
@@ -48,14 +48,10 @@
48
48
  ```
49
49
  controller
50
50
 
51
- private String upload(@RequestParam("imgFile") MultipartFile multipartFile, Model model) {
52
- return multipartFile.getOriginalFilename();
53
- }
54
-
55
-
56
- @GetMapping("new")
51
+ @GetMapping("new")
57
- public String newBook(Model model) {
52
+ public String newBook(@RequestParam("bookImage") MultipartFile bookImage, Model model) {
58
- model.addAttribute("imgFile",upload();
53
+ model.addAttribute("selectBooks",getGenre());
54
+ bookImage.getOriginalFilename();
59
55
  return "books/new";
60
56
  }
61
57
 
@@ -65,7 +61,6 @@
65
61
  if (result.hasErrors()) {
66
62
  return "users/new";
67
63
  } else {
68
- model.addAttribute("imgFile",upload();
69
64
  bookService.save(user);
70
65
  return "redirect:/users";
71
66
  }
@@ -75,12 +70,10 @@
75
70
  ```
76
71
  html
77
72
 
78
- <form th:action="@{/users}" th:method="post">
73
+ <form th:action="@{/users}" th:method="post" enctype="multipart/form-data">
79
74
 
80
- <div class="form-group">
75
+ <div class="form-group"></div>
81
- <label class="control-label">画像</label>
82
- <form method="POST" enctype="multipart/form-data"></form>
83
- <input class="form-control" type="file" name="imgFile" />
76
+ <input class="form-control" type="file" name="bookImage" />
84
77
  </div>
85
78
 
86
79
  <div class="form-group">
@@ -95,5 +88,9 @@
95
88
  Unresolved compilation problem: The method upload(MultipartFile, Model) in the type BookController is not applicable for the arguments ()
96
89
  こういったエラーが出ます。
97
90
 
91
+ 編集後
92
+ Current request is not a multipart requestのエラー
93
+ やはり似たエラーなので原因はほぼ同じだと思っています。
94
+
98
95
  multipartFileこの引数がおかしいのかなと思ってますが解決に至りません。
99
96
  何方かわかりますか?