3

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);
}
xav56883728
  • 315
  • 1
  • 8
  • 20
  • Where you have `outcome`, do you mean a JSF navigation outcome? Or a URL? – kolossus Nov 06 '14 at 17:09
  • mmm... yes, maybe the example is wrong. I do not know what is the correct way to do this – xav56883728 Nov 06 '14 at 17:22
  • That doesn't really answer my question: the `outcome` variable; is it a jsf navigation outcome or a full URL? – kolossus Nov 06 '14 at 18:45
  • Yes, is a full url in this case, but I think that is not the best option. – xav56883728 Nov 06 '14 at 19:10
  • 1
    There's nothing wrong with your idea in principle, but you're going about it in a clumsy way. Save only the colour to the backing bean and build the URL in the `navigate` method(as against the acrobatics you're trying to perform in the view currently) – kolossus Nov 07 '14 at 14:09

1 Answers1

3

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;"
Thrax
  • 1,926
  • 1
  • 17
  • 32