Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF2 (Mojarra) View Scope Managed Bean wants all members to be Serializable

I am trying to convert a session scoped JSF managed bean to view scoped. However, when I try to access the xhtml page for this bean, then i get the following error:

java.io.NotSerializableException: foo.bar.SomeDaoClass

I have a member of a helper DAO that I use to delegate persistence related tasks within the bean. If I make this DAO class implement Serializable then other UIComponent references start causing the same errors!

The main use case is that I have a link on the click of which I open a jquery lightbox pop-up showing the xhtml page which is backed by a session bean. When the user clicks the submit button on the pop-up form, I remove the session bean programatically. The problem is if the user clicks the close button of the pop-up itself, and clicks on another link pointing to another id, then the same values are shown (being session scoped)!

I would like to use the view scope to preserve values while viewing this form in a pop-up and when the user clicks the close button of the pop-up, the values may be discarded.

like image 785
DeeTewari Avatar asked Mar 28 '11 09:03

DeeTewari


2 Answers

I hope you've already resolved this problem, but for other people landing here, who don't want to use session scope and uses view scope as alternative, which force you to use serializable implementation, you can use the transient keyword next to the properties that you don't want to make serializable, that would be very helpful if you want to call a service or dao.

example:

@ManagedBean(name="addressTableBeanExample4")
@ViewScoped
public class ExampleBean4 implements Serializable {

    private static final long serialVersionUID = 1L;

    // non serialazable class
    private transient List<Customer> data = new ArrayList<Customer>();

    private Customer selected;
}
like image 139
Lithium Avatar answered Nov 15 '22 09:11

Lithium


Referring to Balusc blog http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html

"In a nutshell: the @ViewScoped breaks when any UIComponent is bound to the bean using binding attribute"

like image 45
fyousof Avatar answered Nov 15 '22 09:11

fyousof