Search code examples
javajspdatetimejodatimestripes

How to pass date from JSP to Java using stripes framework?


I'm using the Stripes Framework and am trying to get a DateTime from the jsp but for some reason the setter always gets null passed into it.

JSP snippet:

<stripes:form name="dateForm" action="some.actionBean.url">
    <stripes:hidden name="myDate" value="12-23-2015 12:00" />
</stripes:form>

ActionBean snippet:

private DateTime myDate;

public void setMyDate(DateTime date){
  //when the setter gets called date is null, but why?
  this.myDate = date;
}
public DateTime getMyDate(){
  return this.myDate;
}

I tried many things already, like

  • tried to set the value to different time formats
  • tried to have the setter take in a string and then convert to a DateTime(this didn't work because the string is also null)
  • tried different stripes tags

no luck yet, What am I doing wrong?

I'm basically tapping in the dark because I can't find the Tag Lib documentation. On the official site it is linked to a broken page.


Solution

  • You are binding into a DateTime object. Stripes has a built in TypeConverter for Date objects but not for DateTime.

    When you change myDate to java.util.Date Stripes' DateTypeConverter will pick it up.

    Otherwise, if for instance you need support for joda.time.DateTime you'll need to write your own custom TypeConverter (which you don't need because its printed below):

    public class JodaDateTimeTypeConverter implements TypeConverter<DateTime> {
    
        @Override
        public DateTime convert(String input, Class<? extends DateTime> type, Collection<ValidationError> errors) {
    
            DateTimeFormatter formatter = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm");
            DateTime datetime = formatter.parseDateTime(input);
            return datetime;
        }
    
        @Override
            public void setLocale(Locale arg0) {
        }
    }
    

    And put this custom TypeConverter class in (one of) your Stripes Extension package(s) which can be defined in web.xml under the filter named StripesFilter.:

    <init-param>
        <param-name>Extension.Packages</param-name>
        <param-value>path.to.my.extensionpackage</param-value>
    </init-param>