質問編集履歴
2
ああああああ
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Spring
|
1
|
+
Spring bootでHTMLファイルの遷移関係が理解できない
|
body
CHANGED
File without changes
|
1
aaa
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
+
以下のリンクに記載されているコードに関して質問です。
|
2
|
+
https://www.microstone.info/spring-boot-2%E5%AE%9F%E8%B7%B5%E5%85%A5%E9%96%80%EF%BC%9A%E7%B0%A1%E5%8D%98%E3%81%AAweb%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E4%B8%80%E3%81%8B%E3%82%89%E4%BD%9C%E6%88%90%E3%83%81%E3%83%A5%E3%83%BC/
|
3
|
+
|
1
4
|
▪️ディレクトリ構造
|
2
5
|

|
3
6
|
▪️疑問点
|
4
7
|
ItemController.javaファイルで@RequestMapping("/items")と記述があるが、/itemsのファイルは生成されていない。なぜ、生成されていないファイルに遷移することができるのか?
|
8
|
+
また、Index.htmlに
|
9
|
+
<table class="table table-striped" th:if="${items.size()}">と記載があるが、
|
10
|
+
items.size()とは、何か?Itemsとインスタンスを生成する記述がないため、この記述があるのか理解できません。
|
5
11
|
▪️ソースコード
|
6
12
|
ItemController.java
|
7
13
|
|
@@ -143,4 +149,227 @@
|
|
143
149
|
<script src="/js/bootstrap.min.js"></script>
|
144
150
|
</body>
|
145
151
|
</html>
|
152
|
+
```
|
153
|
+
Application.javaのファイル
|
154
|
+
```java
|
155
|
+
package com.example.demo;
|
156
|
+
|
157
|
+
import org.springframework.boot.SpringApplication;
|
158
|
+
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
159
|
+
|
160
|
+
@SpringBootApplication
|
161
|
+
public class Application {
|
162
|
+
|
163
|
+
public static void main(String[] args) {
|
164
|
+
SpringApplication.run(Application.class, args);
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
ItemController.javaのファイル
|
172
|
+
```java
|
173
|
+
package com.example.demo.controller;
|
174
|
+
|
175
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
176
|
+
import org.springframework.stereotype.Controller;
|
177
|
+
import org.springframework.ui.Model;
|
178
|
+
import org.springframework.validation.BindingResult;
|
179
|
+
import org.springframework.validation.annotation.Validated;
|
180
|
+
import org.springframework.web.bind.annotation.DeleteMapping;
|
181
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
182
|
+
import org.springframework.web.bind.annotation.ModelAttribute;
|
183
|
+
import org.springframework.web.bind.annotation.PathVariable;
|
184
|
+
import org.springframework.web.bind.annotation.PostMapping;
|
185
|
+
import org.springframework.web.bind.annotation.PutMapping;
|
186
|
+
import org.springframework.web.bind.annotation.RequestMapping;
|
187
|
+
|
188
|
+
import com.example.demo.domain.Item;
|
189
|
+
import com.example.demo.service.ItemService;
|
190
|
+
|
191
|
+
@Controller
|
192
|
+
@RequestMapping("/items")
|
193
|
+
public class ItemController {
|
194
|
+
|
195
|
+
@Autowired
|
196
|
+
private ItemService itemService;
|
197
|
+
|
198
|
+
@GetMapping
|
199
|
+
public String index(Model model) {
|
200
|
+
model.addAttribute("items", itemService.findAll());
|
201
|
+
return "index";
|
202
|
+
}
|
203
|
+
|
204
|
+
@GetMapping("{id}")
|
205
|
+
public String show(@PathVariable Long id, Model model) {
|
206
|
+
model.addAttribute("item", itemService.findOne(id));
|
207
|
+
return "show";
|
208
|
+
}
|
209
|
+
|
210
|
+
@GetMapping("new")
|
211
|
+
public String newItem(@ModelAttribute("item") Item item, Model model) {
|
212
|
+
return "new";
|
213
|
+
}
|
214
|
+
|
215
|
+
@GetMapping("{id}/edit")
|
216
|
+
public String edit(@PathVariable Long id, @ModelAttribute("item") Item item, Model model) {
|
217
|
+
model.addAttribute("item", itemService.findOne(id));
|
218
|
+
return "edit";
|
219
|
+
}
|
220
|
+
|
221
|
+
@PostMapping
|
222
|
+
public String create(@ModelAttribute("item") @Validated Item item, BindingResult result, Model model) {
|
223
|
+
if (result.hasErrors()) {
|
224
|
+
return "new";
|
225
|
+
} else {
|
226
|
+
itemService.save(item);
|
227
|
+
return "redirect:/items";
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
@PutMapping("{id}")
|
232
|
+
public String update(@PathVariable Long id, @ModelAttribute("item") @Validated Item item, BindingResult result, Model model) {
|
233
|
+
if (result.hasErrors()) {
|
234
|
+
model.addAttribute("item", item);
|
235
|
+
return "edit";
|
236
|
+
} else {
|
237
|
+
item.setId(id);
|
238
|
+
itemService.update(item);
|
239
|
+
return "redirect:/items";
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
@DeleteMapping("{id}")
|
244
|
+
public String delete(@PathVariable Long id) {
|
245
|
+
itemService.delete(id);
|
246
|
+
return "redirect:/items";
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
```
|
251
|
+
|
252
|
+
|
253
|
+
Item.javaのファイル
|
254
|
+
```java
|
255
|
+
package com.example.demo.domain;
|
256
|
+
|
257
|
+
public class Item {
|
258
|
+
private Long id;
|
259
|
+
|
260
|
+
@NotBlank(message="商品名を記入してください。")
|
261
|
+
private String name;
|
262
|
+
|
263
|
+
@Min(value=10, message="10以上の数値を入力してください。")
|
264
|
+
@Max(value=10000, message="10000以下の数値を入力してください。")
|
265
|
+
private float price;
|
266
|
+
|
267
|
+
@Size(max=50, message="ベーダー名は50文字を超えないでください。")
|
268
|
+
private String vendor;
|
269
|
+
|
270
|
+
public Long getId() {
|
271
|
+
return id;
|
272
|
+
}
|
273
|
+
|
274
|
+
public void setId(Long id) {
|
275
|
+
this.id = id;
|
276
|
+
}
|
277
|
+
|
278
|
+
public String getName() {
|
279
|
+
return name;
|
280
|
+
}
|
281
|
+
|
282
|
+
public void setName(String name) {
|
283
|
+
this.name = name;
|
284
|
+
}
|
285
|
+
|
286
|
+
public float getPrice() {
|
287
|
+
return price;
|
288
|
+
}
|
289
|
+
|
290
|
+
public void setPrice(float price) {
|
291
|
+
this.price = price;
|
292
|
+
}
|
293
|
+
|
294
|
+
public String getVendor() {
|
295
|
+
return vendor;
|
296
|
+
}
|
297
|
+
|
298
|
+
public void setVendor(String vendor) {
|
299
|
+
this.vendor = vendor;
|
300
|
+
}
|
301
|
+
}
|
302
|
+
```
|
303
|
+
|
304
|
+
ItemMapper.javaのファイル
|
305
|
+
```java
|
306
|
+
package com.example.demo.mapper;
|
307
|
+
|
308
|
+
import java.util.List;
|
309
|
+
|
310
|
+
import org.apache.ibatis.annotations.Mapper;
|
311
|
+
|
312
|
+
import com.example.demo.domain.Item;
|
313
|
+
|
314
|
+
@Mapper
|
315
|
+
public interface ItemMapper {
|
316
|
+
List<Item> findAll();
|
317
|
+
|
318
|
+
Item findOne(Long id);
|
319
|
+
|
320
|
+
void save(Item item);
|
321
|
+
|
322
|
+
void update(Item item);
|
323
|
+
|
324
|
+
void delete(Long id);
|
325
|
+
}
|
326
|
+
|
327
|
+
```
|
328
|
+
|
329
|
+
ItemService.javaのファイル
|
330
|
+
```java
|
331
|
+
package com.example.demo.service;
|
332
|
+
|
333
|
+
import java.util.List;
|
334
|
+
|
335
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
336
|
+
import org.springframework.stereotype.Service;
|
337
|
+
import org.springframework.transaction.annotation.Transactional;
|
338
|
+
|
339
|
+
import com.example.demo.domain.Item;
|
340
|
+
import com.example.demo.mapper.ItemMapper;
|
341
|
+
|
342
|
+
@Service
|
343
|
+
public class ItemService {
|
344
|
+
|
345
|
+
@Autowired
|
346
|
+
ItemMapper itemMapper;
|
347
|
+
|
348
|
+
@Transactional
|
349
|
+
public List<Item> findAll() {
|
350
|
+
return itemMapper.findAll();
|
351
|
+
}
|
352
|
+
|
353
|
+
@Transactional
|
354
|
+
public Item findOne(Long id) {
|
355
|
+
return itemMapper.findOne(id);
|
356
|
+
}
|
357
|
+
|
358
|
+
@Transactional
|
359
|
+
public void save(Item item) {
|
360
|
+
itemMapper.save(item);
|
361
|
+
}
|
362
|
+
|
363
|
+
@Transactional
|
364
|
+
public void update(Item item) {
|
365
|
+
itemMapper.update(item);
|
366
|
+
}
|
367
|
+
|
368
|
+
@Transactional
|
369
|
+
public void delete(Long id) {
|
370
|
+
itemMapper.delete(id);
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
146
375
|
```
|