java
1@GetMapping(path = "myProfile") //pathの指定がない場合は URL は/konkatsu 2 String MyList(@RequestParam Integer id, Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 3 Profile profile = profileService.findProfile(id); 4 5 //画像ファイルをBASE64形式でエンコード 6 String encodedImage = Base64.getEncoder().encodeToString(profile.getImage()); 7 System.out.println(encodedImage); 8 9 model.addAttribute("image", encodedImage); 10 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 11 return "konkatsu/myProfile"; //遷移する画面の名前 12 }
html
1 <td><img th:src="${'data:image/png;base64,' + image}" width="250" height="150"/></td> 2 <td th:text="${profile.name}">山田</td> 3 <td th:text="${profile.gender.gender}">山田</td> 4 <td th:text="${profile.birthday}">仮</td> 5 <td th:text="${profile.height}">仮</td> 6 <td th:text="${profile.occupation.occupationName}">仮</td> 7 <td th:text="${profile.income}">仮</td> 8 <td th:text="${profile.user.mail}">duke</td> 9 <td th:text="${profile.text}">仮</td>
この場合、
画像が画面に表示されるのですが
どのように書き換えたら良いのでしょうか???
この理論であっていますか??
java
1String encodedImage = Base64.getEncoder().encodeToString(ここにリストProfileのimageを入れたいのです));
html
1 2 <tr th:each="profile : ${profile}"> 3 <!-- エンコードした画像の表示 --> 4 <td><img th:src="${'data:image/png;base64,' + image}" width="250" height="150"/></td> 5 <td th:text="${profile.name}">山田</td> 6 <td th:text="${profile.gender.gender}">山田</td> 7 <td th:text="${profile.birthday}">仮</td> 8 <td th:text="${profile.height}">仮</td> 9 <td th:text="${profile.occupation.occupationName}">仮</td> 10 <td th:text="${profile.income}">仮</td> 11 <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 12 <td th:text="${profile.text}">仮</td> 13 </tr>
同じような形で
ListのProfileで登録ユーザー一覧に画像を表示したい場合
java
1 @GetMapping(path = "usersList") 2 String usersList(@RequestParam Integer genderId, Model model){ 3 List<Profile> profile = profileService.findUsers(genderId); 4 5 6 String encodedImage = Base64.getEncoder().encodeToString(profile.getImage()); 7 System.out.println(encodedImage); 8 9 model.addAttribute("image", encodedImage); 10 model.addAttribute("profile", profile); 11 return "konkatsu/profileList"; 12 }
profile.getImage()でエラーとなってしまいます
List Profileに格納されているimageを出力したいです
回答1件
あなたの回答
tips
プレビュー