Search code examples
jsf-2tomcat6myfacesresourcebundle

JSF 2.0 EL handeling nulls for resource keys


Just a quick question for you guys.

I have a resource key that is stored as a string in a managed bean and I'd like to get it to resolve to the value in a particular mapped Resource Bundle.

Here is what I started with:

<h:outputText value="#{msgs[bean.someVal]}"/>

I immediatly noticed that when someVal was null I would get the following exception:

javax.el.PropertyNotFoundException: /webpage.xhtml at line 118 and column 188 value="#{msgs[bean.someVal]}": Property '' not found on type java.util.PropertyResourceBundle

So I tried to set up a ternary like this:

<h:outputText value="#{bean.someVal == null ? '' : msgs[bean.someVal]}"/>

But I got the same error only quoting the new value.

I'm running JSF2.0 (Apache) on Tomcat6.

Anyone got any ideas? I'm pretty stumped on this one..

Let me know if you need more info, I hope this is enough to go on.. I'm thinking this is just something dumb that I'm doing ;)


Solution

  • Property '' not found

    You've there an empty string. An empty string is not the same as null.

    Use the empty check instead. It will check if the value is not null and if it is not an empty string.

    <h:outputText value="#{empty bean.someVal ? '' : msgs[bean.someVal]}" />
    

    An alternative, by the way, is to provide a custom ResourceBundle implementation on #{msgs} which doesn't throw the exception, but instead returns null or an empty String on handleGetObject() method.