Home>

I have a question regarding the code provided in the link below.
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 /

▪️ Directory structure

▪️ Questions
There is a description of @RequestMapping ("/ items") in the ItemController.java file, but the/items file has not been generated. Why is it possible to transition to an ungenerated file?
Also in Index.html
There is a description

,
What is items.size ()? I don't understand if there is this description because there is no description to create Items and instance.
▪️ Source code
ItemController.java

package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.domain.Item;
import com.example.demo.service.ItemService;
@Controller
@RequestMapping ("/ items")
public class ItemController {
  @Autowired
  private ItemService itemService;
  @GetMapping
  public String index (Model model) {
    model.addAttribute ("items", itemService.findAll ());
    return "index";
  }
  @GetMapping ("{id}")
  public String show (@PathVariable Long id, Model model) {
    model.addAttribute ("item", itemService.findOne (id));
    return "show";
  }
  @GetMapping ("new")
  public String newItem (@ModelAttribute ("item") Item item, Model model) {
    return "new";
  }
  @GetMapping ("{id}/edit")
  public String edit (@PathVariable Long id, @ModelAttribute ("item") Item item, Model model) {
    model.addAttribute ("item", itemService.findOne (id));
    return "edit";
  }
  @PostMapping
  public String create (@ModelAttribute ("item") @Validated Item item, BindingResult result, Model model) {
    if (result.hasErrors ()) {
      return "new";
    } else {
      itemService.save (item);
      return "redirect:/items";
    }
  }
  @PutMapping ("{id}")
  public String update (@PathVariable Long id, @ModelAttribute ("item") @Validated Item item, BindingResult result, Model model) {
    if (result.hasErrors ()) {
      model.addAttribute ("item", item);
      return "edit";
    } else {
      item.setId (id);
      itemService.update (item);
      return "redirect:/items";
    }
  }
  @DeleteMapping ("{id}")
  public String delete (@PathVariable Long id) {
    itemService.delete (id);return "redirect:/items";
  }
}

index.html file

<! DOCTYPE html><html><head><meta charset = "UTF-8"><meta name = "viewport" content = "width = device-width, initial-scale = 1"><link rel = "stylesheet" href = "/ css/bootstrap.min.css" /><title>Product list</title></head><body><nav>                 Product management demo             </nav>                       Product list  New                          <p>There are no products.</p>            <table th: if = "${items.size ()}">      <thead>          <tr>            <th>ID</th>            <th>Product name</th>            <th>Price</th>            <th>Vendor</th>            <th></th>          </tr>      </header>      <tbody>          <tr th: each = "item: ${items}" th: object = "${item}">            <td th: text = "* {id}"></td>            <td th: text = "* {name}"></td>            <td th: text = "* {price}"></td>            <td th: text = "* {vendor}"></td>            <td>              <form th: action = "@ {/ items/{id} (id = * {id})}" th: method = "delete">                   Details                      Change               <button>Delete</button>          </form>            </td>          </tr>      </tbody>    </table>      <script src = "/ js/jquery-3.4.1.min"></script><script src = "/ js/bootstrap.min.js"></script></body></html>


Application.java file

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main (String [] args) {
        SpringApplication.run (Application.class, args);
    }
}

ItemController.java file

package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.domain.Item;
import com.example.demo.service.ItemService;
@Controller
@RequestMapping ("/ items")
public class ItemController {
  @Autowired
  private ItemService itemService;
  @GetMapping
  public String index (Model model) {
    model.addAttribute ("items", itemService.findAll ());
    return "index";
  }
  @GetMapping ("{id}")
  public String show (@PathVariable Long id, Model model) {
    model.addAttribute ("item", itemService.findOne (id));
    return "show";
  }
  @GetMapping ("new")
  public String newItem (@ModelAttribute ("item") Item item, Model model) {
    return "new";
  }
  @GetMapping ("{id}/edit")
  public String edit (@PathVariable Long id, @ModelAttribute ("item") Item item, Model model) {
    model.addAttribute ("item", itemService.findOne (id));
    return "edit";
  }
  @PostMapping
  public String create (@ModelAttribute ("item") @Validated Item item, BindingResult result, Model model) {
    if (result.hasErrors ()) {
      return "new";
    } else {
      itemService.save (item);
      return "redirect:/items";
    }
  }
  @PutMapping ("{id}")
  public String update (@PathVariable Long id, @ModelAttribute ("item") @Validated Item item, BindingResult result, Model model) {
    if (result.hasErrors ()) {
      model.addAttribute ("item", item);
      return "edit";
    } else {
      item.setId (id);
      itemService.update (item);
      return "redirect:/items";
    }
  }
  @DeleteMapping ("{id}")
  public String delete (@PathVariable Long id) {
    itemService.delete (id);
    return "redirect:/items";
  }
}

Item.java file

package com.example.demo.domain;
public class Item {
    private Long id;
    @NotBlank (message = "Please enter the product name.")
    private String name;
    @Min (value = 10, message = "Please enter a number greater than or equal to 10")
    @Max (value = 10000, message = "Please enter a number below 10000.")
    private float price;@Size (max = 50, message = "Vader name must not exceed 50 characters.")
    private String vendor;
    public Long getId () {
        return id;
    }
    public void setId (Long id) {
        this.id = id;
    }
    public String getName () {
        return name;
    }
    public void setName (String name) {
        this.name = name;
    }
    public float getPrice () {
        return price;
    }
    public void setPrice (float price) {
        this.price = price;
    }
    public String getVendor () {
        return vendor;
    }
    public void setVendor (String vendor) {
        this.vendor = vendor;
    }
}

ItemMapper.java file

package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.domain.Item;
@Mapper
public interface ItemMapper {
  List<Item>findAll ();
  Item findOne (Long id);
  void save (Item item);
  void update (Item item);
  void delete (Long id);
}

ItemService.java file

package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.domain.Item;
import com.example.demo.mapper.ItemMapper;
@Service
public class ItemService {
    @Autowired
    ItemMapper itemMapper;
    @Transactional
    public List<Item>findAll () {
        return itemMapper.findAll ();
    }
    @Transactional
    public Item findOne (Long id) {
        return itemMapper.findOne (id);
    }
    @Transactional
    public void save (Item item) {
        itemMapper.save (item);
    }
    @Transactional
    public void update (Item item) {
        itemMapper.update (item);
    }
    @Transactional
    public void delete (Long id) {
        itemMapper.delete (id);
    }
}
  • Answer # 1

    You can find out by looking at "routing".
    The actual implementation method differs depending on the language and framework, but the concept is almost the same.
    Just set "If you receive a request to/items, this controller will process it" and it works as it is.

    If you remember only html first, you will think that "Web server is displaying existing files", but you can use files that do not exist.
    If it's just an html file
    The Web server is performing the process of "returning index.html as it is (if it exists) when a request to index.html is received". 404 error if it does not exist.
    If you do it in Java instead of a web server
    "Request to XX" and "Return XX" can be changed dynamically.
    I think it's okay to have an image like this.