从托管 bean 事件 JSF 重定向

Err*_*ion 0 java redirect jsf-2

我有一个包含饼图的 JSF 页面。这个想法是,当点击饼图的扇区时,应用程序会重定向到另一个页面。我面临重定向的挑战,因为它只将完整 url 带到非动态页面,因为它会根据环境而变化。我的 ItemSelect 方法是:

public void itemSelect(ItemSelectEvent event) {

        try {

           FacesContext.getCurrentInstance().
             getExternalContext().redirect("http://localhost:8084/SIMBANK_BI/faces/OpenedAccountsProductTypeLevel.xhtml");

        } catch (IOException asd) {
            System.err.println(asd.getMessage());
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的 JSF 页面:

 <left>
             <p:growl id="growl" showDetail="true" />
            <p:pieChart id="custom" value="#{OpenedAccountsBranchLevelBean.pieModel}" legendPosition="w" showDataLabels="true"   
                style="width:500px;height:400px" sliceMargin="2">
                  <p:ajax event="itemSelect" listener="#{OpenedAccountsBranchLevelBean.itemSelect}" update="growl" />
            </p:pieChart>
        </left> 
Run Code Online (Sandbox Code Playgroud)

我试过了;

  FacesContext.getCurrentInstance().
                 getExternalContext().redirect("/faces/OpenedAccountsProductTypeLevel.xhtml");
Run Code Online (Sandbox Code Playgroud)

我也试过:

 FacesContext.getCurrentInstance().
                 getExternalContext().redirect("/OpenedAccountsProductTypeLevel.xhtml");
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在不必放入整个 URL 的情况下重定向?

小智 5

尝试使用

 FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest origRequest = (HttpServletRequest)context.getExternalContext().getRequest();
    contextPath = origRequest.getContextPath();
try {
        FacesContext.getCurrentInstance().getExternalContext()
                .redirect(contextPath  + "/faces/OpenedAccountsProductTypeLevel.xhtml");
    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)