Liferay 在FreeMarker中分析日期时出错
我在第一次越战时出错了。我们的语言设置运行良好,但更改为VN后出现错误Liferay 在FreeMarker中分析日期时出错,liferay,freemarker,Liferay,Freemarker,我在第一次越战时出错了。我们的语言设置运行良好,但更改为VN后出现错误 Error: on line 20, column 11 in 10157#10197#22325 The string doesn't match the expected date/time format. The string to parse was: "CN, 15 thg 7 2018 18:54:00 +0700". The expected format was: "EEE, dd MMM yyyy
Error: on line 20, column 11 in 10157#10197#22325
The string doesn't match the expected date/time format. The string to parse was: "CN, 15 thg 7 2018 18:54:00 +0700". The expected format was: "EEE, dd MMM yyyy hh:mm:ss zzz".
我使用liferay 6.2,使用Freemarker模板显示web内容。这是我的密码
<#assign articleDisplayDate = .vars['reserved-article-display-date'].data />
<#assign AssetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService" )>
<#assign JournalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService")>
<#assign currentArticle = JournalArticleLocalService.getArticle(getterUtil.getLong(groupId),.vars['reserved-article-id'].data)>
<#assign currentArticleResourcePrimKey = currentArticle.getResourcePrimKey()>
<#assign currentArticleAssetEntry = AssetEntryLocalService.getEntry("com.liferay.portlet.journal.model.JournalArticle", currentArticleResourcePrimKey)/>
<div class="wrapcap">
<h3 class="title">
<a href="">${.vars['reserved-article-title'].data}</a>
</h3>
</div>
<div class="datecomm">
<span>
<i class="fa fa-calendar" aria-hidden="true"></i>
<span>${articleDisplayDate?date("EEE, dd MMM yyyy hh:mm:ss zzz")?string("MMMM, dd yyyy")}</span>
</span>
<span><i class="fa fa-eye" aria-hidden="true"></i> Số lượt xem: <span>${currentArticleAssetEntry.viewCount}</span></span>
</div>
<div class="desccomm">
<p></p>
</div>
<div class="contentcomm">
${content.getData()}
</div>
${articleDisplayDate?日期(“EEE,dd-MMM-yyyy hh:mm:ss-zzz”)?字符串(“MMMM,dd-yyyy”)}
số lượt xem:${currentArticleAssetEntry.viewCount}
${content.getData()}
不知道此日期格式。它正在正确报告错误 让我们一起验证一下: 字符串与预期的日期/时间格式不匹配。串到 解析为:“中国,2018年7月15日18:54:00+0700”。预期格式 was:“EEE,dd MMM yyyy hh:mm:ss zzz” 中国,2018年7月15日18:54:00+0700 所以EEE是CN,检查 dd是15,检查 嗯。我不知道,但检查一下 yyyy是7岁。我觉得不太对 hh是2018年。绝对不对
等等等等等等。它正在正确地报告错误 让我们一起验证一下: 字符串与预期的日期/时间格式不匹配。串到 解析为:“中国,2018年7月15日18:54:00+0700”。预期格式 was:“EEE,dd MMM yyyy hh:mm:ss zzz” 中国,2018年7月15日18:54:00+0700 所以EEE是CN,检查 dd是15,检查 嗯。我不知道,但检查一下 yyyy是7岁。我觉得不太对 hh是2018年。绝对不对 等等等等等等这是测试结果 英语:
- ${articleDisplayDate}:太阳,2018年7月15日18:54:00+0700
- ${.now?字符串(“EEE,dd MMM yyyy hh:mm:ss zzz”)}:2018年8月20日星期一10:46:48 GMT+07:00
- ${articleDisplayDate}:CN,2018年7月15日18:54:00+0700
- ${.now?字符串(“EEE,dd MMM yyyy hh:mm:ss zzz”)}:2018年8月20日星期一10:47:46 GMT+07:00 这是测试结果 英语:
- ${articleDisplayDate}:太阳,2018年7月15日18:54:00+0700
- ${.now?字符串(“EEE,dd MMM yyyy hh:mm:ss zzz”)}:2018年8月20日星期一10:46:48 GMT+07:00
- ${articleDisplayDate}:CN,2018年7月15日18:54:00+0700
- ${.now?字符串(“EEE,dd MMM yyyy hh:mm:ss zzz”)}:2018年8月20日星期一10:47:46 GMT+07:00
区域设置
FreeMarker配置设置是某种英语(请尝试查看${.locale}
)。在分析日期/时间/日期时间甚至数字时,当前区域设置很重要,因为日期和月份等的名称取决于区域设置
因此,您应该将locale
设置为越南语。通常,locale
FreeMarker配置设置设置在模板外部。如果各个模板处理都使用相同的公共配置
对象,则可以在freemarker.core.Environment
对象上分别为每个模板处理设置不同的区域设置。如果出于某种原因必须在模板内设置区域设置,也可以:
<#setting locale="vi_VN">
${"CN, 15 thg 7 2018 18:54:00 +0700"?datetime("EEE, dd MMM yyyy hh:mm:ss zzz")}
${“中国,15 thg 7 2018 18:54:00+0700”?日期时间(“EEE,dd MMM yyyy hh:mm:ss zzz”)}
还要注意,我使用的是
?datetime
,而不是?date
。最后一个仅表示日期,这可能会导致其他地方出现一些故障,因为您在那里也有时间。从您提供的示例中,问题似乎是您的locale
FreeMarker配置设置是某种英语(请尝试${.locale}
查看)。在分析日期/时间/日期时间甚至数字时,当前区域设置很重要,因为日期和月份等的名称取决于区域设置
因此,您应该将locale
设置为越南语。通常,locale
FreeMarker配置设置设置在模板外部。如果各个模板处理都使用相同的公共配置
对象,则可以在freemarker.core.Environment
对象上分别为每个模板处理设置不同的区域设置。如果出于某种原因必须在模板内设置区域设置,也可以:
<#setting locale="vi_VN">
${"CN, 15 thg 7 2018 18:54:00 +0700"?datetime("EEE, dd MMM yyyy hh:mm:ss zzz")}
${“中国,15 thg 7 2018 18:54:00+0700”?日期时间(“EEE,dd MMM yyyy hh:mm:ss zzz”)}
还要注意,我使用的是?datetime
,而不是?date
。最后一个仅表示日期,这可能会导致其他地方出现一些故障,因为您在那里也有时间。
似乎liferay对这个bug的处理非常糟糕
我在这里用代码解决了这个问题:
<#-- Retrieve the published date meta data field of the web content -->
<#assign articleDisplayDate = .vars['reserved-article-display-date'].data />
<#-- Save the original page locale for later -->
<#assign originalLocale = locale>
<#-- Set the page locale to the portals default locale -->
<#setting locale = 'en_US'>
<#-- Parse the date to a date object use locale en-US -->
<#assign date = articleDisplayDate?date("EEE, dd MMM yyyy hh:mm:ss zzz")>
<#-- Create date time formart use originalLocale (Your site localle) -->
<#assign dateTimeFormat = languageUtil.get(originalLocale, "MMMM, dd yyyy")>
<#-- Result -->
<#assign date = date?string(dateTimeFormat)>
似乎liferay对这个bug的处理非常糟糕
我在这里用代码解决了这个问题:
<#-- Retrieve the published date meta data field of the web content -->
<#assign articleDisplayDate = .vars['reserved-article-display-date'].data />
<#-- Save the original page locale for later -->
<#assign originalLocale = locale>
<#-- Set the page locale to the portals default locale -->
<#setting locale = 'en_US'>
<#-- Parse the date to a date object use locale en-US -->
<#assign date = articleDisplayDate?date("EEE, dd MMM yyyy hh:mm:ss zzz")>
<#-- Create date time formart use originalLocale (Your site localle) -->
<#assign dateTimeFormat = languageUtil.get(originalLocale, "MMMM, dd yyyy")>
<#-- Result -->
<#assign date = date?string(dateTimeFormat)>
问题可能在于日期和月份的名称。那个日期用什么语言(为什么用拉丁字母)?请尝试${.now?string(“EEE,dd MMM yyy hh:mm:ss zzz”)}
查看预期格式的外观。日期使用越南语。我在下面发布了测试结果。也许问题出在日期和月份的名称上。那个日期用什么语言(为什么用拉丁字母)?请尝试${.now?string(“EEE,dd MMM yyy hh:mm:ss zzz”)}
查看预期格式的外观。日期使用越南语。测试结果我贴在下面。我认为报告也正确。但在使用越南语时出现了错误,尽管英语很好。不知道。我也认为报告正确。但在使用越南语时出现了错误,尽管英语很好。不知道。这应该是你问题的一部分(你可以编辑它),而不是答案。这应该是你问题的一部分(你可以编辑它),而不是答案。