【環境】
使用言語:JAVA
開発環境:Spring Tool Suite、XAMPP(phpMyAdmin)
DB:MariaDB
【前提】
Spring Tool Suiteを使用してJAVAのWebアプリを開発しています。
LicenseクラスにLocalDate型のフィールド「expiration」が存在し、フォームから入力した値をDBに保存しています。
このDBは、XAMPPのphpMyAdminで管理しています。
DBはMariaDBを使用しています。
上記expirationを含むテーブルをThymeleafを使用し
index.htmlに表示しています。
【実現したいこと】
index.htmlにアクセスした際、その時点の日にちよりexpirationの日付が前であった場合、クラス「bg-red」をタグに付与して該当する行のみ赤く表示したい。
より具体的には、以下になります。
1.th:classappendの条件にLocalDate型のフィールド比較を指定できるのか?
2.1が不可能である場合、どういったアプローチが考えられるか?
【試したこと】
java
1コード 2[index.html] 3```<tbody> 4 <tr th:each="license : ${licenses}" th:object="${license}" th:classappend="${license.id.equals('123456789')} ? 'bg-red'"> 5 <td th:text="*{company}"></td> 6 <td th:text="*{id}"></td> 7 <td th:text="*{licensekey}" sec:authorize="hasRole('EDIT')"></td> 8 <td th:text="*{#numbers.formatInteger(price,3,'COMMA')}"></td> 9 <td th:text="*{expiration}"></td></tr> 10</tbody> 11 12[コントローラークラス] 13 @GetMapping("/") 14 public String home(Model model,Model timeModel) { 15 List<License> licenses = licenseRepository.findAll(); 16 model.addAttribute("licenses", licenses); 17 timeModel.addAttribute("nowDate", new Date()); 18 return "index"; 19 }
index.htmlの記述で、idフィールドの値が'123456789'といった具合に、指定フィールドに固定値が入力されている場合にクラスを付与することは実現できました。
しかし、th:classappend内において日付を比較する方法が分かりません。
th:classappend="${license.expiration lt ('nowDate') ? 'bg-red'"}
等と記述してもエラーになります。
th:ifであればできるようにも思うのですが、これはbool値に応じてタグ内を表示する/しないを切り替えるものなので、クラスを付与したものと付与していないものを両方を出す用途には使えないと理解しています。
th:classappendにてLocalDate型のフィールドを比較するといった条件は記述できますでしょうか。
自分なりに色々検索してみたのですがどうにも分からず、
お力添えいただけると幸いです。
th:classappendに拘っているわけではないので、違ったアプローチであっても解決方法であったり、調べ方のご助言をいただけると大変助かります。
JAVAもThymeleafも素人で、当サイトでの質問も初めてですので、不足している情報がある、聞き方がよくない等ございましたらご指摘ください。
2022/5/7追記
エラーについて誤り及び追記がございました。
th:classappend="${license.expiration lt ('nowDate') ? 'bg-red'"}
等と記述してもエラーになります。
この記述ではエラーになりません。そのかわり、全ての行にクラスが付与され赤くなってしまいます。これは、nowDateが単なる文字列になっているため比較ができていないと理解しています。
th:classappend="${license.expiration lt (nowDate) ? 'bg-red'"}
このように修正した際にエラーが出ていました。
エラーメッセージを追記します。全文載せると文字数制限を超過するため、「at org.thymeleaf…」等の行は省略しました。
エラーメッセージ ```Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat May 07 14:39:43 JST 2022 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/index.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index.html]") (省略) Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "license.id lt (nowDate)" (template: "index" - line 58, col 64) (省略) Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "license.id lt (nowDate)" (template: "index" - line 58, col 64) (省略) Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1013E: Cannot compare instances of class java.lang.String and class java.util.Date (省略) Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
回答1件
あなたの回答
tips
プレビュー