开发者

Does Freemarker have any integration for JodaTime?

开发者 https://www.devze.com 2023-02-15 17:15 出处:网络
Are there any ways to easy format Joda DateTime objects in Freemarker?For example with Java dates, we could use the ?string and other directives.

Are there any ways to easy format Joda DateTime objects in Freemarker? For example with Java dates, we could use the ?string and other directives.

I know I could call toDate and get a Java Date, but I was hoping there was a b开发者_Python百科etter way.


You should be able to call the toString(String pattern) method directly from Freemarker:

${dateTime.toString('MM-dd-yy')}

(not tested)


There's even a simpler way of doing this, if you don't want to splatter toString('MM-dd-yy') all over your templates.

Simply extend Freemarker's DefaultObjectWrapper, so that it understands Joda Time out of the box:

public class JodaAwareObjectWrapper extends DefaultObjectWrapper {

  @Override
  public TemplateModel wrap(final Object obj) throws TemplateModelException {
    if (obj == null) { return super.wrap(obj); }
    if (obj instanceof LocalDate) { return new SimpleDate(((LocalDate) obj).toDate(), TemplateDateModel.DATE); }
    // add support for all desired types here...
    return super.wrap(obj);
  }
}

and feed this object wrapper to the FreeMarker config when you fire up your FreeMarker engine

Configuration config = // ...
config.setObjectWrapper(new JodaAwareObjectWrapper());

You can then use FreeMarkers standard date built ins, such as ${dateTime?date} in your templates


I do not believe at this time there is any integration in Freemarker for JodaTime. It is pretty easy to put an object in your model for formatting, i.e.

Write a class "MyCustomJodaFormatterBean", with a format(String pattern, DateTime dateTime) method. Put an instance of this in the root.

root.put("joda", new MyCustomJodaFormatterBean());

Then in freemarker,

${joda.format("MM-dd-yyy", dateTime)}


During parsing of FTL files freemarker builds its internal model of objects. For example java.util.Date expressions are wrapped into freemarker.template.SimpleDate. If expression value of your model is of type org.joda.time.DateTime - which is unknown for freemarker, it will be wrapped by default into freemarker.ext.beans.StringModel, converting your DateTime to string using toString() method.

For example, assume we have in FTL expression like:

med.expiryDate?date <= today?date

Where "med.expiryDate" is of type DateTime. "med.expiryDate" will be wrapped into freemarker.ext.beans.StringModel and after this "med.expiryDate?date" will be parsed used freemarker.template.Configuration dateFormat. Which can leed to exception if this dateFormat is different then default format of DateTime.toString().

To fix this you need to make Freemarker understand that DateTime is also a date, not a string. Write your custom object wraper:

/**
 * Wrapper to make freemarker identify org.joda.time.DateTime objects as date.
 * User: evvo
 * Date: 5/26/2016
 * Time: 18:21
 */
public class DateTimeAwareObjectWrapper extends DefaultObjectWrapper {

   @Override
   public TemplateModel wrap(Object obj) throws TemplateModelException {
      if (obj instanceof DateTime) {
         return new SimpleDate(((DateTime) obj).toDate(), getDefaultDateType());
      }
      return super.wrap(obj);
   }
}

And set it into freemarker configuration

configuration.setObjectWrapper(new DateTimeAwareObjectWrapper());

After such change I belive ?string suffix will also work on DateTime expression.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号