How can I redirect in JSF 2.0
Asked Answered
A

1

7

I want to redirect from a link in a JSF page, how can I do it?

In HTML I can use <a> tag for this. But in JSF I use <h:outputLink> or <h:commandLink> as they can be conditionally rendered. I want redirect link to other page in same application or to an external URL. How can I do it with JSF? How can I use action in <h:commandLink> for this?

Akel answered 16/10, 2010 at 6:49 Comment(0)
B
11

Assuming that you'd like to redirect to some.xhtml which is placed in web root folder:

  1. You can just continue using plain HTML.

     <a href="#{request.contextPath}/some.xhtml">go to some page</a>
    

    For conditional rendering, just wrap it in an <ui:fragment>.


  2. Or use <h:link> with implicit navigation.

     <h:link outcome="/some" value="go to some page" />
    

    Note: no need to prepend context path nor to include FacesServlet mapping.


  3. Or use <h:commandLink> with ?faces-redirect=true.

     <h:commandLink action="/some?faces-redirect=true" value="go to some page" />
    

    Note: no need to prepend context path nor to include FacesServlet mapping.


  4. Or use <h:outputLink>, but you need to specify context path.

     <h:outputLink value="#{request.contextPath}/some.xhtml" value="go to some page" />
    

Redirecting to an external URL is already answered in this duplicate: Redirect to external URL in JSF.

See also:

Besiege answered 16/10, 2010 at 12:57 Comment(4)
Hi Balus thank you, but i have problem with include page, i include menu page into main page and when i click on Register link, it not redirect into register page, can you help me? Thank You ! Best RegardsAkel
The h:link outcome must point to the filename of the page without extension and it must be in the same folder as the main page.Besiege
i use h:commandLink, but if menu page in one folder and register page in other folder and i include menu page into main page (it outside all of folder) it can be redirect? in my backing bean if return String outcome i must return such as return "../register" ? Thank for your response !Akel
UICommand components should go in a h:form. The outcome must just match the page filename. If it's an URL, then you need to define a navigation case in faces-config.xml. You can on the other hand also use h:outputLink. This behaves exactly the same as <a>.Besiege

© 2022 - 2024 — McMap. All rights reserved.