【发布时间】:2019-04-10 19:46:14
【问题描述】:
我在从控制器获取日期时遇到了一些问题,因为我不明白什么是正确的格式。
控制器:
@RequestMapping(value = "/findFlights", method = RequestMethod.POST)
public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to,
@RequestParam("departureDate") @DateTimeFormat(pattern = "YYYY-MM-DD") LocalDate departureDate, Model model) {}
表格:
<form th:action="@{/findFlights}" method="POST">
From:<input type="text" name="from" required/>
To:<input type="text" name="to" required/>
Departure Date:<input type="date" name="departureDate" required />
<input type="submit" value="Search"/>
</form>
当我提交表单时,无论日期的格式是什么,它总是给我一个错误:(这是错误:
无法将“java.lang.String”类型的值转换为所需类型 'java.time.LocalDate';嵌套异常是 org.springframework.core.convert.ConversionFailedException: 失败 从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] 的值'2018-11-05';嵌套异常是 java.lang.IllegalArgumentException:值的解析尝试失败 [2018-11-05]
如果我指定@DateTimeFormat 注释,我认为转换会自动完成。
【问题讨论】: