0

I am developing a web application using JSF and Primefaces. I want o show the following menu and depending on the choosen option go to one page or another.

XHTML code:

<p:outputLabel for="car" value="Grouping: " />
        <p:selectOneMenu id="car" value="#{selectOneMenuView.car}">
            <f:selectItem itemLabel="Select One" itemValue="" />
            <f:selectItems value="#{selectOneMenuView.cars}" />
        </p:selectOneMenu>

Managed bean code:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;


ManagedBean
public class SelectOneMenuView {

    private String console;

    private String car; 
    private List<SelectItem> cars;

    private String city; 
    private Map<String,String> cities = new HashMap<String, String>();

    private Theme theme;  
    private List<Theme> themes;

    @ManagedProperty("#{themeService}")
    private ThemeService service;

    @PostConstruct
    public void init() {
        //cars
        SelectItemGroup g1 = new SelectItemGroup("German Cars");
        g1.setSelectItems(new SelectItem[] {new SelectItem("BMW", "BMW"), new SelectItem("Mercedes", "Mercedes"), new SelectItem("Volkswagen", "Volkswagen")});

        SelectItemGroup g2 = new SelectItemGroup("American Cars");
        g2.setSelectItems(new SelectItem[] {new SelectItem("Chrysler", "Chrysler"), new SelectItem("GM", "GM"), new SelectItem("Ford", "Ford")});

        cars = new ArrayList<SelectItem>();
        cars.add(g1);
        cars.add(g2);

public String getCar() {
        return car;
    }

    public void setCar(String car) {
        this.car = car;
    }
 }

How can I do so that an user navigates to pageBMW.xhtml if he chooses the BMW option from the list or to pagemercedes.xhtml if he chooses the Mercedes option from the list?

william
  • 103
  • 1
  • 2
  • 9

2 Answers2

2

I would append an AJAX on change event to the selectOneMenu which calls a method on your bean and redirects through it similar to a combination of the respective accepted answers of selectOneMenu ajax events (AJAX event) and Sending a redirect from inside an ajax listener method (redirect).

Community
  • 1
  • 1
Smutje
  • 17,733
  • 4
  • 24
  • 41
0

You might process the value of car via an AJAX call and redirect the response from your bean class.

<p:selectOneMenu id="car" value="#{selectOneMenuView.car}">
        <f:selectItem itemLabel="Select One" itemValue="" 
            noSelectionOption="true" />
        <f:selectItems value="#{selectOneMenuView.cars}" />
        <p:ajax listener="#{selectOneMenuView.someAction()"
            process="@this" partialSubmit="true" />
</p:selectOneMenu>

And within your bean class something like

  public void someAction() {
        String location = "page"+car+".xhtml";
        FacesContext.getCurrentInstance()
            .getExternalContext()
            .redirect(location)
        ;
    }
}
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
stg
  • 2,757
  • 2
  • 28
  • 55
  • Thanks for your response, the code works. Could you explain me how to do this with a commandbutton? I mean the user chooses one car ,then he clicks on the button and navigates to pageNameOfTheCar.xhtml – william Sep 20 '14 at 23:08
  • 1
    @william simply call `someAction` through a command button - are you even *trying* to understand the Primefaces documentation by yourself? – Smutje Sep 21 '14 at 09:21
  • could you explain it better, with an example? – william Sep 21 '14 at 13:44