【问题标题】:Failed to convert String to Date Spring MVC无法将字符串转换为日期 Spring MVC
【发布时间】:2013-05-27 09:04:10
【问题描述】:

在我的数据库表中,我有一列 Date 类型,我想将值插入到该列中,因此我应该先将 Strings 转换为 Date;为此,我使用类 CustomDateEditor,这就是我所做的:

创建一个将转换字符串的控制器:

 package gestion.delegation.controller;

    import gestion.delegation.domaine.Movement;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.servlet.mvc.SimpleFormController;

    public class ServiceController extends SimpleFormController{
         protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
                binder.registerCustomEditor(Date.class, editor);
    }

    }

验证者:

package gestion.delegation.validator;

import java.util.Date;

import gestion.delegation.domaine.Movement;
import gestion.delegation.domaine.User;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class AddActivityValidator  implements Validator{

    @Override
    public boolean supports(Class<?> clazz) {

        return Movement.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object obj, Errors err) {
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "mode_affectation", "modeaffectattion.requiered", "Choisissez un mode d'affectation");
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "nom_etabl", "name.required","Choisissez un nom d'une etablissement");
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "cina", "cinn.required","Il faut choisir un fonctionnaire au dessus avant d'essayer de saisir un service");
       Movement move =  (Movement) obj;
       Date debut = move.getDate_debut();
       Date fin=move.getDate_fin();


            if (debut != null && fin != null && fin.before(debut)) {
              err.rejectValue("fin", "notbefore.startdate", "End date cannot be before start date.");
            }

    }

}

页面Jsp:

<tr>
                                        <td>Date début :</td>
                                        <td><form:input type="text" name="date" class="tcal"
                                                value="" path="date_debut" /></td>

                                    </tr>
                                    <tr>
                                        <td>Date fin :</td>
                                        <td><form:input type="text" name="date" class="tcal"
                                                value="" path="date_fin" /></td>
                                           <td><form:errors path="date_fin" Class="errorbox" /></td>
                                    </tr>

但执行后我得到这个错误:

Failed to convert property value of type java.lang.String to required type java.util.Date for property date_fin; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "2013-06-01" from type java.lang.String to type java.util.Date; nested exception is java.lang.IllegalArgumentException

在我看来,控制器不工作或找不到要转换的字段。请问问题出在哪里?谢谢

【问题讨论】:

  • 顺便说一句:开始使用 Spring 3.x,SimpleFormController 自 Spring 3.0(2009 年 12 月 16 日发布)起已弃用

标签: java spring date model-view-controller


【解决方案1】:

你真的应该删除这个 spring 2.x 的东西。 SimpleFormController.initBinder 仅适用于该控制器。如果你想注册一个适用于所有控制器的全局控制器,那么你必须在FormattingConversionServiceFactoryBean注册它

要获得强大的解决方案,请查看以下答案:https://stackoverflow.com/a/13778502/280244 of (Register converters and converterFactories with annotations in Spring 3)


顺便说一句:即使是说明你的问题的原因,但你应该添加

super.initBinder(request, binder)

在您的 initBinder 方法的末尾。

【讨论】:

  • 谢谢 =D 我通过将我的方法 initBinder 添加到带有注释 @InitBinder 的页面的控制器中来解决问题
  • 请问添加这个super.initBinder(request, binder)有什么用处?
  • 它初始化了由 webBindingInitializer 参数配置的活页夹---但是你是对的,这不是你的问题的原因,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 2016-01-10
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多