Profile.java
java
1//profileテーブルとマッピングさせる 2 3package com.example.konkatsu.domain; 4 5import java.sql.Date; 6 7import javax.persistence.Column; 8import javax.persistence.Entity; 9import javax.persistence.FetchType; 10import javax.persistence.GeneratedValue; 11import javax.persistence.GenerationType; 12import javax.persistence.Id; 13import javax.persistence.JoinColumn; 14import javax.persistence.ManyToOne; 15import javax.persistence.Table; 16 17import lombok.AllArgsConstructor; 18import lombok.Data; 19import lombok.NoArgsConstructor; 20 21@Data 22@NoArgsConstructor //デフォルトコンストラクタも必要な場合 23@AllArgsConstructor //すべてのフィールドを引数に持つコンストラクタを自動生成 24@Entity //エンティティ(一単位として扱われるデータのまとまり)であることを示す。指定したクラス名がテーブルとマッピングされる 25@Table(name = "profile") //接続するテーブルを指定 26public class Profile { 27 @Column(name = "user_id") 28 @Id //主キー 29 @GeneratedValue(strategy = GenerationType.IDENTITY) //自動採番される 30 private Integer id; 31 private Integer genderId; 32 private String name; 33 private Date birthday; 34 private Integer height; 35 private Integer occupationId; 36 private Integer income; 37 private String text; 38 private byte[] image; 39 @ManyToOne(fetch = FetchType.LAZY) //@ManyToOneで多対1の関係にする 40 @JoinColumn(nullable = true, name = "user_id", insertable=false, updatable=false) //外部キーのカラム名の指定 41 private User user; 42 43}
profileForm.java
java
1//このクラスにプロフィール登録画面の<form>から送るパラメータをマッピングさせる 2 3 4package com.example.konkatsu.web; 5 6import java.sql.Date; 7 8import lombok.Data; 9 10@Data 11public class ProfileForm { 12 @NotNull 13 private String name; 14 @NotNull 15 private Date birthday; 16 @NotNull 17 private Integer height; 18 @NotNull 19 private Integer occupationId; 20 @NotNull 21 private Integer income; 22 @NotNull 23 private String text; 24 @NotNull 25 private byte[] image; 26} 27
ProfileRepository
java
1package com.example.konkatsu.repository; 2 3import java.util.List; 4 5import org.springframework.data.domain.Page; 6import org.springframework.data.domain.Pageable; 7import org.springframework.data.jpa.repository.JpaRepository; 8import org.springframework.data.jpa.repository.Query; 9import org.springframework.stereotype.Repository; 10 11import com.example.konkatsu.domain.Profile; 12 13@Repository 14 public interface ProfileRepository extends JpaRepository<Profile, Integer> { 15 @Query("SELECT x FROM Profile x ORDER BY x.id, x.genderId, x.name, x.birthday, x.height, x.occupationId, x.income, x.text, x.image") //@QueryをつけてJPQLを記述 16 List<Profile> findAllOrderByProfile(); 17 18 @Query("SELECT x FROM Profile x ORDER BY x.id, x.genderId, x.name, x.birthday, x.height, x.occupationId, x.income, x.text, x.image") 19 Page<Profile> findAllOrderByProfile(Pageable pageable); 20 21 } 22 23
Controller
java
1 2省略 3 4 5@PostMapping(path = "createProfile") //URLが konkatsu/createProfile となる 6 String create 7 // 送信されたフォームの入力チェックを行う為に@Validatedアノテーションを付ける。 8 // これによりProfileFormに設定したBean Validationアノテーションが評価され、結果が隣の引数のBindingResultに格納される 9 (@Validated ProfileForm form, BindingResult bindingResult, Model model, 10 @AuthenticationPrincipal LoginUserDetails userDetails){ //@AuthenticationPrincipalをつけることでログイン中の[LoginDetails]オブジェクトを取得できる 11 if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る 12 System.out.println("エラー"); 13 return list(model); 14 } 15 16 Profile profile = new Profile(); 17 BeanUtils.copyProperties(form, profile);//ProfileFormをProfileにコピーする 18 profileService.create(profile, userDetails.getUser()); //LoginUserDetailsオブジェクトに格納されているUser情報を取り出してProfileServiceに渡す 19 //ログイン中の[LoginUserDetails]オブジェクトに格納されているUser情報を取り出して[ProfileService]に渡す 20 return "/konkatsu/profileConfirm"; 21 }
html
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> <!-- ThymeleafのSpring Security連携機能の名前空間を設定 --> 4<head> 5 <meta charset="utf-8"/> 6 <title>婚活サイト</title> 7 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 8 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/> 9 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 10 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}"/> 11</head> 12 13<body> 14 15<div class="container"> 16 17 <h2> 18 <span sec:authentication="principal.user.mail">メアド</span> 19 <!-- sec:authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、principal.user.mailでログインユーザー名が分かる--> 20 さん、ログイン中 21 </h2> 22 23 24 25 <form th:action="@{/logout}" method="post"> 26 <input type="submit" class="btn btn-default btn-xs" value="ログアウト" /> 27 </form> 28 29 <form th:action="@{/konkatsu/createProfile}" th:object="${profileForm}" class="form-signin" enctype="multipart/form-data" method="post" ><!-- th:actionでformタグのaction属性の内容を置換できる --> 30 <!-- action:フォームの送信ボタンを押して送信されるデータの送信先URLを指定する --> 31 <!-- th:object="${profileForm}":profileFormクラスのnameなどを使えるようにする --> 32 33 <!-- 部品がテキストフィールドなら初期値として入力欄に表示されます。テキストが書き換えられればそれが value値になり、name属性とセットで送信されます。 34 また、ラジオボタンやチェックボックスでは、選択された値の名称となり、name"src/main/resources/templates/konkatsu/profileForm.html"属性とセットで送信されます。 35 送信やリセットボタンなどの場合はボタン上に表示されます--> 36 37 <h2 class="form-signin-heading">Myプロフィールフォーム</h2> 38 39 <div th:if="${param.error}" class="alert alert-danger"> 40 正しく入力してください。 41 </div> 42 43 <div class="form-group" > 44 <label>氏名</label> 45 <!-- プログラムに送信するデータに名前を付けるのがname属性 --> 46 <input type="text" class="form-control" name="name" placeholder="(例) 山田太郎" required="required" 47 autofocus="autofocus"/> 48 </div> 49 50 <div class="form-group"> 51 <label>性別</label> 52 <select class="form-control" name="genderId" > 53 <option value="1">男</option> 54 <option value="2">女</option> 55 </select> 56 </div> 57 58 <div class="form-group"> 59 <label>生年月日</label> 60 <input type="text" class="form-control" name="birthday" placeholder="(例) 2017/01/01" required="required"/> 61 </div> 62 63 <div class="form-group"> 64 <label>身長</label> 65 <input type="text" class="form-control" name="height" placeholder="(例)180cm" required="required"/> 66 </div> 67 68 <div class="form-group"> 69 <label>職業</label> 70 <select class="form-control" name="occupationId" > 71 <!-- <option value='' disabled selected style='display:none;'>職業</option> --> 72 <option value="1">公務員系</option> 73 <option value="2">医療・介護・福祉系</option> 74 <option value="3">教育系</option> 75 <option value="4">金融系</option> 76 <option value="5">IT・電気・電子系</option> 77 <option value="6">製造・建築・土木系</option> 78 <option value="7">アート・デザイン系</option> 79 <option value="8">音楽・イベント系</option> 80 <option value="9">芸能・マスコミ系</option> 81 <option value="10">サービス系</option> 82 <option value="11">飲食系</option> 83 <option value="12">ファッション・美容系</option> 84 <option value="13">スポーツ・インストラクター系</option> 85 <option value="14">自営業</option> 86 <option value="15">その他</option> 87 </select> 88 </div> 89 90 <div class="form-group"> 91 <label>年収</label> 92 <input type="number" class="form-control" name="income" placeholder="(例)600万円" required="required"/> 93 </div> 94 95 <div class="form-group"> 96 <label for="InputTextarea">自己紹介文</label> 97 <textarea class="form-control" name="text" rows="6" placeholder="自己PRをしてください"></textarea> 98 </div> 99 100 <div class="form-group"> 101 <label>プロフィール画像</label> 102 <input type="file" class="form-control" name="image" placeholder="写真" required="required"/> 103 </div> 104 105 106 <button class="btn btn-lg btn-primary btn-block" type="submit">登録する</button> 107 </form> 108 109 <form th:action="@{/konkatsu/list}" method="get"> 110 <input type="submit" class="btn btn-default" value="ユーザー一覧へ"/> 111 </form> 112 113</div> 114 <table class="table table-striped table-bordered table-condensed"> 115 <h2>登録ユーザー</h2> 116 <tr> 117 <th>名前</th> 118 <th>誕生日</th> 119 <th>身長(cm)</th> 120 <th>職業</th> 121 <th>年収</th> 122 <th>自己紹介</th> 123 <th>担当者</th> 124 125 126 </tr> 127 <tr th:each="profile : ${profile}"> 128 <td th:text="${profile.name}">山田</td> 129 <td th:text="${profile.birthday}">山田</td> 130 <td th:text="${profile.height}">山田</td> 131 <td th:text="${profile.occupationId}">山田</td> 132 <td th:text="${profile.income}">山田</td> 133 <td th:text="${profile.text}">山田</td> 134 <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 135 <td th:="${profile.image}">画像</td> 136 137 </tr> 138 </table> 139 140 141</body> 142</html> 143
ユーザー登録でデータを入力して登録を押しても
DBに保存れず
ログは出ず
コンソールに”エラー”と表示されるだけです
つまりは
if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る System.out.println("エラー"); return list(model); }
ここのエラーだと思うのですが
解決策がわかりません
DBとの連結はできています
なにか助言をいただきたいです
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/07 01:13
2017/07/07 01:20
2017/07/07 01:40