前提・実現したいこと
ユーザー一覧表示をしたいのですがエラーになってしまいます
発生している問題・エラーメッセージ
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "userid" (template: "users/index" - line 28, col 17)
2021-04-18 15:03:07.839[0;39m [31mERROR[0;39m [35m17341[0;39m [2m---[0;39m [2m[nio-8080-exec-7][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/users/index.html]")] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'userid' cannot be found on object of type 'com.example.demo.models.User' - maybe not public or not valid?
該当のソースコード
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>Listing Users</title> <link rel="stylesheet" href="/css/bootstrap.css" /> <script src="/js/jquery.js"></script> <script src="/js/bootstrap.js"></script> </head> <body> <div class="container"> <h1>Listing Users</h1> <table class="table"> <thead> <tr> <th>ID</th> <th>名前</th> <th>年齢</th> <th>趣味</th> <th></th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr th:each="user:${users}" th:object="${user}"> <td th:text="*{id}"></td> <td th:text="*{nickname}"></td> <td th:text="*{age}"></td> <td th:text="*{hobby}"></td> <td><a class="btn btn-default btn-xs" th:href="@{/users/{id}(id=*{id})}">参照</a></td> <td><a class="btn btn-default btn-xs" th:href="@{/users/{id}/edit(id=*{id})}">編集</a></td> <td> <form th:action="@{/users/{id}(id=*{id})}" th:method="delete"> <input class="btn btn-default btn-xs" type="submit" value="削除" /> </form> </td> </tr> </tbody> </table> <a class="btn btn-default" href="/users/new">新規作成</a> </div> </body> </html>
package com.example.demo.controllers; 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.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.example.demo.models.User; import com.example.demo.service.UserService; @Controller @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping //メソッドの引数にModel型の値を設定,Modelのインスタンスが自動的に渡される public String index(Model model) { List<User> users = userService.findAll(); model.addAttribute("users",users); return "users/index"; } @GetMapping("new") public String newUser(Model model) { return "users/new"; } @GetMapping("{id}/edit") //@PathVariableを設定するとURL上の値を取得 public String edit(@PathVariable Integer id,Model model) { User user = userService.findOne(id); model.addAttribute("user",user); return "users/edit"; } @GetMapping("{id}") public String show(@PathVariable Integer id,Model model) { User user = userService.findOne(id); model.addAttribute("user",user); return "users/show"; } @PostMapping //@ModelAttributeをつけると送信されたリクエストのbodyの情報を取得できる public String create(@ModelAttribute User user) { userService.save(user); //createメソッドの処理が終わった後indexに戻る return "redirect:/users"; } @PutMapping("{id}") public String Update(@PathVariable Integer id,@ModelAttribute User user) { user.setId(id); userService.save(user); return "redirect:/users"; } @DeleteMapping("{id}") public String destroy(@PathVariable Integer id) { userService.delete(id); return "redirect:/users"; } }
package com.example.demo.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity//DBに登録、更新、保持 public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)//主キーの自動生成(id) @Column(name="id")//カラムの指定 private Integer id; @Column(name="nickname") private String nickname; @Column(name="age") private Integer age; @Column(name="hobby") private String hobby; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public String toString() { return "User [id="+ id +",nickname="+ nickname +",age="+ age +",hobby="+ hobby +"]"; } }
package com.example.demo.service; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.models.User; import com.example.demo.repositries.UserRepository; @Service //サーバ処理を実装 public class UserService { @Autowired //Beanをインジェクトしてnewしなくても使える private UserRepository userRepository; public List<User> findAll(){ return userRepository.findAll(); } public User findOne(Integer id) { Optional<User> user = userRepository.findById(id); if(user.isPresent()) { return user.get();//get()で値を返す }else { return null; } // return userRepository.findById(id); } public User save(User user) { return userRepository.save(user); } public void delete(Integer id) { userRepository.deleteById(id); } }
試したこと
モデルにuseridがないとなっていたのでidを指定したのですが、、、
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー