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

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

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

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

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

Q&A

0回答

1568閲覧

spring boot でのthymeleafでの値の受渡しで例外が発生

goriwo

総合スコア17

Java

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

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

0グッド

0クリップ

投稿2021/04/18 06:11

編集2021/04/18 19:47

前提・実現したいこと

ユーザー一覧表示をしたいのですがエラーになってしまいます

発生している問題・エラーメッセージ

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 ERROR 17341 --- [nio-8080-exec-7] 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.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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問