Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
phoenix7360 Avatar asked Jan 22 '13 14:01

phoenix7360


1 Answers

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.

like image 75
bjedrzejewski Avatar answered Nov 08 '22 19:11

bjedrzejewski