<html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"/> <title>婚活サイト</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}"/> </head> <body th:with="user=${#authentication.principal.user}"> <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> <h2> <span th:text="${user.mail}">メアド</span> さん、ログイン中 </h2> <h2> <span th:text="${user.name}">名前</spa さん、ログイン中 </h2> <table class="table table-striped table-bordered table-condensed"> <h2>登録ユーザー</h2> <tr> <th>名前</th> <th>性別</th> <th>誕生日</th> <th>身長(cm)</th> <th>職業</th> <th>年収(万円)</th> <th>自己紹介</th> </tr> <tr th:each="profile : ${profile}"> <td th:text="${profile.name}">山田</td> <td th:text="${profile.gender.gender}">山田</td> <td th:text="${profile.birthday}">仮</td> <td th:text="${profile.height}">仮</td> <td th:text="${profile.occupation.occupationName}">仮</td> <td th:text="${profile.income}">仮</td> <td th:text="${profile.text}">仮</td> <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> <td> <img th:src="${profile.image}"/></td> </tr> </table> <form th:action="@{/konkatsu}" method="get"> <input type="submit" class="btn btn-lg btn-primary btn-block" value="Myページへ"/> </form> </body> </html>
LoginUserDetails.java
java
1//UserDetailsの実装クラス 2 3package com.example.konkatsu.service; 4 5import org.springframework.security.core.authority.AuthorityUtils; 6 7import com.example.konkatsu.domain.User; 8 9import lombok.Data; 10 11@Data 12public class LoginUserDetails extends org.springframework.security.core.userdetails.User{ 13 private final User user; 14 15 16 public LoginUserDetails(User user){ 17 super(user.getMail(), user.getPass(), AuthorityUtils.createAuthorityList("ROLE_USER")); 18 this.user = user; 19 } 20}
やりたいことはログインユーザーで登録したプロフィール情報を画面に出力したいです
usersテーブルとprofileテーブルはJOINしています。
<table class="table table-striped table-bordered table-condensed"> <h2>登録ユーザー</h2> <tr> <th>名前</th> <th>性別</th> <th>誕生日</th> <th>身長(cm)</th> <th>職業</th> <th>年収(万円)</th> <th>自己紹介</th> </tr> <tr th:each="profile : ${profile}"> <td th:text="${profile.name}">山田</td> <td th:text="${profile.gender.gender}">山田</td> <td th:text="${profile.birthday}">仮</td> <td th:text="${profile.height}">仮</td> <td th:text="${profile.occupation.occupationName}">仮</td> <td th:text="${profile.income}">仮</td> <td th:text="${profile.text}">仮</td> <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> <td> <img th:src="${profile.image}"/></td> </tr> </table>
だとログインユーザーで登録した情報など
リストが全て問題なく表示されるのですが、
<body th:with="user=${#authentication.principal.user}"> <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> <h2> <span th:text="${user.mail}">メアド</span> さん、ログイン中 </h2> <h2> <span th:text="${user.name}">名前</span> さん、ログイン中 </h2>
でログインユーザーのアドレスは表示されますが
nameはエラーとなり表示されません
これはうまくjoinされていないのか
それとも書き方が違うのかわかりません。
ログイン中のプロフィール情報だけ
出力したいです
少しで良いので助言お願いします
追加するものがありましたら
お聞かせください
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。