質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.69%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

1回答

68122閲覧

spring boot でのthymeleafでの値の受渡し時にエラー

TaSa

総合スコア1

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

1クリップ

投稿2020/05/26 06:03

前提・実現したいこと

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

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 4<head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta> 6</head> 7 <body> 8<div th:fragment="search_contents"> 9 <div class="page-header"> 10 <h1>商品検索画面</h1> 11 </div> 12 <form method="post" action="search"> 13 <table> 14 <tr><td>商品コード : <input type="text" class="form-control" id="productCode" name="productCode" th:value="${productCode}"/></td></tr> 15 <tr><td>商品名 : <input type="text" class="form-control" id="productName" name="productName" th:value="${productName}"/></td></tr> 16 <tr> 17 <td>価格帯 : <input type="text" class="form-control" id="priceFrom" name="priceFrom" th:value="${priceFrom}"/> 18 ~ <input type="text" class="form-control" id="priceTo" name="priceTo" th:value="${priceTo}"/></td> 19 </tr> 20 <tr><td><input type="submit" value="検索"/></td></tr> 21 </table> 22 </form> 23 <div th:if="${resultSize > 0}"><label th:text="${resultSize}"></label>件</div> 24 <table border="1" th:if="${resultSize > 0}"> 25 <tr> 26 <td>商品ID</td> 27 <td>商品名</td> 28 <td>価格</td> 29 </tr> 30 <tr th:each="data : ${result}"> 31 <td th:text="${data.productCode}"/> 32 <td th:text="${data.productName}"/> 33 <td th:text="${data.price}"/> 34 </tr> 35 </table> 36 </div> 37 </body> 38</html>

controller

1package com.example.demo.controller; 2 3import java.math.BigDecimal; 4import java.util.List; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.context.annotation.ComponentScan; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.web.bind.annotation.GetMapping; 11import org.springframework.web.bind.annotation.RequestMapping; 12import org.springframework.web.bind.annotation.RequestMethod; 13import org.springframework.web.bind.annotation.RequestParam; 14import org.springframework.web.servlet.ModelAndView; 15 16import com.example.demo.domain.model.Product; 17import com.example.demo.domain.service.ProductService; 18 19@ComponentScan 20@Controller 21@RequestMapping("/search") 22public class SearchController { 23 24 private static final String VIEW = "/search"; 25 26 @Autowired 27 private ProductService service; 28 29 @RequestMapping(method = RequestMethod.GET) 30 public String index() { 31 return VIEW; 32 } 33 34 //検索画面のGET用メソッド 35 @GetMapping("/search") 36 public String getSearch(Model model) { 37 //コンテンツ部分に検索画面を表示するための文字列を登録 38 model.addAttribute("contents", "/search :: search_contents"); 39 40 return "homeLayout"; 41 } 42 43 @RequestMapping(method = RequestMethod.POST) 44 public ModelAndView search(ModelAndView mav, 45 @RequestParam("productCode") String productCode, 46 @RequestParam("productName") String productName, 47 @RequestParam("priceFrom") BigDecimal priceFrom, 48 @RequestParam("priceTo") BigDecimal priceTo) { 49 mav.setViewName("search"); 50 mav.addObject("productCode", productCode); 51 mav.addObject("productName", productName); 52 mav.addObject("priceFrom", priceFrom); 53 mav.addObject("priceTo", priceTo); 54 List<Product> result = service.search(productCode, productName, priceFrom, priceTo); 55 mav.addObject("result", result); 56 mav.addObject("resultSize", result.size()); 57 return mav; 58 } 59 60} 61

service

1(中略) 2 public List<Product> search(String productCode, String productName, BigDecimal priceFrom, BigDecimal priceTo) { 3 List<Product> result; 4 if ("".equals(productCode) && "".equals(productName) && priceFrom == null && priceTo == null) { 5 result = dao.findAll(); 6 } else { 7 result = repositoryCustom.search(productCode, productName, priceFrom, priceTo); 8 } 9 return result; 10 } 11

model

1package com.example.demo.domain.model; 2 3import javax.persistence.Column; 4import javax.persistence.Table; 5 6import lombok.Data; 7 8@Data 9@Table(name = "product") 10public class Product { 11 @Column(name = "productCode") 12 private String productCode; //商品コード 13 14 @Column(name = "productName") 15 private String productName; //商品名 16 17 private int price; //値段 18}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

下記のような質問は推奨されていません。

  • 質問になっていない投稿
  • スパムや攻撃的な表現を用いた投稿

適切な質問に修正を依頼しましょう。

また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。

m.ts10806

2020/05/26 20:05

>html PUBLIC "-//W3C//DTD HTML 4.01 何を参考にしているか知らないですが、HTML4時代の書き方をしているものを参考にすべきではないと思います。
Yasumichi

2020/06/04 11:32

GET で発生しているなら、 index() で "result" を設定していないのが、気になります。(同じビューを返しているので) 未検証ですけど。

回答1

0

Ensure that the attribute names in mav.addObject match the variable names used in your Thymeleaf template. In your case, make sure that you have attributes like productCode, productName, priceFrom, priceTo, result, and resultSize in your ModelAndView.Double-check that the variable names you're using in your Thymeleaf template (data.productCode, MyCCPay data.productName, data.price) match the actual field names in your Product class. Ensure they have proper getters in the Product class.Confirm that your Thymeleaf template file is located in the correct directory (src/main/resources/templates) and that its name matches the one you're referring to in your controller ("search").Thymeleaf is case-sensitive, so ensure that you're using the correct case for variable names and attributes.

Here's a quick example of how your ModelAndView attributes should match your Thymeleaf template variables:

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());

And in your Thymeleaf template, you should use these attributes like this:

<td th:text="${productCode}"/> <td th:text="${productName}"/> <td th:text="${price}"/>

Double-checking these points should help you resolve the error and correctly display the data in your template.

投稿2023/09/07 09:45

Hustiought

総合スコア4

下記のような回答は推奨されていません。

  • 質問の回答になっていない投稿
  • スパムや攻撃的な表現を用いた投稿

このような回答には修正を依頼しましょう。

また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.69%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

同じタグがついた質問を見る

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。