1

How is navigation from a Facelet page

<p:commandLink action="xyz.xhtml">

or a backing bean

<p:commandLink action="#{bean.redirect}">
public class Bean{
   public String redirect(){
       .....
       return "xyz.xhtml";
   }
 }

different from each other?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Adarsh
  • 3,613
  • 2
  • 21
  • 37
  • 1
    You are in fact redirecting in *none* of your examples! What you're doing is forwarding. – skuntsel Apr 10 '13 at 08:32
  • Sorry. What I meant was navigation. I have edited the question. – Adarsh Apr 10 '13 at 08:57
  • Command components (links, buttons) are designed to invoke form submits, so as to perform some business action on the server and return a navigation case outcome based on submitted data. Using command components to act as plain navigation links is considered to be a very poor practice. So your `redirect` is in fact `performAction` that *returns* a navigation case outcome. – skuntsel Apr 10 '13 at 09:33
  • 1
    is right and but your method return directly .xhtml file name its not a good way to development .. for navigation make one faces-navigation.xml file and define navigation case in this file and return case from your bean method – hayat Apr 10 '13 at 10:58

2 Answers2

1

Check out the documentation of p:commandLink here, which says the following for action attribute:

A method expression or a string outcome to process when command is executed.

Now, as action="xyz.xhtml" returns String xyz.xhtml you're redirected accordingly and for action="#{bean.redirect}" which again returns xyz.xhtml you are again redirected according to the returned String.

dShringi
  • 1,497
  • 2
  • 22
  • 36
1

How is navigation from a xhtml page or a backing bean different from each other.

There's no difference. The both examples invoke a POST request and instructs JSF to render the view associated with the given outcome. The backing bean method has the only advantage that it allows you to perform some business logic beforehand or even control the outcome value programmatically.

However, if you don't have any business logic at all and solely want to have an idempotent link to another page, then using a command link is actually a bad practice. Using POST for page-to-page navigation is not user nor SEO friendly. The target page is not bookmarkable (the URL remains the one of the page where the POST form was been submitted to) nor searchbot-crawlable (it is using JavaScript to submit a hidden form).

You should instead use a normal link.

<h:link outcome="xyz.xhtml">

This generates a SEO-friendly <a> element with the full URL in its href and ends up in an user-friendly bookmarkable URL.

See also:

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