Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a resource bundle key does not exist using JSTL tags?

I have a resource file that will have some optional keys. If the optional resource key is not present, I set a default instead. It appears that there is no easy way to determine if a key exists in the resource bundle. So this is what I'm doing to get around it.

<fmt:message var="title" key="login.reg.signup.${signupForm.regfrom}.title" />
<c:if test='${fn:startsWith(title, "??")}'>
    <fmt:message var="title" key="login.reg.signup.default.title" /> 
</c:if>

Is there a better way?

like image 744
Dave Jensen Avatar asked Apr 10 '09 23:04

Dave Jensen


2 Answers

You could write your own JSP tag that does this, so you can then just do:

<my:message var="title" key="${form}.title" default="default.title"/>

The tag implementation could either be your current JSP syntax, or a Java class.

like image 166
Peter Hilton Avatar answered Oct 05 '22 23:10

Peter Hilton


You can use the #{messagesFactory.messages.containsKey('key')} to check.

like image 43
Fernando Abreu Avatar answered Oct 06 '22 00:10

Fernando Abreu