2

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.

Sreeram
  • 3,160
  • 6
  • 33
  • 44

3 Answers3

5

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

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

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

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.

Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • Thanks for the response.Can you suggest any method for JSF 1.X because i am using JSF 1.2? – Sreeram Nov 18 '11 at 11:34
  • Hi any idea how to archieve this in JSF 1.2 ? – wutzebaer Feb 07 '12 at 12:23
  • No, as far as I know, this is a feature that came with 2.0 first. I think you will have to have a config file with `navigation-case`s, so that JSF knows which `commandAction` return value leads to which page. – Sebastian Wramba Feb 07 '12 at 16:27