Prevent home (welcome file) URL from changing to full URL when submitting a JSF form - jsf

I've a welcome file
<welcome-file-list>
<welcome-file>configurationClass.xhtml</welcome-file>
</welcome-file-list>
It is opening properly on start with url "http://127.0.0.1:8080/fav/".
Then in my page, i am having a sortable table which is calling to a method sortFavs() present in my class. And i am returning null from server method.
So url is changing to "http://127.0.0.1:8080/fav/configurationClass.xhtml"
Can i get the same output with previous url? How to do this without using any other lib like omnifaces or others ?

Add <f:ajax execute="#form" render="#form" /> to the command button/link. This way the POST request will be performed asynchronously instead of synchronously and the browser's address bar URL will stay the same all time.

Related

Is loading triggered from the click of a submit button generated by h:commandButton a full load?

A jsf page contains a form with a commandButton element like <h:commandButton action="#{bean.search}". This submit button will trigger a bean, which then will send more data back to this same jsf page.
Question: after the submit, is the jsf page fully loaded again? or is just the part containing the newly retrieved data reload and other parts are not loaded again?
By default JSF will fully reload the page. This can be tested by looking at the browser network developer tools while submitting the commandButton. If you intend to only render a part of the page - e.g. a label that outputs the result - you can do that by using AJAX.
For example if your commandButton is in a form, by adding
<f:ajax execute="#form" render="#form:result" />
within your commandButton tag, you can specify that only the contents of the form are processed by the server and only the label with the ID 'result' is rendered.
For further information take a look at this guide for AJAX in JSF: https://www.beyondjava.net/a-comprehensive-guide-to-jsf-ajax
You will see that the author shows the full page loading without AJAX.
Alternatively here is the official documentation for <f:ajax>:
https://jakarta.ee/specifications/faces/2.3/vdldoc/f/ajax.html

forwarding request to a page with p:outputpanel make it load forever

i m developing a web-app using primefaces 6.0 on wildfly 10, my problem is:
redirecting to a page that contain a primefaces outputpanel cause the outputpanel to load forever :/
by redirecting i mean code like:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/aboutUs.xhtml");
dispatcher.forward(request, response);
i will appreciate any help :D
after some testing, i got it:
-lets say i've sent a requset to my servlet, the requset got forwared and a nice web page it returned
-now the javascript code in my outputpanel widget try to load its contents, so it send another request
-this time the servlet my just decide to not forward to the same page, or it my not forward at all or ...
the problem is i did not write this servlet (its faces servlet in my case, and since even faces-config.xml won't do the trick it's hopless)
what i've done is using a parameter to enable/disable outputpanel lazy loading like this:
<p:outputPanel deferred="#{request.getParameter('lazy') == 'off'? false : true}">
then i used this in my auto forward pages:
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.xhtml?lazy=off</form-login-page>
<form-error-page>/login.xhtml?lazy=off</form-error-page>
</form-login-config>
</login-config>
the rest is ensuring than any ajax call these pages do should also include this parameter so i've added this parameter in my "change language" menuitem
<p:menuitem ...>
<f:param name="lazy" value="#{request.getParameter('lazy')}" />
</p:menuitem>
now these pages load without lazy loading insdead of loading forever, not a perfect solution but better than nothing :D
this is the best idea that i got until now :/

JSF view parameters and h:form action

this question is "extending" the following post
Handling view parameters in JSF after post
Asume that I'm creating an custom URL that I'm opening in a dialog.
URL example "test.html?id=123"
I want to insert the "id=123" part into the h:form action so that it returns back to the same view with the extra parameter "id=123" part.
I have followed the above post and worked on the third solution, the custom ViewHandler that extends ViewHandlerWrapper.
I'm modifying the action and the form gets redirected to the same origin (until now it works).
First issue
Lets say I have the following xhtml page.
<f:metadata>
<f:event type="preRenderView" listener="#{bean.init}" />
<f:viewParam name="id" value="#{bean.id}"/>
</f:metadata>
<h:form>
...
...
</h:form>
When i post the above i get the correct id value on the back end.
If i resubmit the post i get null values, the url however stays the same (test.html?id=xxx)
Cant figure out why i'm getting null values on the second submit
Second issue is that when I have validated the form and everything is fine, my submit method returns back a new url "test.html?force-redirect=true..." but this does not happen, instead the URL is the same as the one entered in the h:form action, (if i make a custom redirect with the new URL then it works and i get redirected to "test.html?faces-redirect=true...").
My question, I guess is a theoretical one, is this the supposed behavior?
Thanks,
Dimman

How to retain form data in a multi form page

I am working on JSF. I have an xhtml page with multiple forms. when i am submitting one form, and if i had made some changes on other form i am loosing it, as on submit the page is getting refreshed. I cannot use a single form. is there any way to do.
any solution will be highly appreciated.
thanks !
If you're already using JSF 2.x (your statement that you're using XHTML (Facelets) confirms this less or more), just submit the form by ajax.
It's a matter of adding the following tag to the command links/buttons of the form:
<f:ajax execute="#form" render="#form" />
This way the current <h:form> will be submitted by ajax and the current <h:form> only will be rendered (updated/refreshed). You can if necessary specify other to-be-updated components in the render attribute by adding other client ID(s).
If you're still on the old JSF 1.x, you may want to look at Ajax4jsf sublibrary of RichFaces which supports basically the same.

Issue with primefaces , Specially p:commandButton

I developed a JSF 2 application using RichFaces first and then migrated to Primefaces recently.
All things are working fine except:
When clicking on a button and calling update="id1, id2, etc", it does not update my page contents.
<p:commandButton value="Logout" action="#{profile.logout}" /> does not work, in logout method I simply clear session and return to login page, but it stays on the same page.
I am using Tomcat 6.0.32 on Windows 7 with 4GB RAM. I m using PrimeFaces 2.2, the latest Stable build.
When clicking on a button and calling update="id1, id2, etc", it does not update my page contents.
You have to provide more context before we can give an answer on that. My first guesses would be that you have to remove those commas (and only separate by spaces like in standard JSF2 and RF4). My second guess would be that those components are simply not resolveable in the current component and NamingContainer hierarchy. My third guess would be that the ajax request didn't return a valid ajax response (use Firebug to check it).
<p:commandButton value="Logout" action="#{profile.logout}" /> does not work, in logout method I simply clear session and return to login page, but it stays on the same page.
The <p:commandButton> sends by default an ajax request. If you want to navigate to a different view as response on an ajax request, then you have to send a redirect. You can do this among others by adding the faces-redirect=true parameter to the outcome:
return "login?faces-redirect=true";
Or, alternatively, just turn off ajax on the button:
<p:commandButton ... ajax="false" />
Or, use the standard <h:commandButton> instead:
<h:commandButton ... />

Resources