xav56883728
xav56883728

Reputation: 315

JSF/PrimeFaces selectOneMenu change url parameter and redirect

I'm using JSF2 and PrimeFaces. How can I write selectOneMenu that would invoke JSF navigation to redirect user to the same page but with diferent parameters when he change option in menu? I only need to change color.

From url:

    <f:metadata>
        <f:viewParam name="vehicle" value="#{myViewController.vehicle}"/>
        <f:viewParam name="vehicle" value="#{myViewController.color}"/>
    </f:metadata>

MyView.xhtml: (no work)

    <h:form id="selectOneMenuEdition">
    <p:selectOneMenu id="selectOneMenuHeader"
                     value="#{selectOneMenuHeader.outcome}" >
        <f:selectItem itemValue="viewVehicles.xhtml?vehicle=#{myViewController.vehicle}&amp;color=red" itemLabel="Vehicle red " />
        <f:selectItem itemValue="viewVehicles.xhtml?vehicle=#{myViewController.vehicle}&amp;color=blue" itemLabel="Vehicle blue" />
        <p:ajax event="change" listener="#{selectOneMenuHeader.navigate}" />
    </p:selectOneMenu>
    </h:form>

ManagedBean:

private String outcome;

public void navigate() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect(outcome);
}

Upvotes: 3

Views: 2472

Answers (1)

Thrax
Thrax

Reputation: 1964

Your <p:selectOneMenu> is bound to selectOneMenuHeader.outcome, so you just need to get its value in your bean.

Then you can create your own URL to redirect the user with the desired parameters.

Example (bean) :

public void navigate() throws IOException {
    String url = "yourPage.jsf?color=" + outcome;
    FacesContext.getCurrentInstance().getExternalContext().redirect(url);
}

A second solution would be to add a javascript onchange event to redirect the user by taking the current selected option.

Example (vanilla JS) :

onchange="document.location.href='yourPage' + this.options[this.selectedIndex].value;"

Upvotes: 3

Related Questions