コントローラー
java
1 2 3package com.example.konkatsu.web; 4 5import java.io.IOException; 6import java.util.List; 7 8import org.springframework.beans.BeanUtils; 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.security.core.annotation.AuthenticationPrincipal; 11import org.springframework.stereotype.Controller; 12import org.springframework.ui.Model; 13import org.springframework.validation.BindingResult; 14import org.springframework.validation.annotation.Validated; 15import org.springframework.web.bind.annotation.GetMapping; 16import org.springframework.web.bind.annotation.ModelAttribute; 17import org.springframework.web.bind.annotation.PostMapping; 18import org.springframework.web.bind.annotation.RequestMapping; 19import org.springframework.web.bind.annotation.RequestParam; 20import org.springframework.web.multipart.MultipartFile; 21 22import com.example.konkatsu.domain.Profile; 23import com.example.konkatsu.service.LoginUserDetails; 24import com.example.konkatsu.service.ProfileService; 25 26 27@Controller 28@RequestMapping("konkatsu") //「URL」の接頭辞をkonkatsuに設定(このクラスで呼ばれたpostなどは/konkatsu/(path)となる) 29 30public class KonkatsuController { 31 @Autowired 32 ProfileService profileService; 33 34 @ModelAttribute //@ModelAttributeを付けたメソッド内でProfileFormを初期化 35 //@ModelAttributeがついたメソッドは、@PostMappingでマッピングされたメソッドの前に実行され 36 //「返り値」は自動で「Model」に追加される(この例だと「list」や「create」メソッドが呼ばれる前に「model.addAttribute(new ProfileForm())」相当の処理が行われる) 37 ProfileForm setUpForm() { 38 return new ProfileForm(); 39 } 40 41 42 43 @GetMapping //pathの指定がない場合は URL は/konkatsu 44 public String myPage(@AuthenticationPrincipal LoginUserDetails userDetails) { 45 return "konkatsu/myPage"; 46 } 47 48 49 @GetMapping(path = "profileForm") //pathの指定がない場合は URL は/konkatsu 50 String list(Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 51 List<Profile> profile = profileService.findAll(); 52 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 53 return "konkatsu/profileForm"; //遷移する画面の名前 54 } 55 56 57 58 //th:action="@{/konkatsu/createProfile}" の処理 59 @PostMapping(path = "createProfile") //URLが /konkatsu/createProfile となる 60 public String create(@Validated ProfileForm form, BindingResult bindingResult, Model model, 61 // 送信されたフォームの入力チェックを行う為に@Validatedアノテーションを付ける。 62 // これによりProfileFormに設定したBean Validationアノテーションが評価され、結果が隣の引数のBindingResultに格納される 63 @AuthenticationPrincipal LoginUserDetails userDetails) throws IOException{ 64 //@AuthenticationPrincipalをつけることでログイン中の[LoginUserDetails]オブジェクトを取得できる 65 66 System.out.println(form.getId()); 67 System.out.println(form.getName()); 68 System.out.println(form.getGenderId()); 69 System.out.println(form.getBirthday()); 70 System.out.println(form.getHeight()); 71 System.out.println(form.getOccupationId()); 72 System.out.println(form.getIncome()); 73 System.out.println(form.getText()); 74 System.out.println(form.getFile()); 75 76 if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る 77 System.out.println("エラー"); 78 return list(model); 79 } 80 81 82 Profile profile = new Profile(); 83 profile.setId(form.getId()); 84 profile.setName(form.getName()); 85 profile.setGenderId(form.getGenderId()); 86 profile.setBirthday(form.getBirthday()); 87 profile.setHeight(form.getHeight()); 88 profile.setOccupationId(form.getOccupationId()); 89 profile.setIncome(form.getIncome()); 90 profile.setText(form.getText()); 91 MultipartFile uploaded = form.getFile(); 92 byte[] image = uploaded.getBytes(); //アップロードファイルをbyte配列で取得 93 profile.setImage(image); 94 95 BeanUtils.copyProperties(form, profile);//ProfileFormをProfileにコピーする 96 profileService.create(profile, userDetails.getUser()); //ProfileをDBに追加する (ログインユーザー情報も) 97 98 return "/konkatsu/profileConfirm"; 99 100 } 101 102 @GetMapping(path = "manList") //pathの指定がない場合は URL は/konkatsu 103 String manList(@RequestParam Integer id, Model model){ //htmlからidとして送られた値を@RequestParamにて取得SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 104 List<Profile> profile = profileService.findUser(id); 105 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 106 return "konkatsu/profileList"; //遷移する画面の名前 107 } 108 109 @GetMapping(path = "myProfile") //pathの指定がない場合は URL は/konkatsu 110 String MyList(@RequestParam Integer id, Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 111 Profile profile = profileService.findOne(id); 112 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 113 return "konkatsu/myProfile"; //遷移する画面の名前 114 } 115 116}
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="utf-8"/> 5 <title>婚活サイト</title> 6 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 7 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/> 8 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 9 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}"/> 10</head> 11 12<body th:with="user=${#authentication.principal.user}"> 13 <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> 14 15 <h2> 16 <span th:text="${user.mail}">メアド</span> 17 さん、Myプロフィール 18 </h2> 19 20 21 <table class="table table-striped table-bordered table-condensed"> 22 23 <tr> 24 <th>名前</th> 25 <th>性別</th> 26 <th>誕生日</th> 27 <th>身長(cm)</th> 28 <th>職業</th> 29 <th>年収(万円)</th> 30 <th>自己紹介</th> 31 <th>メールアドレス</th> 32 <th>画像</th> 33 </tr> 34 35 36 <tr> 37 38 <td th:text="${profile.name}">山田</td> 39 <td th:text="${profile.gender.gender}">山田</td> 40 <td th:text="${profile.birthday}">仮</td> 41 <td th:text="${profile.height}">仮</td> 42 <td th:text="${profile.occupation.occupationName}">仮</td> 43 <td th:text="${profile.income}">仮</td> 44 <td th:text="${profile.text}">仮</td> 45 <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 46 47---------------------------------- 48 <td> <img th:src="${profile.image}"/></td> 49----------------------------------- 50 </tr> 51 </table> 52 53 54 55 56 <form th:action="@{/konkatsu/manList}" method="get"> 57 <input type="hidden" name="id" th:value="${profile.genderId}" /> 58 <button type="submit" class="btn btn-lg btn-primary btn-block">登録ユーザー一覧を見る</button> 59 </form> 60 61 62 <form th:action="@{/logout}" method="post"> 63 <input type="submit" class="btn btn-lg btn-primary btn-block" value="ログアウト" /> 64 </form> 65 66</body> 67</html>
MultipartFileで取得した画像を
byte[] image = uploaded.getBytes();
でバイト配列にしDBに保存しました
そのDBにある画像データを画面上に表示したいです
<td> <img th:src="${profile.image}"/></td>とコントローラーを書き加えると思うのですが
Base64形式にして出力する方法があると思いますが
具体的にどこを直せば良いのでしょうか??
追加した方が良いクラスがありましたら
教えてください!!!!
お願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/14 06:43