views:

118

answers:

1

I want to read pattern for JST formatDate also from resource bundle but this naive approach does not working, what I'm doing wrong ?

in com/company/MyPortlet.properties is this key:

company.date.format = yyyy-MM-dd HH:mm:ss

In page I have:

<fmt:setBundle basename="com.company.MyPortlet"/>
<fmt:formatDate value="${date}"  pattern="${company.date.format}" />
+1  A: 

You need to give the bundle a variable name.

<fmt:setBundle basename="com.company.MyPortlet" var="bundle" />

This way bundle is accessible in the page by ${bundle}. You can get messages by fmt:message and you can use its var attribute to store it in a local scope. Then you can use it in the pattern attribute of the fmt:formatDate

<fmt:message bundle="${bundle}" key="company.date.format" var="pattern" />
<fmt:formatDate value="${date}" pattern="${pattern}" />
BalusC
Looks reasonable but I'm getting javax.el.PorpertyNotFoundException with "property 'company.date.format' not found on type javax.servlet.jsp.jstl.fmt.LocalizationContext" .
binary_runner
Oh, I'm sorry, it has been a long time ago I used them for the last since I switched to JSF and the initial approach works fine with JSF's `f:loadBundle` with which I got confused. I've updated the answer now.
BalusC
Ah, it looks so unnecessarily complex :), I didn't see it. This is the answer, thanks alot.
binary_runner
You're welcome. Yes, that's what you pay for an ancient i18n library :) You can also consider to load the bundle yourself in a session scoped bean extending `ResourceBundle`.
BalusC