无法将String转换为Date Spring MVC

时间:2013-05-27 09:04:11

标签: java spring date model-view-controller

在我的数据库表中,我有一个Date类型的列,我想在此列中插入值,所以我应该将字符串转换为Date之前;为此,我使用了Class 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.");
            }

    }

}

Page 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

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

1 个答案:

答案 0 :(得分:2)

你真的应该删除这个春天2.x的东西。 SimpleFormController.initBinder仅适用于该控制器。如果要注册适用于所有控制器的全局转发器,则必须在FormattingConversionServiceFactoryBean

注册它

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


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

super.initBinder(request, binder)

initBinder方法的最后。