スプリングフレームワークを使用してDBから取得した値を画面上に表示しようと試みているのですがWhitelabel Error Page となってうまく表示されません。
ステータスは500となっています。解決方法を調べたのですがうまくいかないためご助力いただきたいです
以下はエラー内容となります
022-05-27 14:54:34.035 ERROR 16228 --- [nio-8180-exec-6] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8180-exec-6] Exception processing template "user/list": Exception evaluating SpringEL expression: "ID" (template: "user/list" - line 19, col 15) org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "ID" (template: "user/list" - line 19, col 15) at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292) ~[thymeleaf-spring5-3.0.15.RELEASE.jar:3.0.15.RELEASE] Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ID' cannot be found on object of type 'com.example.entity.User' - maybe not public or not valid? 2022-05-27 14:54:34.043 ERROR 16228 --- [nio-8180-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "ID" (template: "user/list" - line 19, col 15)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ID' cannot be found on object of type 'com.example.entity.User' - maybe not public or not valid?
以下ソースコードです
controllerクラス
package com.example.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import com.example.entity.User; import com.example.service.UserService; @Controller public class DBgetValue { /** * ユーザー情報 Service */ @Autowired private UserService userService; /** * ユーザー情報一覧画面を表示 * @param model Model * @return ユーザー情報一覧画面 */ @GetMapping(value = "/user/list") public String displayList(Model model) { List<User> userlist = userService.searchAll();//userServiceクラスのメソッドを呼び出している model.addAttribute("userlist", userlist);//サービスクラスから取得した内容を設定している return "user/list";//表示する画面を設定している(model.addAttributeで画面に渡す情報を指定してこの画面を表示する) } }
サービスクラス
package com.example.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.entity.User; import com.example.repository.UserRepository; /** * ユーザー情報 Service */ @Service public class UserService { /** * ユーザー情報 Repository */ @Autowired private UserRepository userRepository; /** * ユーザー情報 全検索 * @return 検索結果 */ public List<User> searchAll() { return userRepository.findAll();//データベースから全件取得してくるのでリポジトリクラスに設定されているfindAllメソッドを呼び出してリターン //findAllでエンティティに詰めて返却している } }
エンティティクラス
package com.example.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * ユーザー情報 Entity */ @Entity // ここでエンティティクラスだと明示している @Table(name = "DBSAMPLE") // SELECT対象のテーブル名を記載 public class User { @Id @Column(name = "ID")//DB上のどのカラムなのかを記載 @GeneratedValue(strategy = GenerationType.IDENTITY) private String ID; @Column(name = "NAME") private String NAME; @Column(name = "SEX") private String SEX; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getNAME() { return NAME; } public void setNAME(String nAME) { NAME = nAME; } public String getSEX() { return SEX; } public void setSEX(String sEX) { SEX = sEX; } }
リポジトリクラス
package com.example.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.entity.User; /** * ユーザー情報 Repository */ @Repository public interface UserRepository extends JpaRepository<User, String> { //ここの親クラスにfindAllメソッドが存在しているので全件取得することができる }
画面HTML
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head th:replace="common/head :: head_fragment(title = 'ユーザー情報一覧', scripts = ~{::script}, links = ~{::link})"></head> <body> <div class="container"> <h1>ユーザー情報一覧</h1> </div> <table border="1"> <thead> <tr> <th>ID</th> <th>名前</th> <th>性別</th> <th></th> </tr> </thead> <tbody> <tr th:each="user : ${userlist}" th:object="${user}" class="align-middle"> <td th:text="*{iD}"></td> <td th:text="*{nAME}"></td> <td th:text="*{sEX}"></td> </tr> </tbody> </table> </div> </body> </html>
以上となります。
宜しくお願いします
あなたの回答
tips
プレビュー