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

2

5

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.

Levey answered 28/3, 2011 at 9:33 Comment(0)
C
5

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;
}
Cannular answered 16/9, 2011 at 21:47 Comment(0)
P
4

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"

Peper answered 29/3, 2011 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.