简体   繁体   中英

JSF/Primefaces - ui:param in p:ajax element

I'm writing an application in Java EE 6 and using Primefaces 3.4.1 for the user interface.

I have something like that.

genericPage.xhtml

<ui:composition template="mainApplicationTemplate.xhtml" [...]>
    <p:tabView id="tabView" dynamic="false"cache="false">
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form1">
                ...
            </h:form>
        </p:tab>
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form2">
                ...
            </h:form>
        </p:tab>
    </p:tabView>
<ui:composition >

child1.xmtml

<ui:composition template="genericPage.xhtml" ...>
    <ui:param name="actionBean" value="#{actionBeanA}"/>
</ui:compisition>

child2.xmtml

<ui:composition template="genericPage.xhtml" ...>
    <ui:param name="actionBean" value="#{actionBeanB}"/>
</ui:compisition>

The idea behind that is that child1.xhtml and child2.xhtml share the same jsf code, all contained in the genericPage.xhtml, but they have different backend bean (parametrised by "actionBean")

So far that works really well. It gets complicated when I put the ui parameter inside an <p:ajax/> element.

From the backend bean, I need to programmatically update the active tab, leaving the other untouched. In order to that, I need to store the active tab in the action bean, When certain external events occur, the action bean updates the active tabs.

Note that due to some other factors:

  • I cannot set dynamic="true" in the tabView
  • I cannot have a global form around the tabView and thus cannot use the 'activeIndex' property (which I am doing in an other part of the application) to manage the active tab.

What I want to do

To solve this issue, I want to use the tabChange event of the tabView element:

<p:tabView id="tabView" dynamic="false"cache="false">
        <p:ajax event="tabChange" listener="#{actionBean.listen}"
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form1">
                ...
            </h:form>
        </p:tab>
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form2">
                ...
            </h:form>
        </p:tab>
    </p:tabView>

action bean

@Named
@WindowScoped
public class ActionBeanA implements Serializable{
    public void listen(TabChangeEvent event){
        ...
    }

}

What doesn't work

When I do that, I'm getting the error

Target Unreachable, identifier 'actionBean' resolved to null: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'actionBean' resolved to null

This seems to indicate that the <p:ajax> element has not been passed the action bean and therefore doesn't know what actionBean is.

However if I change the signature of the listener method like that

<p:ajax event="tabChange" listener="#{actionBean.listen(anything)}"

and change the backend bean to something like:

public void listen(TabChangeEvent event){
    System.out.println(event.toString());
}

Doing that, I am not getting the target unreachable error but a null pointer exception in the listen method (because I haven't assigned a value to "anything"). That shows that in this case, the <p:ajax/> elements knows what actionBean is and manages to call the method in the bean.

Questions

How could I get around this problem? I'd like to be able, on a tab change event, to send to my backend bean the new active tab.

I was also having this problem and found the solution in Primefaces issue tracker: Solution found in the post #20

To summarise, this is a known problem in Primefaces. The semi-fix is found in the release 3.4.2 and requires some changes to the code. At the moment you have:

public void listen(TabChangeEvent event){
    System.out.println(event.toString());
}

Which does not work. You should change your code to:

public void listen(AjaxBehaviorEvent event){
    System.out.println(event.toString());
}

And if you need to use specific methods of TabChangeEvent then you need to do the casting: (TabChangeEvent)event .

It has a status fixed on the issue tracker so it may require this as a permanent solution.

There's a difference between value and method expression processing. For the latter, you need to have the parameter available a bit later - in the Facelet Context.

To include the value of the parameter into the facelet context, you simply have to put the parameter value definition inside of an <f:metadata> tag.

Also, note that the <f:metadata> tag should always be a direct child of the <f:view> tag, so I would do something like this:

template.xhtml

<f:view ....>
    <ui:insert name="metadata"/>
    ....
</f:view>

childPage.xhtml

<ui:composition template="/WEB-INF/template.xhtml">
    <ui:define name="metadata">
        <f:metadata>
            <ui:param name="actionBean" value="#{actionBeanA}"
        </f:metadata>
    </ui:define>
</ui:composition>

After doing this, you may use the listener attribute in the template.xhtml the original way with no workarounds and your method will always be called with the appropriate event type parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM