0

I have left panel that contains the link to different pages. The right panel is the one that should be updated with the new page based on the link clicked in the left panel. I want it to be an ajax update so that the left panel, header and footer do not refresh. I tried with ui:include but the page only gets refreshed on the second click. The reference to that thread is

ui:include in richfaces 4 only gets updated on second click

Is there any alternative way to achieve the same in JSF2 and Primefaces 2.2.1. Thanks

Updates

<ui:define name="content">
    <h:form id="form_01">

    <div style="margin:0; padding:0; float:right; width:740px;background:#FFF; ">
        <p:outputPanel id="pagePanel">
            <ui:include src="#{panelMenu.currentPage}.xhtml"></ui:include>
        </p:outputPanel>
    </div>

    <div style="padding:0; float:left;">
        <p:panel  style="width:205px;" header="User Menu">
            <h:panelGrid columns="1">
                <p:commandLink update="pagePanel" action="#{panelMenu.setCurrentPage('/pages/group_message')}">
                    <h:outputText value="Compose"/>
                </p:commandLink>
                <p:separator/>
                <p:commandLink update="pagePanel" action="#{panelMenu.setCurrentPage('/pages/group_detail')}">
                    <h:outputText value="Groups"/>
                </p:commandLink>
                <p:separator/>

                </h:panelGrid>
        </p:panel>
    </div>
    </h:form>
</ui:define>

Backing Bean

@ManagedBean(name="panelMenu")
@SessionScoped

public class PanelMenu {
private String currentPage = "/pages/group_detail";

public String getCurrentPage() {
    System.out.println(Thread.currentThread().getStackTrace()[1]);
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId());
    return currentPage;
}

public void setCurrentPage(String currentPage) {
    System.out.println(Thread.currentThread().getStackTrace()[1]);
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId());
    this.currentPage = currentPage;
}

}

Community
  • 1
  • 1
WuR
  • 85
  • 2
  • 14

1 Answers1

1

Here is an example:

Managed-bean:

@ManagedBean
@ViewScoped
public class PartialNavBean {
    private String pageName = "/pages/group_member";

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }
}

View:

<p:panel id="leftPanel">
    <h:form>
        <p:commandLink value="Group member"
                       action="#{partialNavBean.setPageName('/pages/group_member')}"
                       update="rightPanel"/>
        <br/>
        <p:commandLink value="Group detail"
                       action="#{partialNavBean.setPageName('/pages/group_detail')}"
                       update="rightPanel"/>
    </h:form>
</p:panel>

<p:panel id="rightPanel">
    <ui:include src="#{partialNavBean.pageName}.xhtml"/>
</p:panel>

Update:

I tried with the following:

public String getPageName() {
    System.out.println(Thread.currentThread().getStackTrace()[1]);
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId());
    return pageName;
}

public void setPageName(String pageName) {
    System.out.println(Thread.currentThread().getStackTrace()[1]);
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId());
    this.pageName = pageName;
}

And the output is as follows:

INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14)
INFO: RESTORE_VIEW 1
INFO: pkg.PartialNavBean.setPageName(PartialNavBean.java:20)
INFO: INVOKE_APPLICATION 5
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14)
INFO: RENDER_RESPONSE 6
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14)
INFO: RESTORE_VIEW 1
INFO: pkg.PartialNavBean.setPageName(PartialNavBean.java:20)
INFO: INVOKE_APPLICATION 5
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14)
INFO: RENDER_RESPONSE 6
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • thanks Bhesh for the example but it has the same problem as I pointed out in the link. The page is only refreshed after the second click. if you trace you app you will notice that getPageName is called before setPageName after a link is clicked. – WuR Oct 27 '11 at 16:43
  • @WuR: I have tried it already and it works as it supposed to. `setPageName` is called during the Invoke Application phase and `getPageName` during the Render Response phase. I don't why it's not working in your case. Try adding `immediate="true"` for both the links. – Bhesh Gurung Oct 27 '11 at 16:58
  • @WuR: See the update. You can try with that to see why it's supposed to work. – Bhesh Gurung Oct 27 '11 at 17:08
  • I tried adding `immediate=true` but no effect. I attached a phased listener to get a clear picture where these methods are being called so following sequence show at what stage the methods are being called. `START PHASE RESTORE_VIEW(1) **<-- getPageName called** END PHASE RESTORE_VIEW(1) START PHASE APPLY_REQUEST_VALUES(2) **<-- setPageName called** END PHASE APPLY_REQUEST_VALUES(2) START PHASE RENDER_RESPONSE(6) END PHASE RENDER_RESPONSE(6)` – WuR Oct 27 '11 at 17:21
  • `START PHASE RESTORE_VIEW(1) PanelMenu.getCurrentPage(PanelMenu.java:14) RESTORE_VIEW(1) END PHASE RESTORE_VIEW(1) START PHASE APPLY_REQUEST_VALUES(2) PanelMenu.setCurrentPage(PanelMenu.java:20) APPLY_REQUEST_VALUES(2) END PHASE APPLY_REQUEST_VALUES(2) START PHASE RENDER_RESPONSE(6) END PHASE RENDER_RESPONSE(6)` – WuR Oct 27 '11 at 17:30
  • @WuR: Update the post with your code both view and managed-bean, so that I can see. – Bhesh Gurung Oct 27 '11 at 17:46
  • @WuR: Your exact code works on my machine. I don't know why it's not working on yours. Can you post the code in template too? – Bhesh Gurung Oct 27 '11 at 18:20
  • @BheshGurung I've run into this exact problem on a demo project--however when I tried to recreate it just now it worked. I'm using Primefaces 2.1, I'll test it in a new project and see if I can recreate the error. – Daniel B. Chapman Oct 27 '11 at 20:03
  • @DanielChapman: That means probably some code somewhere else is messing with it. – Bhesh Gurung Oct 27 '11 at 20:25
  • @BheshGurung I am unable to recreate it (partially because my server at the moment is a JBoss5.1 override so the EL processing isn't as full featured. If I figure it out I'll be sure to let you know. However--I can say the problem (as I had diagnosed it a month ago) was that the Javascript was updating the wrong target. I wish I could be of more help, but I'm unable to recreate it. Give it a shot with the latest build of Primefaces and see if that helps. – Daniel B. Chapman Oct 27 '11 at 21:16
  • @DanielChapman: Sorry, I thought I was talking to the OP. But here I am the one who is having the problem. I use Glassfish3.1 and PF2.2.1 and it's impossible to recreate it cause it's not a problem with any of these. I think in the OP's case, I guess some code somewhere else is messing with it. – Bhesh Gurung Oct 27 '11 at 21:52
  • thanks guys but any clue how can i debug the whole app to see if there is some other code messing the whole thing. – WuR Oct 28 '11 at 09:47
  • @WuR Yes, you can debug it via a Javascript debugger (I like Chrome) and see what's getting set where. A possible work around (for the moment) is to use the JSF element to do the updates instead of using Primefaces, however this isn't exactly the most elegant solution. If I have time this weekend I'll see if I can roll back my project to recreate it. Good luck with this one--I'd like to identify it so I can submit a patch. – Daniel B. Chapman Oct 28 '11 at 14:38