2

I am trying to make my app "Bookmarkable", and i am using view parameters to achieve it.

And i think i still do not get the right way to do it right in JSF, even after reading this, and many others.

My problem is that the get parameters get lost after any non-ajax postback, i mean, the parameter value is still set in the bean and the app works correctly, but it gets removed from the URL making the URL invalid.

For instance, having an URL like http://company.com/users?id=4, as soon as that page executes a non-ajax postback (for uploading data, for instance) the URL becomes just http://company.com/users. The app continues to work correctly, but the link is not any more "Bookmarkable".

Is there any way to prevent the non-ajax postbacks removing the viewParams from the URL?

My use case is to be able to bookmark a page to EDIT an object, and there i need to be able to upload data (if not i would not use non-ajax postbacks). I know i would not need any postback if i would want to bookmark the page to only VIEW the data of the object, but that is not my case.

I could also do a redirect to the same page with the same params, and let the app to recreate the view scoped bean, but then i really do not see any benefit over request scoped beans...

Any suggestion is very appreciated.

Community
  • 1
  • 1
mmoossen
  • 1,237
  • 3
  • 21
  • 32
  • 2
    possible duplicate of [Handling view parameters in JSF after post](http://stackoverflow.com/questions/10352641/handling-view-parameters-in-jsf-after-post) – BalusC Jan 07 '13 at 13:51
  • @BalusC: thanks, once again you brought light to my ignorance ;), as i see it, since i need to upload data, the only way to go is to implement my own ViewHandler... but good that i am already using OmniFaces, i just did not came across that feature yet! and please write an answer. – mmoossen Jan 07 '13 at 15:46
  • and it just works! and it did also fix http://stackoverflow.com/questions/14194462/validation-of-viewparam-and-a4jcommandbutton – mmoossen Jan 07 '13 at 15:53
  • You're welcome. I reposted it as an answer. – BalusC Jan 07 '13 at 15:55

1 Answers1

2

This behaviour is "by design". The <h:form> generates a HTML <form> element with an action URL without any view parameters. The synchronous POST request just submits to exactly that URL which thus get reflected as-is in browser's address bar. If you intend to keep the view parameters in the URL, while using ajax is not an option, then you basically need to create a custom ViewHandler which has the getActionURL() overridden to include the view parameters. This method is used by <h:form> to generate the action URL.

public String getActionURL(FacesContext context, String viewId) {
    String originalActionURL = super.getActionURL(context, viewId);
    String newActionURL = includeViewParamsIfNecessary(context, originalActionURL);
    return newActionURL;
}

Or, as you're based on the comments already using OmniFaces, you could also use its <o:form> component which basically extends the <h:form> with the includeViewParams attribute which works much like the same as in <h:link> and <h:button>.

<o:form includeViewParams="true">
    ...
</o:form>

This way all <f:viewParam> values will end up in the form action URL.

See also:

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