RobD
RobD

Reputation: 455

PrimeFaces navigation using MenuItem

I have a PrimeFaces 3.4 Menubar like this:

<h:form>
    <p:menubar>
        <p:submenu label="File">
            <p:submenu label="New">
                <p:menuitem value="Go" id="page1LinkID" action="#{navBean.goNav}">     
                    <f:param id="page1ParamID" name="pageViewID" value="Page1"/>
                </p:menuitem>
                <p:menuitem value="Other" url="#"/>
            </p:submenu>
        </p:submenu>
    </p:menubar>
</h:form>

the backing bean is like this

@ManagedBean
@RequestScoped
public class navBean {

/**
 * Creates a new instance of navBean
 */
private String includedPage = "contentMain.xhtml";
public navBean() {
}
public String getPage(){
    return includedPage;
}
public void setPageName(){
    this.includedPage = includedPage;
}

public String goNav() {
   FacesContext context = FacesContext.getCurrentInstance();
   String selectedPageViewId = context.getExternalContext().getRequestParameterMap().get("pageViewId");
   if (selectedPageViewId.equalsIgnoreCase("page1")){
        includedPage = "test.xhtml";
    }
}   

Problem is it does not work, no exceptions nothing. I originally tried putting the action into like this... Changing goNav to void...nothing.

Any Ideas? Thanks In Advance Rick

Upvotes: 4

Views: 24340

Answers (2)

Serkan Arıkuşu
Serkan Arıkuşu

Reputation: 5619

Primefaces menuitem has a url attribute for this case:

url: Url to be navigated when menuitem is clicked.

If you are not using "url" but want to go with "action" then remember that you are doing a post request.

In this case you should either

  1. Add ?faces-redirect=true to your returning String, like "test.xhtml?faces-redirect=true"
  2. Set ajax=false in your menuitems, like

See this answer from the lead developer of primefaces

Upvotes: 7

satya
satya

Reputation: 164

try this

   public String goNav() {
       FacesContext context = FacesContext.getCurrentInstance();
       String selectedPageViewId = context.getExternalContext().getRequestParameterMap().get("pageViewId");
       if (selectedPageViewId.equalsIgnoreCase("page1")){
           includedPage = "test.xhtml?redirect=true";

       }
       return includedPage;
}

Upvotes: 0

Related Questions