简体   繁体   中英

Spring MVC hidden date field does not get mapped to controller

I am developing an update form. For this form I am loading a screen which first displays all the existing details and gives user option to edit. Now I have a field called createdOnTs which is a date field which I do not want the user to be able to update. So I have added an spring form hidden element in my form.

<td><form:hidden path="createdOnTs"/></td>

On checking value using firbug this field does have the value which it gets from DB. But when user submits update button and Spring controller which gets invoked in there the value is coming as null. How do I configure spring MVC to map the existing date hidden value?

I had the same issue to convert string date to actual date and assign it to model. in your case Spring doesn't do it for you, in your model class you have write a String date to Util Date convert util method and change your path to reflect the method.

this is what I have in my code.

    <input name ="stringDate" value="01/01/2010 10:10:10" style="display:none";> 

and I have this method in my model.

   public void setStringDate(String stringDate){
    //convert stringDate to Date. 
    Date date = convertToUtilDate(stringDate);
  }

Let the spring binder know what class type you want it to bind. So that you won't get TypeMismatchException by adding class = "date"

<td><form:hidden path = "createdOnTs" class = "date"/></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM