以下のリンクに記載されているコードに関して質問です。
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/
▪️ディレクトリ構造
▪️疑問点
ItemController.javaファイルで@RequestMapping("/items")と記述があるが、/itemsのファイルは生成されていない。なぜ、生成されていないファイルに遷移することができるのか?
また、Index.htmlに
java
1package com.example.demo.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.validation.BindingResult; 7import org.springframework.validation.annotation.Validated; 8import org.springframework.web.bind.annotation.DeleteMapping; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.ModelAttribute; 11import org.springframework.web.bind.annotation.PathVariable; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.PutMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import com.example.demo.domain.Item; 17import com.example.demo.service.ItemService; 18 19@Controller 20@RequestMapping("/items") 21public class ItemController { 22 23 @Autowired 24 private ItemService itemService; 25 26 @GetMapping 27 public String index(Model model) { 28 model.addAttribute("items", itemService.findAll()); 29 return "index"; 30 } 31 32 @GetMapping("{id}") 33 public String show(@PathVariable Long id, Model model) { 34 model.addAttribute("item", itemService.findOne(id)); 35 return "show"; 36 } 37 38 @GetMapping("new") 39 public String newItem(@ModelAttribute("item") Item item, Model model) { 40 return "new"; 41 } 42 43 @GetMapping("{id}/edit") 44 public String edit(@PathVariable Long id, @ModelAttribute("item") Item item, Model model) { 45 model.addAttribute("item", itemService.findOne(id)); 46 return "edit"; 47 } 48 49 @PostMapping 50 public String create(@ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 51 if (result.hasErrors()) { 52 return "new"; 53 } else { 54 itemService.save(item); 55 return "redirect:/items"; 56 } 57 } 58 59 @PutMapping("{id}") 60 public String update(@PathVariable Long id, @ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 61 if (result.hasErrors()) { 62 model.addAttribute("item", item); 63 return "edit"; 64 } else { 65 item.setId(id); 66 itemService.update(item); 67 return "redirect:/items"; 68 } 69 } 70 71 @DeleteMapping("{id}") 72 public String delete(@PathVariable Long id) { 73 itemService.delete(id); 74 return "redirect:/items"; 75 } 76}
index.htmlのファイル
HTML
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <link rel="stylesheet" href="/css/bootstrap.min.css" /> 7 <title>商品一覧</title> 8</head> 9<body> 10 <nav class="navbar navbar-inverse"> 11 <div class="container"> 12 <div class="navbar-header"> 13 <a class="navbar-brand" href="/items">商品管理デモ</a> 14 </div> 15 </div> 16 </nav> 17 <div class="container"> 18 <div class="card card-primary mb-3"> 19 <div class="card-header"> 20 <h5 class="card-title">商品リスト<a href="/items/new" class="btn btn-success float-right">新規</a></h5> 21 </div> 22 <div class="card-body" th:if="!${items.size()}"> 23 <p>商品がありません。</p> 24 </div> 25 <table class="table table-striped" th:if="${items.size()}"> 26 <thead> 27 <tr> 28 <th style="width: 10%">ID</th> 29 <th style="width: 30%">商品名</th> 30 <th style="width: 10%">価格</th> 31 <th style="width: 20%">ベンダー</th> 32 <th style="width: 30%"></th> 33 </tr> 34 </thead> 35 <tbody> 36 <tr th:each="item:${items}" th:object="${item}"> 37 <td th:text="*{id}"></td> 38 <td th:text="*{name}"></td> 39 <td th:text="*{price}"></td> 40 <td th:text="*{vendor}"></td> 41 <td class="float-right"> 42 <form th:action="@{/items/{id}(id=*{id})}" th:method="delete"> 43 <a class="btn btn-primary" th:href="@{/items/{id}(id=*{id})}">詳細</a> 44 <a class="btn btn-primary" th:href="@{/items/{id}/edit(id=*{id})}">変更</a> 45 <button class="btn btn-primary">削除</button> 46 </form> 47 </td> 48 </tr> 49 </tbody> 50 </table> 51 </div> 52 </div> 53 <script src="/js/jquery-3.4.1.min"></script> 54 <script src="/js/bootstrap.min.js"></script> 55</body> 56</html>
Application.javaのファイル
java
1package com.example.demo; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class Application { 8 9 public static void main(String[] args) { 10 SpringApplication.run(Application.class, args); 11 } 12 13} 14
ItemController.javaのファイル
java
1package com.example.demo.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.validation.BindingResult; 7import org.springframework.validation.annotation.Validated; 8import org.springframework.web.bind.annotation.DeleteMapping; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.ModelAttribute; 11import org.springframework.web.bind.annotation.PathVariable; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.PutMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import com.example.demo.domain.Item; 17import com.example.demo.service.ItemService; 18 19@Controller 20@RequestMapping("/items") 21public class ItemController { 22 23 @Autowired 24 private ItemService itemService; 25 26 @GetMapping 27 public String index(Model model) { 28 model.addAttribute("items", itemService.findAll()); 29 return "index"; 30 } 31 32 @GetMapping("{id}") 33 public String show(@PathVariable Long id, Model model) { 34 model.addAttribute("item", itemService.findOne(id)); 35 return "show"; 36 } 37 38 @GetMapping("new") 39 public String newItem(@ModelAttribute("item") Item item, Model model) { 40 return "new"; 41 } 42 43 @GetMapping("{id}/edit") 44 public String edit(@PathVariable Long id, @ModelAttribute("item") Item item, Model model) { 45 model.addAttribute("item", itemService.findOne(id)); 46 return "edit"; 47 } 48 49 @PostMapping 50 public String create(@ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 51 if (result.hasErrors()) { 52 return "new"; 53 } else { 54 itemService.save(item); 55 return "redirect:/items"; 56 } 57 } 58 59 @PutMapping("{id}") 60 public String update(@PathVariable Long id, @ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 61 if (result.hasErrors()) { 62 model.addAttribute("item", item); 63 return "edit"; 64 } else { 65 item.setId(id); 66 itemService.update(item); 67 return "redirect:/items"; 68 } 69 } 70 71 @DeleteMapping("{id}") 72 public String delete(@PathVariable Long id) { 73 itemService.delete(id); 74 return "redirect:/items"; 75 } 76} 77
Item.javaのファイル
java
1package com.example.demo.domain; 2 3public class Item { 4 private Long id; 5 6 @NotBlank(message="商品名を記入してください。") 7 private String name; 8 9 @Min(value=10, message="10以上の数値を入力してください。") 10 @Max(value=10000, message="10000以下の数値を入力してください。") 11 private float price; 12 13 @Size(max=50, message="ベーダー名は50文字を超えないでください。") 14 private String vendor; 15 16 public Long getId() { 17 return id; 18 } 19 20 public void setId(Long id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public float getPrice() { 33 return price; 34 } 35 36 public void setPrice(float price) { 37 this.price = price; 38 } 39 40 public String getVendor() { 41 return vendor; 42 } 43 44 public void setVendor(String vendor) { 45 this.vendor = vendor; 46 } 47}
ItemMapper.javaのファイル
java
1package com.example.demo.mapper; 2 3import java.util.List; 4 5import org.apache.ibatis.annotations.Mapper; 6 7import com.example.demo.domain.Item; 8 9@Mapper 10public interface ItemMapper { 11 List<Item> findAll(); 12 13 Item findOne(Long id); 14 15 void save(Item item); 16 17 void update(Item item); 18 19 void delete(Long id); 20} 21
ItemService.javaのファイル
java
1package com.example.demo.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8 9import com.example.demo.domain.Item; 10import com.example.demo.mapper.ItemMapper; 11 12@Service 13public class ItemService { 14 15 @Autowired 16 ItemMapper itemMapper; 17 18 @Transactional 19 public List<Item> findAll() { 20 return itemMapper.findAll(); 21 } 22 23 @Transactional 24 public Item findOne(Long id) { 25 return itemMapper.findOne(id); 26 } 27 28 @Transactional 29 public void save(Item item) { 30 itemMapper.save(item); 31 } 32 33 @Transactional 34 public void update(Item item) { 35 itemMapper.update(item); 36 } 37 38 @Transactional 39 public void delete(Long id) { 40 itemMapper.delete(id); 41 } 42 43} 44
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。