【问题标题】:Not able to hit controller when submitting Spring form提交 Spring 表单时无法点击控制器
【发布时间】:2018-07-26 08:31:37
【问题描述】:

我正在使用带有休眠功能的 Spring 4.2.0。我正在尝试向我的控制器提交一个包含日期字段的 jsp spring 表单。但我无法点击我的控制器,它给我抛出了以下异常:

字段“dateOfBooking”上的对象“loginDetails”中的字段错误: 拒绝值 [2018-02-09];代码 [typeMismatch.loginDetails.dateOfBooking,typeMismatch.dateOfBooking,typeMismatch.java.util.Date,typeMismatch]; 论据 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [loginDetails.dateOfBooking,dateOfBooking];论据 []; 默认消息 [dateOfBooking]];默认消息[转换失败 “java.lang.String”类型的属性值到所需类型 属性“dateOfBooking”的“java.util.Date”;嵌套异常是 java.lang.IllegalStateException:无法转换类型的值 [java.lang.String] 到所需类型 [java.util.Date] 的属性 'dateOfBooking':没有找到匹配的编辑器或转换策略]。

下面是我的jsp代码、bean和控制器代码。

这是我用于预订餐桌的 bean,其中包含一个日期字段:

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

这是我的控制器代码,无缘无故不热:

@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");  
}

请帮助我。我一直在尝试这样做。但不能。

【问题讨论】:

  • 您可以尝试将@InitBinder 添加到您的控制器吗?例如:concretepage.com/spring/spring-mvc/…
  • 非常感谢......它成功了
  • 酷..请接受下面发布的答案。希望它能帮助有同样问题的人

标签: java spring


【解决方案1】:

您可以尝试将@InitBinder 添加到您的控制器吗?

要自定义请求参数数据绑定,我们可以在控制器中使用@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多