如何在faces-config.xml中不使用navigation-case的情况下在两个页面之间实现JSF导航

Sre*_*ram 2 jsf jsf-1.2

我正在寻找另一种JSF导航方式,而不是提到导航案例faces-config.xml.

目前我正在使用faces-config.xml导航.我想清理它.

请建议所有其他方式,以便我可以使用任何适合我的需要.

Bal*_*usC 5

对于简单的页面到页面导航(不提交任何内容),您应该使用<h:outputLink>而不是<h:commandLink>.

因此,代替

<h:form>
    <h:commandLink value="Page 1" action="page1" />
    <h:commandLink value="Page 2" action="page2" />
    <h:commandLink value="Page 3" action="page3" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

和那些导航案例

<navigation-rule>
    <navigation-case>
        <from-outcome>page1</from-outcome>
        <to-view-id>page1.jsf</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>page2</from-outcome>
        <to-view-id>page2.jsf</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>page3</from-outcome>
        <to-view-id>page3.jsf</to-view-id>
    </navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)

应该使用

<h:outputLink value="page1.jsf">Page 1</h:outputLink>
<h:outputLink value="page2.jsf">Page 2</h:outputLink>
<h:outputLink value="page3.jsf">Page 3</h:outputLink>
Run Code Online (Sandbox Code Playgroud)

对于真实表单提交,您应该重写动作方法以返回voidnull代替结果.因此,代替

<h:form>
     <h:inputText value="#{bean.query}" />
     <h:commandButton value="Search" action="#{bean.search}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)

public String search() {
    results = searchService.find(query);
    return "results";
}
Run Code Online (Sandbox Code Playgroud)

在一页上

<h:dataTable value="#{bean.results}" var="result">
    ...
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)

在其他页面和此导航案例

<navigation-rule>
    <from-view-id>search.jsf</from-view-id>
    <navigation-case>
        <from-outcome>results</from-outcome>
        <to-view-id>results.jsf</to-view-id>
    </navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)

应该使用

<h:form rendered="#{empty bean.results}">
     <h:inputText value="#{bean.query}" />
     <h:commandButton value="Search" action="#{bean.search}" />
</h:form>
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
    ...
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)

public void search() {
    results = searchService.find(query);
}
Run Code Online (Sandbox Code Playgroud)

如有必要,您可以包含页面片段<jsp:include>.

也可以看看: