Home>

I am developing a web application with Spring Boot.
I want to implement the search function, but I cannot implement it well.
I'm getting a 404 error, but I'm not sure why.

If there is someone who understands the problem area, I would like to ask a professor.

Referenced site

404 error

Applicable source code
<! DOCTYPE html PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns: th = "http://www.thymeleaf.org"
      xmlns: layout = "http://www.ultraq.net.nz/thymeleaf/layout">        <link th: href = "@ {/ webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" rel = "stylesheet"></link>    <script th: src = "@ {/ webjars/jquery/1.11.1/jquery.min.js}"></script>    <script th: src = "@ {/ webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script><head>   <meta http-equiv = "Content-Type" content = "text/html;charset = UTF-8"></meta></head>  <body>                     Product search screen               <form method = "post" th: action = "@ {/ search}" th: object = "${searchForm}">      <table>        <tr><td>Product code:<input type = "text" name = "productCode" th: value = "${productCode}" /></td></tr>        <tr><td>Product name:<input type = "text" name = "productName" th: value = "${productName}" /></td></tr>        <tr><td>Price:<input type = "text" name = "price" th: value = "${price}" /></td></tr>        <tr><td><input type = "submit" value = "Search" /></td></tr>      </table>    </form>     0}"><label th: text = "${resultSize}"></label>cases     <table border = "1" th: if = "${resultSize>0}">      <tr>        <td>Product code</td>        <td>Product name</td>        <td>price</td>      </tr>      <tr th: each = "data: ${result}">        <td th: text = "${product.productCode}" />        <td th: text = "${product.productName}" />        <td th: text = "${product.price}" />      </tr>    </table>      </body></html>
<! DOCTYPE html PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns: th = "http://www.thymeleaf.org"
    xmlns: layout = "http://www.ultraq.net.nz/thymeleaf/layout"><head><meta http-equiv = "Content-Type" content = "text/html;charset = UTF-8"></meta><!-Bootstrap settings-><link th: href = "@ {/ webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}"
    rel = "stylesheet"></link><script th: src = "@ {/ webjars/jquery/1.11.1/jquery.min.js}"></script><script th: src = "@ {/ webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script><!-Read CSS-><link th: href = "@ {/ css/home.css}" rel = "stylesheet"></link><title>Home</title></head><!-===== header (navigation bar) =====-><body>    <!-===== Header (Navigation Bar) =====->    <nav>                         WEB application practice                 <!-===== Sidebar =====->                                        <ul>                    <li role = "presentation">Product management                     </li>                    </ul>                    <ul>                    <li role = "presentation">Product search                     </li>                </ul>                    <ul>                    <li role = "presentation">Product registration                     </li>                </ul>                            <!-===== content =====->                                                                 </html>
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.domain.model.Product;
import com.example.demo.domain.repository.ProductDao;
import com.example.demo.domain.service.ProductService;
public class SearchController {
    @Autowired
    private ProductDao ProductDao;
    @Autowired
    private ProductService productService;
    @GetMapping ("search")
    public String find () {
        return "search";
    }
    // Search screen GET method
    @GetMapping ("/ search")
    public String getHome (Model model) {
        // Register the character string to display the search screen in the content partmodel.addAttribute ("contents", "/ search :: search_contents");
        return "homeLayout";
    }
    @RequestMapping (value = "search", method = RequestMethod.POST)
    public ModelAndView search (ModelAndView mav,
            @RequestParam ("productCode") String productCode,
            @RequestParam ("productName") String productName,
            @RequestParam ("price") int price) {
        mav.setViewName ("search");
        mav.addObject ("productCode", productCode);
        mav.addObject ("productName", productName);
        mav.addObject ("price", price);
        List<Product>result = ProductService.search (productCode, productName, price);
        mav.addObject ("result", result);
        mav.addObject ("resultSize", result.size ());
        return mav;
    }
}
Applicable source code
package com.example.demo.domain.repository;
import java.util.List;
import org.springframework.dao.DataAccessException;
import com.example.demo.domain.model.Product;
public interface ProductDao {
    // Insert 1 record in Product table.
    public int insertOne (Product product) throws DataAccessException;
    // Get 1 record of Product table
    public Product selectOne (String productCode) throws DataAccessException;
    // Get all the data in the Product table.
    public List<Product>selectMany () throws DataAccessException;
    // Update 1 item in Product table.
    public int updateOne (Product product) throws DataAccessException;
    // Delete one Product table.
    public int deleteOne (String productCode) throws DataAccessException;
    public static List<Product>findAll () {
        return null;
    }
}
Applicable source code
package com.example.demo.domain.model;
import lombok.Data;
@Data
public class SignupForm {
    private String productCode;
    private String productName;
    private int price;
}
Applicable source code
package com.example.demo.domain.jdbc;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.example.demo.domain.model.Product;
import com.example.demo.domain.repository.ProductDao;
@Repository
public class ProductDaoJdbcImpl implements ProductDao {
    @Autowired
    JdbcTemplate jdbc;// Insert one data from Product table
    @Override
    public int insertOne (Product product) throws DataAccessException {
        // Register 1 case
        int rowNumber = jdbc.update ("insert into product (productCode,"
                + "productName,"
                + "price)"
                + "values ​​(?,?,?)", product.getProductCode (), product.getProductName (), product.getPrice ());
        return rowNumber;
    }
    @Override
    // Get 1 record of Product table
    public Product selectOne (String productCode) throws DataAccessException {
        Map<String, Object>map = jdbc.queryForMap ("select * from product" + "where productCode =?", ProductCode);
        // Variable for returning results
        Product product = new Product ();
        // Set the acquired data to the variable for returning results
        product.setProductCode ((String) map.get ("productCode"));// product code
        product.setProductName ((String) map.get ("productName"));// product name
        product.setPrice ((int) map.get ("price"));// price
        return product;
    }
    @Override
    // Get all the data in the Product table.
    public List<Product>selectMany () throws DataAccessException {
        List<Map<String, Object >>getList = jdbc.queryForList ("SELECT * FROM product");
        // Variable for returning results
        List<Product>productList = new ArrayList<Product>();
        // Store the acquired data in the List for returning results
        for (Map<String, Object>map: getList) {
            // Create a Product instance
            Product product = new Product ();
            // Set the acquired data in the Product instance
            product.setProductCode ((String) map.get ("productCode"));// product code
            product.setProductName ((String) map.get ("productName"));// Product ID
            product.setPrice ((Integer) map.get ("price"));// product ID
            // Add to List for returning results
            productList.add (product);
        }
        return productList;
    }
    @Override
    // Update 1 item in Product table.
    public int updateOne (Product product) throws DataAccessException {
        // 1 update
        int rowNumber = jdbc.update ("update product"
                + "set"
                + "productName =?,"
                + "price =?"
                + "where productCode =?", product.getProductName (), product.getPrice (), product.getProductCode ());
        return rowNumber;
    }
    @Override
    // Delete one Product table.
    public int deleteOne (String productCode) throws DataAccessException {
        // Delete one
        int rowNumber = jdbc.update ("delete from product where productCode =?", productCode);
        return rowNumber;
    }
}
  • Answer # 1

    Solved by adding @Controller annotation to controller class.