我遇到此错误,请问我做错了吗?
无法将类型“ java.lang.String”的值转换为必需的类型 java.time.LocalDate的嵌套异常是 java.lang.IllegalStateException:无法转换类型的值 'java.lang.String'为必需的类型'java.time.LocalDate':无匹配项 编辑器或转换策略。
package com.pc.controller;
import java.time.LocalDate;
import java.util.Map;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class Controllera {
@RequestMapping(value="/home.htm",method=RequestMethod.GET)
public String showForm() {
System.out.println("Controllera.showForm()");
return "welcome";
}
@RequestMapping(value="/home.htm",method=RequestMethod.POST)
public String process(Map<String ,Object>map,@RequestParam("date")
@DateTimeFormat(pattern="dd-MM-yyyy") LocalDate date) {
System.out.println("Controllera.process()");
System.out.println(date);
map.put("date",date);
return "success";
}
}
//end of controller
My web page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<form method="POST">
<input type="text" name="date" id="date"/><br>
<input type="submit"/>
</form >
答案 0 :(得分:1)
HTML的默认日期格式为YYYY-MM-YY。
您有2种解决方案:
1-更改您的参数,例如@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date
您的控制器方法也应为:
@RequestMapping(value="/home.htm",method=RequestMethod.POST)
public String process(Map<String ,Object> map, @RequestParam("date")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
// omitted for brevity.
}
}
2-或者您可以在JSP中执行此操作,因为您需要检查this