Sreeram
Sreeram

Reputation: 3258

How to implement JSF navigation between two pages without using navigation-case in faces-config.xml

I am looking for a another way of JSF navigation other than mentioning navigation-cases in faces-config.xml.

At present i am using faces-config.xml to navigate. I want to clean it up.

Please suggest all other ways so that i can use whatever suits my need.

Upvotes: 2

Views: 20311

Answers (3)

BalusC
BalusC

Reputation: 1108782

For simple page-to-page navigation (without submitting anything) you should be using <h:outputLink> instead of <h:commandLink>.

So, instead of

<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>

and those navigation cases

<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>

you should use

<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>

For real form submits you should rewrite the action methods to return void or null instead of an outcome. So, instead of

<h:form>
     <h:inputText value="#{bean.query}" />
     <h:commandButton value="Search" action="#{bean.search}" />
</h:form>

with

public String search() {
    results = searchService.find(query);
    return "results";
}

on one page and

<h:dataTable value="#{bean.results}" var="result">
    ...
</h:dataTable>

on other page and this navigation case

<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>

you should use

<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>

with

public void search() {
    results = searchService.find(query);
}

You can if necessary include page fragments by <jsp:include>.

See also:

Upvotes: 5

Siva Charan
Siva Charan

Reputation: 18064

//JSF

<h:outputLink value="login.xhtml" >
    Login page
</h:outputLink>

//HTML output

<a href="login.xhtml">
    Login page
</a>

Refer this URL for more info:-

commandLink and outputLink example

Upvotes: 2

Sebastian Wramba
Sebastian Wramba

Reputation: 10127

You can set the return value of a navigation action to the name of the page you want to go to (e.g. return "page2"; to switch to page2.jsf). But as far as I know, this feature has been implemented first in JSF 2.0.

Upvotes: 0

Related Questions