简体   繁体   中英

Trouble with using Freemarker to formate Date for GeoServer GetFeatureInfo

I'm having some trouble while trying to use the Freemarker Template to display dates in the desired format.

I store points with a date information in a PostGIS database written through an FME-process in an ISO format (%Y-%m-%d) to use them in an time-enabled WMS with GeoServer.

When calling the GetFeatureInfo, the date is displayed in the following format 10/4/12 12:00 AM, where it should be 2012-10-04. We allready changened the server setting to -Dorg.geotools.localDateTimeHandling=true -Duser.country=DE -Duser.timezone=GMT -Duser.language=de.

Since this didn't give the desired outcome, we tried it with the Freemarker Template. The idea was to check the attributes for date format and format them accordingly. Somehow, I can't make it work. I tried this:

<#if attribute.is_unknown_date_like>
${attribute.value?string("YYYY-MM-DD")}
<#else>
${attribute.value}
</#if>

I get an error message for the line where the condition starts:

freemarker.core.ParseException

How can I make this condition statement work?

I'm having some trouble while trying to use the Freemarker Template to display dates in the desired format.

I store points with a date information in a PostGIS database written through an FME-process in an ISO format (%Y-%m-%d) to use them in an time-enabled WMS with GeoServer.

When calling the GetFeatureInfo, the date is displayed in the following format 10/4/12 12:00 AM, where it should be 2012-10-04. We allready changened the server setting to -Dorg.geotools.localDateTimeHandling=true -Duser.country=DE -Duser.timezone=GMT -Duser.language=de.

Since this didn't give the desired outcome, we tried it with the Freemarker Template. The idea was to check the attributes for date format and format them accordingly. Somehow, I can't make it work. I tried this:

<#if attribute.is_unknown_date_like>
${attribute.value?string("YYYY-MM-DD")}
<#else>
${attribute.value}
</#if>

I get an error message for the line where the condition starts:

freemarker.core.ParseException

How can I make this condition statement work?

Updated : Added parsing to the #else branch, etc.

You could do this if you don't know if attribute.value will be java.lang.String or a java.util.Date :

<#if attribute.value?is_date_like>
${attribute.value?date?string.iso}
<#else>
${attribute.value?date("M/d/yy hh:mm a")?string.iso}
</#if>

If you know the type of attribute.value , then you only have to do what's inside the #if , or the #else .

If you are using a really old version of FreeMarker, then instead of ?string.iso , you have to use ?string("yyyy-MM-dd") . Also then ?is_date_like might not be available yet, and you had to use attribute.value?is_unknown_date_like || attribute.value?is_datetime || attribute.value?is_date attribute.value?is_unknown_date_like || attribute.value?is_datetime || attribute.value?is_date attribute.value?is_unknown_date_like || attribute.value?is_datetime || attribute.value?is_date .

By the way, if you typically output date/time values with ISO format, then someone should just set the date_format / time_format / datetime_format configuration settings to iso , and then you can omit ?string.iso . (Or, these can be set in the template too, like <#setting date_format='iso'> , etc.)

@ddekany, you are rigth, I am sorry. I will keep that in mind for the future!

So I tried your suggestion, and as statet above using a ? still gives back a errror message but it gets more significant.

2020-07-01 08:19:02,521 ERROR [geoserver.ows] - freemarker.core.ParseException: Error on line 29, column 46, in template content.ftl Found is_date_like, expecting one of: is_directive, parent, js_string, j_string, uncap_first, is_transform, number, is_hash, trim, children, has_content, iso_ms, xml, iso_utc, byte, double, left_pad, matches, capitalize, number_to_datetime, contains, size, iso_local_h_nz, iso_utc_ms, iso_local_m_nz, is_collection, long, default, iso_utc_h_nz, iso_local_ms, is_boolean, last_index_of, c, iso_utc_m_nz, is_macro, rtf, iso_utc_nz, upper_case, node_name, reverse, cap_first, url, is_hash_ex, iso_nz, is_enumerable, exists, number_to_date, first, iso_local, date, iso, replace, float, right_pad, datetime, node_type, split, iso_ms_nz, number_to_time, is_sequence, iso_utc_m, html, ancestors, iso_utc_h, iso_local_ms_nz, new, last, sort, eval, lower_case, web_safe, is_date, is_string, iso_local_nz, word_list, seq_last_index_of, node_namespace, string, keys, iso_m_nz, values, seq_index_of, chunk, sort_by, iso_m, starts_with, substring, index_of, iso_h, root, floor, iso_h_nz, ceiling, if_exists, chop_linebreak, iso_local_h, length, is_indexable, groups, is_node, iso_local_m, int, iso_utc_ms_nz, xhtml, ends_with, round, interpret, is_method, namespace, short, seq_contains, time, is_number in content.ftl

So, the problem seems to lie within the built-in. As statet in the error message, Freemarker expects is_date instead of is_date_like , eventhough in the Freemarker documentation it is states that is_date_like should be uses instead of is_date link . So I tried your suggestion with is_date .

Now, no error message appears, but the date format is unchanged.

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