質問編集履歴
2
ああああああ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Spring
|
1
|
+
Spring bootでHTMLファイルの遷移関係が理解できない
|
test
CHANGED
File without changes
|
1
aaa
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
以下のリンクに記載されているコードに関して質問です。
|
2
|
+
|
3
|
+
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/
|
4
|
+
|
5
|
+
|
6
|
+
|
1
7
|
▪️ディレクトリ構造
|
2
8
|
|
3
9
|
![イメージ説明](fe4ccbc96830697bd78158085c5398aa.png)
|
@@ -6,6 +12,12 @@
|
|
6
12
|
|
7
13
|
ItemController.javaファイルで@RequestMapping("/items")と記述があるが、/itemsのファイルは生成されていない。なぜ、生成されていないファイルに遷移することができるのか?
|
8
14
|
|
15
|
+
また、Index.htmlに
|
16
|
+
|
17
|
+
<table class="table table-striped" th:if="${items.size()}">と記載があるが、
|
18
|
+
|
19
|
+
items.size()とは、何か?Itemsとインスタンスを生成する記述がないため、この記述があるのか理解できません。
|
20
|
+
|
9
21
|
▪️ソースコード
|
10
22
|
|
11
23
|
ItemController.java
|
@@ -289,3 +301,449 @@
|
|
289
301
|
</html>
|
290
302
|
|
291
303
|
```
|
304
|
+
|
305
|
+
Application.javaのファイル
|
306
|
+
|
307
|
+
```java
|
308
|
+
|
309
|
+
package com.example.demo;
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
import org.springframework.boot.SpringApplication;
|
314
|
+
|
315
|
+
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
@SpringBootApplication
|
320
|
+
|
321
|
+
public class Application {
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
public static void main(String[] args) {
|
326
|
+
|
327
|
+
SpringApplication.run(Application.class, args);
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
```
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
ItemController.javaのファイル
|
342
|
+
|
343
|
+
```java
|
344
|
+
|
345
|
+
package com.example.demo.controller;
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
350
|
+
|
351
|
+
import org.springframework.stereotype.Controller;
|
352
|
+
|
353
|
+
import org.springframework.ui.Model;
|
354
|
+
|
355
|
+
import org.springframework.validation.BindingResult;
|
356
|
+
|
357
|
+
import org.springframework.validation.annotation.Validated;
|
358
|
+
|
359
|
+
import org.springframework.web.bind.annotation.DeleteMapping;
|
360
|
+
|
361
|
+
import org.springframework.web.bind.annotation.GetMapping;
|
362
|
+
|
363
|
+
import org.springframework.web.bind.annotation.ModelAttribute;
|
364
|
+
|
365
|
+
import org.springframework.web.bind.annotation.PathVariable;
|
366
|
+
|
367
|
+
import org.springframework.web.bind.annotation.PostMapping;
|
368
|
+
|
369
|
+
import org.springframework.web.bind.annotation.PutMapping;
|
370
|
+
|
371
|
+
import org.springframework.web.bind.annotation.RequestMapping;
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
import com.example.demo.domain.Item;
|
376
|
+
|
377
|
+
import com.example.demo.service.ItemService;
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
@Controller
|
382
|
+
|
383
|
+
@RequestMapping("/items")
|
384
|
+
|
385
|
+
public class ItemController {
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
@Autowired
|
390
|
+
|
391
|
+
private ItemService itemService;
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
@GetMapping
|
396
|
+
|
397
|
+
public String index(Model model) {
|
398
|
+
|
399
|
+
model.addAttribute("items", itemService.findAll());
|
400
|
+
|
401
|
+
return "index";
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
@GetMapping("{id}")
|
408
|
+
|
409
|
+
public String show(@PathVariable Long id, Model model) {
|
410
|
+
|
411
|
+
model.addAttribute("item", itemService.findOne(id));
|
412
|
+
|
413
|
+
return "show";
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
@GetMapping("new")
|
420
|
+
|
421
|
+
public String newItem(@ModelAttribute("item") Item item, Model model) {
|
422
|
+
|
423
|
+
return "new";
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
@GetMapping("{id}/edit")
|
430
|
+
|
431
|
+
public String edit(@PathVariable Long id, @ModelAttribute("item") Item item, Model model) {
|
432
|
+
|
433
|
+
model.addAttribute("item", itemService.findOne(id));
|
434
|
+
|
435
|
+
return "edit";
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
@PostMapping
|
442
|
+
|
443
|
+
public String create(@ModelAttribute("item") @Validated Item item, BindingResult result, Model model) {
|
444
|
+
|
445
|
+
if (result.hasErrors()) {
|
446
|
+
|
447
|
+
return "new";
|
448
|
+
|
449
|
+
} else {
|
450
|
+
|
451
|
+
itemService.save(item);
|
452
|
+
|
453
|
+
return "redirect:/items";
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
@PutMapping("{id}")
|
462
|
+
|
463
|
+
public String update(@PathVariable Long id, @ModelAttribute("item") @Validated Item item, BindingResult result, Model model) {
|
464
|
+
|
465
|
+
if (result.hasErrors()) {
|
466
|
+
|
467
|
+
model.addAttribute("item", item);
|
468
|
+
|
469
|
+
return "edit";
|
470
|
+
|
471
|
+
} else {
|
472
|
+
|
473
|
+
item.setId(id);
|
474
|
+
|
475
|
+
itemService.update(item);
|
476
|
+
|
477
|
+
return "redirect:/items";
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
@DeleteMapping("{id}")
|
486
|
+
|
487
|
+
public String delete(@PathVariable Long id) {
|
488
|
+
|
489
|
+
itemService.delete(id);
|
490
|
+
|
491
|
+
return "redirect:/items";
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
```
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
Item.javaのファイル
|
506
|
+
|
507
|
+
```java
|
508
|
+
|
509
|
+
package com.example.demo.domain;
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
public class Item {
|
514
|
+
|
515
|
+
private Long id;
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
@NotBlank(message="商品名を記入してください。")
|
520
|
+
|
521
|
+
private String name;
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
@Min(value=10, message="10以上の数値を入力してください。")
|
526
|
+
|
527
|
+
@Max(value=10000, message="10000以下の数値を入力してください。")
|
528
|
+
|
529
|
+
private float price;
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
@Size(max=50, message="ベーダー名は50文字を超えないでください。")
|
534
|
+
|
535
|
+
private String vendor;
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
public Long getId() {
|
540
|
+
|
541
|
+
return id;
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
public void setId(Long id) {
|
548
|
+
|
549
|
+
this.id = id;
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
public String getName() {
|
556
|
+
|
557
|
+
return name;
|
558
|
+
|
559
|
+
}
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
public void setName(String name) {
|
564
|
+
|
565
|
+
this.name = name;
|
566
|
+
|
567
|
+
}
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
public float getPrice() {
|
572
|
+
|
573
|
+
return price;
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
public void setPrice(float price) {
|
580
|
+
|
581
|
+
this.price = price;
|
582
|
+
|
583
|
+
}
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
public String getVendor() {
|
588
|
+
|
589
|
+
return vendor;
|
590
|
+
|
591
|
+
}
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
public void setVendor(String vendor) {
|
596
|
+
|
597
|
+
this.vendor = vendor;
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
```
|
604
|
+
|
605
|
+
|
606
|
+
|
607
|
+
ItemMapper.javaのファイル
|
608
|
+
|
609
|
+
```java
|
610
|
+
|
611
|
+
package com.example.demo.mapper;
|
612
|
+
|
613
|
+
|
614
|
+
|
615
|
+
import java.util.List;
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
import org.apache.ibatis.annotations.Mapper;
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
import com.example.demo.domain.Item;
|
624
|
+
|
625
|
+
|
626
|
+
|
627
|
+
@Mapper
|
628
|
+
|
629
|
+
public interface ItemMapper {
|
630
|
+
|
631
|
+
List<Item> findAll();
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
Item findOne(Long id);
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
void save(Item item);
|
640
|
+
|
641
|
+
|
642
|
+
|
643
|
+
void update(Item item);
|
644
|
+
|
645
|
+
|
646
|
+
|
647
|
+
void delete(Long id);
|
648
|
+
|
649
|
+
}
|
650
|
+
|
651
|
+
|
652
|
+
|
653
|
+
```
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
ItemService.javaのファイル
|
658
|
+
|
659
|
+
```java
|
660
|
+
|
661
|
+
package com.example.demo.service;
|
662
|
+
|
663
|
+
|
664
|
+
|
665
|
+
import java.util.List;
|
666
|
+
|
667
|
+
|
668
|
+
|
669
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
670
|
+
|
671
|
+
import org.springframework.stereotype.Service;
|
672
|
+
|
673
|
+
import org.springframework.transaction.annotation.Transactional;
|
674
|
+
|
675
|
+
|
676
|
+
|
677
|
+
import com.example.demo.domain.Item;
|
678
|
+
|
679
|
+
import com.example.demo.mapper.ItemMapper;
|
680
|
+
|
681
|
+
|
682
|
+
|
683
|
+
@Service
|
684
|
+
|
685
|
+
public class ItemService {
|
686
|
+
|
687
|
+
|
688
|
+
|
689
|
+
@Autowired
|
690
|
+
|
691
|
+
ItemMapper itemMapper;
|
692
|
+
|
693
|
+
|
694
|
+
|
695
|
+
@Transactional
|
696
|
+
|
697
|
+
public List<Item> findAll() {
|
698
|
+
|
699
|
+
return itemMapper.findAll();
|
700
|
+
|
701
|
+
}
|
702
|
+
|
703
|
+
|
704
|
+
|
705
|
+
@Transactional
|
706
|
+
|
707
|
+
public Item findOne(Long id) {
|
708
|
+
|
709
|
+
return itemMapper.findOne(id);
|
710
|
+
|
711
|
+
}
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
@Transactional
|
716
|
+
|
717
|
+
public void save(Item item) {
|
718
|
+
|
719
|
+
itemMapper.save(item);
|
720
|
+
|
721
|
+
}
|
722
|
+
|
723
|
+
|
724
|
+
|
725
|
+
@Transactional
|
726
|
+
|
727
|
+
public void update(Item item) {
|
728
|
+
|
729
|
+
itemMapper.update(item);
|
730
|
+
|
731
|
+
}
|
732
|
+
|
733
|
+
|
734
|
+
|
735
|
+
@Transactional
|
736
|
+
|
737
|
+
public void delete(Long id) {
|
738
|
+
|
739
|
+
itemMapper.delete(id);
|
740
|
+
|
741
|
+
}
|
742
|
+
|
743
|
+
|
744
|
+
|
745
|
+
}
|
746
|
+
|
747
|
+
|
748
|
+
|
749
|
+
```
|