我如何导航到 JSF 中的另一个页面?

How can I navigate to another page in JSF?

我正在使用 JSF/PrimeFaces 开发小型 Web 应用程序。我可以从网络调用 bean,但是,我无法 return 网络。这是我的:

requestLeave.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
        <title>Request leave</title>
    </h:head>

    <h:body>
        <script language="javascript">
            var today = new Date();
            document.write(today);
        </script>

        <h:form id="form"  >
            <h:panelGrid id= "grid" columns="2" cellpadding="5">
                <f:facet name="header">
                    <p:messages id="msgs" />
                </f:facet>

                <h:outputLabel for="title" value="Leave Title:" style="font-weight:bold" />
                <p:inputText  value="#{requestLeave.titleLeave}" required="true" requiredMessage="title is required." />

                <h:outputLabel for="type" value="Type:" style="font-weight:bold" />
                <p:inputText  value="#{requestLeave.typeLeave}" required="true" requiredMessage="type is required." />

                <p:commandButton value="Submit" actionListener="#{requestLeave.buttonAction}"  />
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

RequestLeave.java

import javax.faces.bean.ManagedBean;    

@ManagedBean    
public class RequestLeave {

    private String titleLeave;
    private String typeLeave;

    public String getTitleLeave() {
        return titleLeave;
    }

    public void setTitleLeave(String titleLeave) {
        this.titleLeave = titleLeave;
    }

    public String getTypeLeave() {
        return typeLeave;
    }

    public void setTypeLeave(String typeLeave) {
        this.typeLeave = typeLeave;
    }

    public String buttonAction() {

        System.out.println("leave title " + titleLeave);
        System.out.println("leave type " + typeLeave);

        return ("index.jsp");
    }
}

有了这个,我不能return到index.jsp。谁能帮我?提前致谢。

actionListener 属性更改为 action:

<p:commandButton value="Submit" action="#{requestLeave.buttonAction}" />

更多信息:

  • Differences between action and actionListener?