本文介绍了JSF/PrimeFaces selectOneMenu更改view-id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF2和PrimeFaces3.我该如何编写selectOneMenu来调用JSF导航,以在用户更改菜单中的选项时将其重定向到另一个页面?

I'm using JSF2 and PrimeFaces3. How can I write selectOneMenu that would invoke JSF navigation to redirect user to another page when he change option in menu?

推荐答案

附加一个Ajax侦听器,并使其通过.

Attach an ajax listener and let it navigate by NavigationHandler.

例如

<h:form>
    <h:selectOneMenu value="#{navigator.outcome}">
        <f:selectItem itemLabel="Select page..." />
        <f:selectItem itemValue="page1" itemLabel="Page 1" />
        <f:selectItem itemValue="page2" itemLabel="Page 2" />
        <f:selectItem itemValue="page3" itemLabel="Page 3" />
        <f:ajax listener="#{navigator.navigate}" />
    </h:selectOneMenu>
</h:form>

(上面的示例在相同的上下文中期望page1.xhtmlpage2.xhtmlpage3.xhtml;您甚至可以将其设置为<f:selectItems>)

(the above example expects page1.xhtml, page2.xhtml and page3.xhtml in the same context; you can even make it a <f:selectItems> instead)

使用

private String outcome;

public void navigate() {
    FacesContext context = FacesContext.getCurrentInstance();
    NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
    navigationHandler.handleNavigation(context, null, outcome + "?faces-redirect=true");
}

?faces-redirect=true不是必需的,但它可以有效地发送重定向,以便浏览器地址栏中的URL可以正确更改,这对于用户体验和页面可书签性而言是更好的.

The ?faces-redirect=true is not necessary, but it effectively sends a redirect so that the URL in browser address bar will properly change which is better for user experience and bookmarkability of the pages.

这篇关于JSF/PrimeFaces selectOneMenu更改view-id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:17