java - 无法在springboot中将'java.lang.string'的值从jsp转换为'java.localtime'
问题描述
我很陌生SpringBoot
。我在将我从 jsp 表单获得的字符串值转换为 LocalTime 时遇到了一些问题。例如,我有 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>
我有以下控制器,我尝试将此字符串值转换为 LocalTime 变量:
@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]
有人可以帮忙吗?
解决方案
我找到了一个解决方案:只需要在没有@RequestParametr
in 的情况下传递参数Controller
并将字符串转换为本地变量中的本地时间,如下所示:
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) LocalTime shiftStart
这里讨论了:如何在 Spring 中使用 LocalDateTime RequestParam?我收到“无法将字符串转换为 LocalDateTime”
推荐阅读
- python - 如何在熊猫中与来自不同数据框的列并排绘制两个条形图
- python - Python透明水印不透明
- excel - VBA:关于在以下过程后返回目标(工作表更改事件)单元格或至少返回目标工作表
- matlab - Octave:warning: 'optimoptions' 函数属于 Octave Forge 的 optim 包,但尚未实现
- nginx - 30 秒后出现 502 错误网关
- java - 如何编写使用方法显示和用户可以输入输入的java
- java - 更新 JAVA 中由特定字符分隔的文件的特定位置
- c++ - 在函数内部使用数组。对函数内部数组所做的更改对其他函数不可见
- loss-function - 具有 CTC 损失的 CNN
- azure - 工作站的 Azure 备份失败。没有数据传输到云端