Question

I might not be thinking correctly in terms of visual components in JSF, but I guess that's part of my question. My question is around the seeming lack of scope around variables declared within JSF <ui:component> implementations.

So, say I have /resources/comp/myPanel.xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core" 
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:c="http://java.sun.com/jsp/jstl/core"      
              xmlns:cc="http://java.sun.com/jsf/composite">
    <cc:interface>
    </cc:interface>
    <cc:implementation>
        <f:loadBundle var="bundle" basename="panelOnly.bundle" />

        <h:outputText value="#{bundle.myText}" />
    </cc:implementation>
</ui:component>

And there is a resource bundle that gets loaded in that component, panelOnly/bundle.properties:

myText = This is a panel resource

And then I have a page that places the myPanel component, mainPage.xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:comp="http://java.sun.com/jsf/composite/comp">
    <h:body>
        <f:view>
            <f:loadBundle basename="mainPage.bundle" var="bundle" />

            <comp:myPanel />

            <h:outputText value="#{bundle.myText}" />
        </f:view>
    </h:body>
</html>

and there is a resource bundle that gets loaded in the main page, mainPage/bundle.properties:

myText = This is a main page resource

Now, I would assume that my page should render as:

This is a panel resource
This is a main page resource

But, instead, I get:

This is a panel resource
This is a panel resource

And I assume that is because I clobbered what the "bundle" symbol refers to in my component so that when the mainPage.xhtml tries to resolve that value, it looks to the component's "bundle" object and not the original mainPage's.

My workaround to date has been to just use unique named variables within my components that would never clash with variables on my main pages. But I would prefer if there was a way to coax JSF to recognize anything declared in my component as locally scoped variables and not clobber the caller's symbols.

I think there are and other tags that one can use to make locally scoped variables under #{cc.attrs...}. If you could enumerate my local scoping options in your answer, that would be very helpful. I suspect my <f:loadBundle> is a special case, and maybe there isn't a workaround for that one as it was not designed with <ui:component> in mind.

Thanks!

P.S. I'm running Mojarra 2.1.1 (FCS 20110408)

(edited for formatting and copy and paste bugs 6/15/2011)

Was it helpful?

Solution

Unfortunately, that's how <f:loadBundle> works. It's an one-time setting for the entire view. And any subsequent <f:loadBundle> calls in the same view will just override the previous one.

Your best bet is to manage it by a backing component.

<cc:interface componentType="myPanel">

with

@FacesComponent(value="myPanel")
public class MyPanel extends UIComponentBase implements NamingContainer {

    private ResourceBundle bundle;

    public MyPanel() {
        bundle = ResourceBundle.getBundle("panelOnly.bundle", 
            FacesContext.getCurrentInstance().getViewRoot().getLocale());
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }

    public ResourceBundle getBundle() {
        return bundle;
    }

}

which can be used as

<cc:implementation>
    <h:outputText value="#{cc.bundle.myText}" />
</cc:implementation>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top