前提・実現したいこと
Spring bootにて、登録したデータの検索機能を実装しています。
検索条件を入力すると値の受け渡しがうまくいっていない?ようでエラーが発生してしまいます。
対処法として考えるべき点があれば教えていただきたいです。
初心者の為、いつもどうしようもない質問すみません。
こちらのサイトを参考にしています。
https://dev.classmethod.jp/articles/spring-data-jpa_search/
発生している問題・エラーメッセージ
An error happened during template parsing (template: "class path resource [templates/search.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/search.html]") Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "data.productCode" (template: "search" - line 31, col 13) Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "data.productCode" (template: "search" - line 31, col 13) Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'productCode' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?
該当のソースコード
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> </head> <body> <div th:fragment="search_contents"> <div class="page-header"> <h1>商品検索画面</h1> </div> <form method="post" action="search"> <table> <tr><td>商品コード : <input type="text" class="form-control" id="productCode" name="productCode" th:value="${productCode}"/></td></tr> <tr><td>商品名 : <input type="text" class="form-control" id="productName" name="productName" th:value="${productName}"/></td></tr> <tr> <td>価格帯 : <input type="text" class="form-control" id="priceFrom" name="priceFrom" th:value="${priceFrom}"/> ~ <input type="text" class="form-control" id="priceTo" name="priceTo" th:value="${priceTo}"/></td> </tr> <tr><td><input type="submit" value="検索"/></td></tr> </table> </form> <div th:if="${resultSize > 0}"><label th:text="${resultSize}"></label>件</div> <table border="1" th:if="${resultSize > 0}"> <tr> <td>商品ID</td> <td>商品名</td> <td>価格</td> </tr> <tr th:each="data : ${result}"> <td th:text="${data.productCode}"/> <td th:text="${data.productName}"/> <td th:text="${data.price}"/> </tr> </table> </div> </body> </html>
controller
package com.example.demo.controller; import java.math.BigDecimal; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Controller; 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.service.ProductService; @ComponentScan @Controller @RequestMapping("/search") public class SearchController { private static final String VIEW = "/search"; @Autowired private ProductService service; @RequestMapping(method = RequestMethod.GET) public String index() { return VIEW; } //検索画面のGET用メソッド @GetMapping("/search") public String getSearch(Model model) { //コンテンツ部分に検索画面を表示するための文字列を登録 model.addAttribute("contents", "/search :: search_contents"); return "homeLayout"; } @RequestMapping(method = RequestMethod.POST) public ModelAndView search(ModelAndView mav, @RequestParam("productCode") String productCode, @RequestParam("productName") String productName, @RequestParam("priceFrom") BigDecimal priceFrom, @RequestParam("priceTo") BigDecimal priceTo) { mav.setViewName("search"); mav.addObject("productCode", productCode); mav.addObject("productName", productName); mav.addObject("priceFrom", priceFrom); mav.addObject("priceTo", priceTo); List<Product> result = service.search(productCode, productName, priceFrom, priceTo); mav.addObject("result", result); mav.addObject("resultSize", result.size()); return mav; } }
service
(中略) public List<Product> search(String productCode, String productName, BigDecimal priceFrom, BigDecimal priceTo) { List<Product> result; if ("".equals(productCode) && "".equals(productName) && priceFrom == null && priceTo == null) { result = dao.findAll(); } else { result = repositoryCustom.search(productCode, productName, priceFrom, priceTo); } return result; }
model
package com.example.demo.domain.model; import javax.persistence.Column; import javax.persistence.Table; import lombok.Data; @Data @Table(name = "product") public class Product { @Column(name = "productCode") private String productCode; //商品コード @Column(name = "productName") private String productName; //商品名 private int price; //値段 }
まだ回答がついていません
会員登録して回答してみよう