开发者

How can I redirect in JSF 2.0

开发者 https://www.devze.com 2023-01-20 09:21 出处:网络
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 re

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:c开发者_运维技巧ommandLink> for this?


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:

  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • When should I use h:outputLink instead of h:commandLink?
  • JSF implicit vs. explicit navigation
0

精彩评论

暂无评论...
验证码 换一张
取 消