问题描述
我正在使用JSF和Primefaces开发Web应用程序.我要显示以下菜单,然后根据选择的选项转到一页或另一页.
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代码:
<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?
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?
推荐答案
我将在change事件上添加AJAX到selectOneMenu
上,该事件在您的bean上调用方法并通过它重定向,类似于各自接受的答案的组合 selectOneMenu ajax事件(AJAX事件)和(重定向).
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).
这篇关于JSF Primefaces selectonemenu页面导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!