JSF commandLink, POSTing and return button

I recently started doing some JSF work - before that I had always used PHP or Python for web development.

I was somewhat surprised to find that JSF uses HTTP POST to navigate when using the h: commandLink tag.

I am using commandLink as this is apparently the correct way to create JSF applications. Why does JSF use POST for navigation? What happened to GET? I can only assume that Javascript, which is automatically generated by JSF for onclick events, may exceed the maximum length for a GET request.

I already have some pages that are navigated using h: commandLink. This works wells until I use the back button. How do I handle a back button in JSF?

I am trying to understand why JSF was built around POST. This breaks bookmarks, reverse swapping and the ability to index your page in search engines.

+5
source share
3 answers

This will not help you create a link on the client, but be aware of outputLink .

There is a navigation rule redirection element that can help with some upgrade issues.

<navigation-rule>
  <display-name>navBack</display-name>
  <from-view-id>/navBack.jsp</from-view-id>
  <navigation-case>
    <from-outcome>navTo</from-outcome>
    <to-view-id>/navTo.jsp</to-view-id>
    <redirect />
  </navigation-case>
</navigation-rule>

If all else fails, you can make the redirect URL yourself, as in this step:

public String doAction() {
  System.out.println("Did some non-idempotent operation");
  FacesContext context = FacesContext.getCurrentInstance();
  ExternalContext extContext = context.getExternalContext();
  Application app = context.getApplication();
  ViewHandler viewHandler = app.getViewHandler();
  String url = viewHandler.getActionURL(context, "/navTo.jsp");
  url = url + (url.indexOf('?') < 0 ? '?' : '+') + "foo=bar";
  url = extContext.encodeResourceURL(url);
  try {
    extContext.redirect(url);
  } catch (IOException e) {
    throw new FacesException(e);
  }
  return null;
}

Cautions: I can’t remember if the URL is encoded correctly.

Different third-party libraries add different elements. I'm not sure if something was done in this area in JSF 2.0, but it might be worth a look.

+4

. JSF POST, JSF. JAX-RS/Seam Spring GETable.

+1

As BalusC said on his blog , GET should be used to navigate the page. Use h:outputLinkfor navigation.

You can read its PostRedirectGetListener, and it will decide otherwise / update the confirmation confirmation form.

I advise you to add the following to beforePhaseto handle partial ajax processing (if necessary):

if(event.getFacesContext().getPartialViewContext().isAjaxRequest())
{
    return;
}
+1
source

All Articles