我对SpringBoot
非常陌生。我将从jsp表单获取的String值转换为本地时间时遇到一些问题。例如我有jsp形式,我写我的输入
<div class="col-lg-7">
<form:input path="shiftStart" type="time" name="shiftStart" id="shift-start-time" min="08:00:00" max="17:45:00" step="900"></form:input>
</div>
并且我有一个控制器,我尝试将这个字符串值转换为本地时间变量
@PostMapping("/add-shift")
public String createShiftForm(@ModelAttribute("shiftForm") @Valid TimeTable timeTable, BindingResult result, Model model, @RequestParam("shiftStart") String shiftStart ){
if (result.hasErrors()) {
return "/add-shift";
}
LocalTime startShift = LocalTime.parse(shiftStart);
model.addAttribute("shiftStart",startShift);
return "redirect:/admin";
}
我收到错误:
Failed to convert property value of type java.lang.String to required type java.time.LocalTime for property shiftEnd; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.time.LocalTime] for value 09:15; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [09:15]
有人可以帮忙吗?
您需要将String
值解析为LocalTime
,因此,您需要弄清楚接收它的格式,只需添加此代码即可:
LocalTime startShift = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"));