JSF2, reloading a page from a ViewScoped bean without destroying the bean? - jsf-2

I've got a little problem I'd like to solve.
I use Apache Myface 2 with Tomahawk and Trinidad.
Trinidad delivers its Dialog Framework to create Modal Dialogs...
My problem is that after the Dialog is closed, I want to refresh (re-render) the caller page.
As a solution I handle the ReturnListener, update the data of my bean, and send an ExternalContext.redirect to reload my page... but my ViewScoped bean is destroyed and a new one is create !
It is quite logical, but is there a way to reload a page from a Viewscoped bean without sending a redirect (that will destroy the bean...) ?
Is the only solution to use partial redering ?
To have it work I changed the scope to Session, but it's uggly in my case.
Thanks for your answers.

If you want to stick to view scope then yes only ppr can be of help, its not actually hard to set partialTrigger on the top level element which will refresh everything inside.
On the other had if you are ok to go with session or application scope i think you already are able to do that.

Related

How to call #PreDestroy on browser close event?

Here is my scenario. I have a component which searches some records and while searching, those records are marked locked. I am using JSF-2 and primefaces. My MBean is ViewScoped. Now i have a requirement, while closing the browser, the lock on those records need to be released. I was searching and my best bet appears to be #PostDestroy. So can someone help, whether using PostDestroy is correct approach. Some of the threads on StackOverFlow suggest, PostDestroy doesn't get called on browser close events. I I am not able to find much on PostDestroy. Can someone provide pointers on this approach?
Thanks,
Ben
Your #ViewScoped Bean does not know if the user is closing the browser. For the Bean it does not even matter if the client is a browser or some other HTTP-client.
The lifetime of a #ViewScoped Bean ends normally if your application sends a postback to the next view (see How and when is a view scope bean destroyed in JSF for an exact answer).
You can try to detect a closing browser with the JavaScript window.onunload event but I would not recommend that. Some browsers fire this event on browser close, others not, some fire it on reload etc. (see DOM Window unload event, is it reliable? for more details).
The #PreDestroy annotated Method (not #PostDestroy btw.) may also not be called immediately in some cases. You can only rely that it will be called on a session timeout. Which may be a long time for locking records...
You could release the locked records after a shorter period on your own using some kind of scheduler but I'd recommend to switch to optimistic locking (Don't lock and check if data has changed before you write).

IceFaces Window scope not working properly on some browsers

I am running into weird problems when using window scope in IceFaces 3.3. When I put debug statements on constructor and destroy method (using pre-destroy annotation), I am seeing that the same bean is being created and destroyed and re-created when a single user navigates between pages. I checked the access logs on the tomcat server and it shows that the user is using a compatible browser like IE8 and is not opening any new tabs other than navigating between the pages.
What is the lifecycle of a window scoped bean. What triggers a window scope bean to be destroyed?
This behavior does not happen consistently on the same browsers. It happens for some users using IE8 while not others. But most of the cases where the bean is re-created seem to be the cases where the user is behind some kind of proxy. Would the proxy be causing such a behavior? but then again the users session remains valid i.e. the session which keep tracks the name of the user etc., stays the same between pages.
How can this behavior be solved. Is window scope unreliable?
Yes, it's unreliable and perhaps doesn't serve the purpose of window scoping. You need to implement your own custom scope and not the icefaces predefined #{window}. The class org.icefaces.application.WindowScopeManager should give you some hints as to why you are seeing the behavior.

JSF2 - reset ViewScopedBeans when Navigating to other page/tab

I'm working with a Liferay portlet with JSF2 and Primefaces.
A lot of my Backing Beans are SessionScoped, as they are keeping info needed from multiple pages of the same portlet, and I want them to keep this info for the whole session, so I can't turn them to ViewScoped.
This way, the user has to log-out and log-in to reset the Backing Beans, and this is the supposed behavior I wanted. But I'd like to reset them when the user navigates to another portal page. So, my question is, what is the most convenient way to do this ?
Is there some Event I could catch when changing the page, and call some custom reset function on the Portlet?
Maybe some default setting on the portlet that will automatically reset the beans (no custom reset functions) ?
Any other Ideas ?
thanx!

Conversation Scope in Resource Handler in JSF?

I have a Resource Handler that needs to access lower level beans that work in conversation scope. Our application allows multiple login sessions on different browser tabs working within the same browser session by keeping the login details at this scope - so a database access (my Resource Handler) needs the login for the Conversation that referenced it.
I've tried just expecting it to work, even using ExternalContext.encodeRedirectUrl() to try to make Faces include whatever it needs.
I've tried explicitly putting the ?cid parameter onto my request path so I am accessing
/myapp/javax.faces.resource/thumbnail_3963075518712822225212162218.png.xhtml?ln=com.myapp.attachment&cid=1
No Conversation appears in the Resource Handler.
Further if I try to test for this using Conversation.isTransient() I get a
javax.enterprise.context.ContextNotActiveException: Conversation Context not active when method called on conversation Transient conversation
(Yet I thought Conversation is always meant to be active, and the stack trace for the above does include the org.jboss.weld.servlet.ConversationPropagationFilter!
I'm hoping this is not to do with the bug https://issues.jboss.org/browse/JBSEAM-3689 - for which the workaround is "Do not use conversations!"
Is there better?
Thanks
- Richard
Take a look at Seam Faces and see if you can get things to work the way you want. JSF has next to no compatibility with CDI in the JSF 2.0 and 2.1 specs. We're working hard with the lead for JSF to make it better for JSF 2.2, sorry for any inconveniences.
Mad idea - write an XHTML page with a Restore View event handler that manually renders the resource and calls Complete() on the FacesContext. Ugh! But XHTML pages do get conversations.
I have done what is perhaps suggested in the bug report. I've found a way of solving the problem not using Conversation Scope. In my case I have a ThreadLocal that, if present, provides alternate information to the component that access Conversation Scoped data.
Take a look at Apache Myfaces CODI,
Conversation management is superb with their extensions

JSF - Session Management

When I open two browsers and enter details parallely, the values are getting mixed. The values in one browser are populated in the other browser... We are using JSF framework and the managed beans are in session scope (having them in request scope is not possible in our case)... In a nutshell, the values are shared across the browsers... How to avoid this? Any clue?
If you "open two browsers" means you open two windows of same browser then everything is correct - you can get just one session per browser (for firefox there is plug-in which allow to avoid this limitation).
But if you use really two different browsers than in this case seams you save all data in application scope or you have a problem with you container.
You need to put the bean in request or view scope instead. Storing request/view based data in a session scoped bean is a bad idea, as you've encountered yourself.
I think the view scope would help a lot, given the fact that you mentioned that the request scope is "not possible" (it actually is, it only requires preloading the right data so that JSF can take the right actions accordingly).

Resources