gpt4 book ai didi

java - 提交 Spring 表单时无法点击 Controller

转载 作者:行者123 更新时间:2023-12-02 10:58:29 25 4
gpt4 key购买 nike

我正在使用 Spring 4.2.0 和 hibernate。我正在尝试向我的 Controller 提交一个包含日期字段的 jsp spring 表单。但我无法点击我的 Controller ,它引发了以下异常:

Field error in object 'loginDetails' on field 'dateOfBooking':rejected value [2018-02-09]; codes [typeMismatch.loginDetails.dateOfBooking,typeMismatch.dateOfBooking,typeMismatch.java.util.Date,typeMismatch];arguments[org.springframework.context.support.DefaultMessageSourceResolvable:codes [loginDetails.dateOfBooking,dateOfBooking]; arguments [];default message [dateOfBooking]]; default message [Failed to convertproperty value of type 'java.lang.String' to required type'java.util.Date' for property 'dateOfBooking'; nested exception isjava.lang.IllegalStateException: Cannot convert value of type[java.lang.String] to required type [java.util.Date] for property'dateOfBooking': no matching editors or conversion strategy found].

Below is my jsp code, bean and controller code.

This is my bean for table booking which includes a date field:

@DateTimeFormat(pattern="yyyy-MM-dd")
private Date dateOfBooking;

这是我的 Controller 代码,它不会无缘无故地变热:

@RequestMapping("/bookingTable")  
public ModelAndView bookingTable(@ModelAttribute("loginDetails") BookingTable bookingTable,HttpServletRequest request,ModelMap model) throws ParseException{
HttpSession session = request.getSession();
//Doing something here
return new ModelAndView("bookingDetails");
}

请帮我解决这个问题。我长期以来一直在尝试这样做。但不能。

最佳答案

你可以尝试向你的 Controller 添加一个@InitBinder吗?

要自定义请求参数数据绑定(bind),我们可以在 Controller 中使用 @InitBinder 注解的方法。 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/InitBinder.html

例如:https://www.concretepage.com/spring/spring-mvc/spring-mvc-validator-with-initbinder-webdatabinder-registercustomeditor-example

关于java - 提交 Spring 表单时无法点击 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51534386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com