1

In the table of my database i have a column of type Date, and I want to insert value into this column so I should convert Strings to Date before; for do that I use the Class CustomDateEditor, and this is what i did:

Create a controller where the string will be converted :

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

    }

The validator :

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>

but after executing I get this error :

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

It's seems to me that the controller is not working or it can't find the field to convert. Where is the problem please? Thank you

Souad
  • 4,856
  • 15
  • 80
  • 140
  • BTW: Start using Spring 3.x, SimpleFormController is deprecated since Spring 3.0 (released on 16th December 2009) – Ralph May 27 '13 at 10:39

1 Answers1

2

You realy should remove this spring 2.x stuff. The SimpleFormController.initBinder works only for that controller. If you want to register a global conterter that works for all controllers then you have to register it at the FormattingConversionServiceFactoryBean

For a powerfull solution have a look at this answer: https://stackoverflow.com/a/13778502/280244 of (Register converters and converterFactories with annotations in Spring 3)


Btw: Even if it is note the cause of your problem, but you should add

super.initBinder(request, binder)

at the end of your initBinder method.

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thank you =D I resolve the problem by adding my method initBinder into the controller of my page with the annotation **@InitBinder** – Souad May 27 '13 at 10:48
  • What is the utility of adding this **super.initBinder(request, binder)** please ? – Souad May 27 '13 at 14:47
  • 1
    It initialize the binder that is configured by, webBindingInitializer parameter --- but you are right, this was not the cause of your problem, – Ralph May 27 '13 at 17:39