前提・実現したいこと
SpringBootにて、input type ="time"で入力した値をTime型で受け取ろうとしているのですが、
BindExceptionが発生しています。
StringからTimeに変換できない為に入力が受け付けられていないようですが、
特別な実装などが必要なのでしょうか?
またそれはどのようなものでしょうか?
発生している問題・エラーメッセージ
org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'dailyReportForm' on field 'endTime': rejected value [19:54]; codes [typeMismatch.dailyReportForm.endTime,typeMismatch.endTime,typeMismatch.java.sql.Time,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dailyReportForm.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Time' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull java.sql.Time] for value '19:54'; nested exception is java.lang.IllegalArgumentException] Field error in object 'dailyReportForm' on field 'startTime': rejected value [17:54]; codes [typeMismatch.dailyReportForm.startTime,typeMismatch.startTime,typeMismatch.java.sql.Time,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dailyReportForm.startTime,startTime]; arguments []; default message [startTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Time' for property 'startTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull java.sql.Time] for value '17:54'; nested exception is java.lang.IllegalArgumentException]
該当のソースコード
html
1<div class="starter-template text-center py-5 px-3"> 2 <form method ="POST" th:action ="${dailyReportForm.newReport}? @{/main/report/insert}:@{/main/report/update}" th:object="${dailyReportForm}"> 3 <h1 th:text="${title}">Bootstrap</h1> 4 <div class ="alart alart-primary" role ="alert"> 5 <p th:if ="${complete}" th:text ="${complete}"></p> 6 </div> 7 <div th:if ="${#fields.hasErrors('created')}"th:errors ="*{created}" class ="alert alert-danger" role ="alert"></div> 8 <label>作業日時</label> 9 <input type ="datetime-local"name ="created" th:value ="*{created}"> 10 11 12 13 <label>開始時刻</label> 14 <input type ="time" name ="startTime" th:value ="*{startTime}"> 15 16 17 18 <label>終了時刻</label> 19 <input type ="time" name ="endTime" th:value ="*{endTime}"> 20 21 <br> 22 </br> 23 <div class="form-group"> 24 <label for="exampleFormControlTextarea1">日報内容</label> 25 <div th:if="${#fields.hasErrors('detail')}"th:errors="*{detail}"class="alert alert-danger" role="alert"></div> 26 <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="detail" th:value ="*{detail}"></textarea> 27 </div> 28 <br> 29 </br> 30 <div class="d-grid gap-2"> 31 <input th:if ="${dailyReportId}" type ="hidden" th:value ="${dailyReportId}" name ="dailyReportId"> 32 <button type="submit" class ="btn btn-primary">登録</button> 33 </div> 34 35 36 </form> 37 38</div>
java
1package com.example.demo.app.dailyreport; 2 3import java.util.List; 4import java.util.Optional; 5 6import javax.validation.Valid; 7 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.validation.BindingResult; 11import org.springframework.web.bind.annotation.GetMapping; 12import org.springframework.web.bind.annotation.ModelAttribute; 13import org.springframework.web.bind.annotation.PathVariable; 14import org.springframework.web.bind.annotation.PostMapping; 15import org.springframework.web.bind.annotation.RequestMapping; 16import org.springframework.web.bind.annotation.RequestParam; 17import org.springframework.web.servlet.mvc.support.RedirectAttributes; 18 19import com.example.demo.app.entity.DailyReport; 20import com.example.demo.app.service.DailyReportService; 21 22@Controller 23@RequestMapping("/main") 24public class DailyReportController { 25 26 private final DailyReportService dailyReportService; 27 28 public DailyReportController(DailyReportService dailyReportService) { 29 this.dailyReportService = dailyReportService; 30 } 31 32 @GetMapping 33 public String top(Model model) { 34 35 model.addAttribute("title","日報アプリ"); 36 return"top"; 37 } 38 @GetMapping("/report") 39 public String DailyReport(DailyReportForm dailyReportForm,Model model) { 40 41 dailyReportForm.setNewReport(true); 42 List<DailyReport> list = dailyReportService.findAll(); 43 44 model.addAttribute("list",list); 45 model.addAttribute("title","日報入力"); 46 return "DailyReportForm"; 47 } 48 49 @PostMapping("/report/insert") 50 public String insert( 51 @Valid@ModelAttribute DailyReportForm dailyReportForm, 52 BindingResult result, 53 Model model) { 54 55 56 DailyReport dailyReport = makeDailyReport(dailyReportForm,0); 57 58 59 if(!result.hasErrors()) { 60 dailyReportService.insert(dailyReport); 61 return "redirect:/main/report"; 62 }else { 63 dailyReportForm.setNewReport(true); 64 model.addAttribute("DailyReportForm",dailyReportForm); 65 List<DailyReport> list = dailyReportService.findAll(); 66 model.addAttribute("list",list); 67 model.addAttribute("title","日報入力"); 68 System.out.println(result); 69 return"DailyReportForm"; 70 } 71 72 } 73 74 @PostMapping("/report/update") 75 public String update( 76 @Valid@ModelAttribute DailyReportForm dailyReportForm, 77 BindingResult result, 78 @RequestParam("dailyReportId")int dailyReportId, 79 Model model, 80 RedirectAttributes redirectAttributes) { 81 82 83 if(!result.hasErrors()) { 84 DailyReport dailyReport = makeDailyReport(dailyReportForm,dailyReportId); 85 dailyReportService.update(dailyReport); 86 87 return "redirect:/main/report/"+dailyReportId; 88 89 }else { 90 model.addAttribute("dailyReportForm",dailyReportForm); 91 model.addAttribute("title","日報更新フォーム"); 92 93 return"DailyReportForm"; 94 } 95 96 } 97 98 @PostMapping("/report/delete") 99 public String delete( 100 Model model, 101 @RequestParam("dailyReportId")int id 102 ) { 103 104 dailyReportService.deleteById(id); 105 return"redirect:/main/report"; 106 107 } 108 109 private DailyReport makeDailyReport(DailyReportForm dailyReportForm,int dailyReportId) { 110 111 DailyReport dailyReport = new DailyReport(); 112 113 if(dailyReportId != 0) { 114 dailyReport.setId(dailyReportId); 115 } 116 117 dailyReport.setStartTime(dailyReportForm.getStartTime()); 118 dailyReport.setEndTime(dailyReportForm.getEndTime()); 119 dailyReport.setCreated(dailyReportForm.getCreated()); 120 dailyReport.setDetail(dailyReportForm.getDetail()); 121 dailyReport.setTypeId(dailyReportForm.getTypeId()); 122 123 return dailyReport; 124 } 125 126 private DailyReportForm makeDailyReportForm(DailyReport dailyReport) { 127 128 DailyReportForm dailyReportForm = new DailyReportForm(); 129 130 dailyReportForm.setStartTime(dailyReport.getStartTime()); 131 dailyReportForm.setEndTime(dailyReport.getEndTime()); 132 dailyReportForm.setCreated(dailyReport.getCreated()); 133 134 dailyReportForm.setDetail(dailyReport.getDetail()); 135 dailyReportForm.setTypeId(dailyReport.getTypeId()); 136 dailyReportForm.setNewReport(false); 137 138 return dailyReportForm; 139 } 140} 141 142
java
1package com.example.demo.app.dailyreport; 2 3import java.sql.Time; 4import java.time.LocalDateTime; 5 6import javax.validation.constraints.Future; 7import javax.validation.constraints.NotNull; 8import javax.validation.constraints.Size; 9 10import org.springframework.format.annotation.DateTimeFormat; 11 12import com.fasterxml.jackson.annotation.JsonFormat; 13 14public class DailyReportForm { 15 16 private int id; 17 18 private int typeId; 19 20 @NotNull(message ="内容を入力してください") 21 @Future(message = "過去に設定されています") 22 @DateTimeFormat(pattern ="yyyy-MM-dd'T'HH:mm") 23 private LocalDateTime created; 24 25 @NotNull(message="内容を入力してください") 26 private Time startTime; 27 28 @NotNull(message ="内容を入力してください") 29 private Time endTime; 30 31 @NotNull(message ="内容を入力してください") 32 @Size(max =20,min = 1) 33 private String detail; 34 35 private int stuffId; 36 37 private boolean newReport; 38 39 40 public DailyReportForm() { 41 42 } 43 44 public DailyReportForm( 45 int id, 46 LocalDateTime created, 47 Time startTime, 48 Time endTime, 49 String detail, 50 int stuffId, 51 boolean newReport) { 52 53 this.id= id; 54 this.created = created; 55 this.endTime = endTime; 56 this.startTime = startTime; 57 this.detail = detail; 58 this.stuffId = stuffId; 59 this.newReport = newReport; 60 } 61 62 public boolean isNewReport() { 63 return newReport; 64 } 65 66 public void setNewReport(boolean newReport) { 67 this.newReport = newReport; 68 } 69 70 public int getId() { 71 return id; 72 } 73 74 public void setId(int id) { 75 this.id = id; 76 } 77 78 public LocalDateTime getCreated() { 79 return created; 80 } 81 82 public void setCreated(LocalDateTime created) { 83 this.created = created; 84 } 85 86 87 public Time getStartTime() { 88 return startTime; 89 } 90 91 public void setStartTime(Time startTime) { 92 this.startTime = startTime; 93 } 94 95 public Time getEndTime() { 96 return endTime; 97 } 98 99 public void setEndTime(Time endTime) { 100 this.endTime = endTime; 101 } 102 103 public String getDetail() { 104 return detail; 105 } 106 107 public void setDetail(String detail) { 108 this.detail = detail; 109 } 110 111 public int getStuffId() { 112 return stuffId; 113 } 114 115 public void setStuffId(int stuffId) { 116 this.stuffId = stuffId; 117 } 118 119 public int getTypeId() { 120 return typeId; 121 } 122 123 public void setTypeId(int dailyReportId) { 124 this.typeId = dailyReportId; 125 } 126} 127
補足情報(FW/ツールのバージョンなど)
springboot v2.5.0
spring tool suite 4-4.10.0 RELEASE
java11
回答1件
あなたの回答
tips
プレビュー