前提・実現したいこと
いつもお世話になっております。
Spring Bootで1つのフォームから複数のレコードを更新したいのですが、コントローラーで値が受け取れません。
コントローラーで値を受け取り、複数レコードを更新したいです。
イメージは送信ボタンが押されると複数のレコードが同時に更新される感じです。
発生している問題・エラーメッセージ
エラーメッセージ There was an unexpected error (type=Internal Server Error, status=500). No message available java.lang.NullPointerException
該当のソースコード
文字数制限の関係でsetter,getter省略しています
エンティティクラス
java
1package jp.co.itc.mbo.entity; 2 3import java.sql.Timestamp; 4import java.util.List; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.JoinColumn; 12import javax.persistence.ManyToOne; 13import javax.persistence.OneToMany; 14import javax.persistence.Table; 15 16@Entity 17@Table(name="goals_seq") 18public class GoalSeq { 19@Entity 20@Table(name="goals_seq") 21public class GoalSeq { 22 @Id 23 @Column(name="id") 24 @GeneratedValue(strategy = GenerationType.IDENTITY) 25 private Integer id; 26 27 @ManyToOne(targetEntity = UserMaster.class) 28 @JoinColumn(nullable = false, name = "user_id") 29 private UserMaster userid; 30 31 @Column(name="goal_id") 32 private Integer goalid; 33 34 @Column(name="goal_title") 35 private String goaltitle; 36 37 @Column(name="goal_detail") 38 private String goaldetail; 39 40 @Column(name="year") 41 private Integer year; 42 43 @Column(name = "updated_at") 44 private Timestamp updatedat; 45 46 @Column(name = "updated_by") 47 private Integer updatedby; 48 49 @Column(name = "delete_flg") 50 private Boolean deleteflg; 51 52 @Column(name = "created_at") 53 private Timestamp createdat; 54 55 @OneToMany(mappedBy = "goalseqid") 56 private List<Meisai> meisaiList; 57 58
java
1package jp.co.itc.mbo.entity; 2 3import java.sql.Timestamp; 4 5import javax.persistence.Column; 6import javax.persistence.Entity; 7import javax.persistence.GeneratedValue; 8import javax.persistence.GenerationType; 9import javax.persistence.Id; 10import javax.persistence.JoinColumn; 11import javax.persistence.ManyToOne; 12import javax.persistence.Table; 13 14@Entity 15@Table(name = "meisai_tbl") 16public class Meisai { 17 18 @Entity 19@Table(name = "meisai_tbl") 20public class Meisai { 21 22 @Id 23 @Column(name = "meisai_id") 24 @GeneratedValue(strategy = GenerationType.IDENTITY) 25 private Integer meisaiid; 26 27 @Column(name = "timing") 28 private Integer timing; 29 30 @Column(name = "target") 31 private Boolean target; 32 33 @Column(name="goal_weight") 34 private Integer goalWeight; 35 36 @Column(name="evalution") 37 private Integer evalution; 38 39 @Column(name="self_comment") 40 private String selfComment; 41 42 @Column(name="boss_comment") 43 private String bossComment; 44 45 @Column(name="review_check") 46 private Integer reviewCheck; 47 48 @Column(name="status") 49 private Integer status; 50 51 @Column(name="score") 52 private Double score; 53 54 @Column(name = "updated_at") 55 private Timestamp updatedat; 56 57 @Column(name = "updated_by") 58 private Integer updatedby; 59 60 @Column(name = "delete_flg") 61 private Boolean deleteflg; 62 63 @Column(name = "created_at") 64 private Timestamp createdat; 65
フォームクラス
java
1package jp.co.itc.mbo.form; 2 3import java.util.List; 4 5import javax.persistence.OneToMany; 6 7import jp.co.itc.mbo.entity.Meisai; 8 9public class GoalSeqForm { 10 11 private String goaltitle; 12 private Integer year; 13 private String goaldetail; 14 15 @OneToMany(mappedBy = "goalseqid") 16 private List<Meisai> meisaiList; 17} 18
java
1package jp.co.itc.mbo.form; 2 3import javax.persistence.JoinColumn; 4import javax.persistence.ManyToOne; 5 6import jp.co.itc.mbo.entity.GoalSeq; 7 8public class MeisaiForm { 9 10 private Boolean target; 11 private Integer goalWeight; 12 private Integer evalution; 13 private String selfComment; 14 private String bossComment; 15 private Integer reviewCheck; 16 private Integer status; 17 private Double score; 18 19 @ManyToOne(targetEntity = GoalSeq.class) 20 @JoinColumn( name = "goalseq_id") 21 private GoalSeq goalseqid; 22
コントローラー
java
1 @RequestMapping(value = "{id}/weight_input") 2 String weightInput(@PathVariable Integer id,Model model) { 3 List<GoalSeq> goalseqs = goalseqservice.findCurrent(id); 4 model.addAttribute("goalseqs", goalseqs); 5 return "goals/weight_input";} 6 7 @RequestMapping(value = "/weight_complete/{id}", method = RequestMethod.POST) 8 String weightRegist(@PathVariable Integer id,@Valid GoalSeqForm goalseqform, Principal principal,BindingResult bindingResult) { 9 if (bindingResult.hasErrors()) {} 10 List <GoalSeq> goalseqs=goalseqservice.findCurrent(id); 11 for(GoalSeq goalseq: goalseqs) { 12 for (int i = 0; i <= 2; i++) { 13//↓ここでヌルポが出ます 14 goalseq.getMeisaiList().get(i).setGoalWeight(goalseqform.getMeisaiList().get(i).getGoalWeight()); 15 goalseqservice.save(goalseq); 16 } 17 } 18 return "redirect:/{id}"; 19 }
HTML
HTML
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> 4<head th:replace="layout :: layout('HOME',~{::link})"> 5<meta charset="UTF-8"> 6<title>Insert title here</title> 7</head> 8<body> 9 10 <form method="post" th:action="@{/weight_complete/}+${goalseqs[0].userid.id}"> 11 <table> 12 <tr> 13 <th>目標番号</th> 14 <th>期中ウエイト</th> 15 <th>期末ウエイト</th> 16 <th></th> 17 </tr> 18 <tr th:each="goalseq: ${goalseqs}" th:object="${goalseq}"> 19 <td><input type="text" id="goalid" 20 name="goalid" th:value="*{goalid}"></td> 21 <td><input type=number id="goalWeight" name="goalWeight" th:value="*{meisaiList[1].goalWeight}"></td> 22 <td><input type=number id="goalWeight" name="goalWeight" th:value="*{meisaiList[2].goalWeight}" ></td> 23 </tr> 24 </table> 25<input type=submit value="送信"> 26 </form> 27 <div th:replace="layout :: footer"></div> 28</body> 29</html>
試したこと
・HTMLのth:valueをth:fieldに変更→Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'goalseq' available as request attribute
というエラーが出て断念しました
補足情報(FW/ツールのバージョンなど)
初心者で至らない点があると思いますがよろしくお願いします。
ControllerのbindingResultの中身は定義していないので無効となっています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。