JSF Primefaces selectonemenu页面导航

时间:2014-09-20 19:28:23

标签: list jsf primefaces navigation

我正在使用JSF和Primefaces开发一个Web应用程序。我想要显示以下菜单,并根据选择的选项转到一页或另一页。

XHTML代码:

<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>

托管bean代码:

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;
    }
 }

如果他从列表中选择BMW选项,用户导航到pageBMW.xhtml,如果他从列表中选择Mercedes选项,则导航到pagemercedes.xhtml?

2 个答案:

答案 0 :(得分:2)

我会将更改事件的AJAX附加到selectOneMenu,它会调用bean上的方法并通过它重定向,类似于selectOneMenu ajax events(AJAX事件)和Sending a redirect from inside an ajax listener method的相应接受答案的组合{3}}(重定向)。

答案 1 :(得分:0)

您可以通过AJAX调用处理car的值,并重定向bean类的响应。

<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>

在你的bean类中,类似

  public void someAction() {
        String location = "page"+car+".xhtml";
        FacesContext.getCurrentInstance()
            .getExternalContext()
            .redirect(location)
        ;
    }
}