使用@ViewScoped时的java.io.NotSerializableException [英] java.io.NotSerializableException when @ViewScoped is used
问题描述
如果我在JSF中使用@ViewScoped
,则会发生以下异常:
If I use @ViewScoped
in JSF, then the following exception occurs:
java.io.NotSerializableException: com.solv.basics.Basics
java.io.ObjectOutputStream.writeObject0(Unknown Source)
java.io.ObjectOutputStream.writeObject(Unknown Source)
java.util.HashMap.writeObject(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
java.io.ObjectOutputStream.writeSerialData(Unknown Source)
java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
我可以通过让bean实现Serializable
来解决它.但是,我不理解其原因.为什么只在视图作用域而不是其他作用域中发生此异常?
I can solve it by letting the bean implement Serializable
. However, I do not understand the reasoning. Why does this exception only occur for the view scope and not the other scopes?
推荐答案
您没有提供来自web.xml的设置,但是如果javax.faces.STATE_SAVING_METHOD
设置为client
,则视图始终是序列化的,因此NotSerializableException
将始终出现.
You didn't provide the settings from web.xml, but if the javax.faces.STATE_SAVING_METHOD
is set to client
, the view is always serialized, so the NotSerializableException
will always occur.
您应该始终使您的JSF Bean可序列化,因为应用服务器可能希望对会话进行序列化,因此,即使处于状态,所有会话范围和视图范围的bean保存设置为server
.
You should always make your JSF beans serializable, because application server may want to serialize the session, so all session-scoped and view-scoped beans, even if state saving is set to server
.
但是,如果服务器不对您的会话进行序列化,则不会在会话范围的bean上收到该错误.但是,如果视图在客户端上进行了序列化,则意味着所有视图范围的Bean都被序列化为与所有请求一起作为隐藏字段发送的字符串,并且JSF引擎正在检测到您的Bean无法序列化.
But if the server isn't serializing your session, you won't get that error on session scoped beans. But if view is serialized on client, it means that all view scoped beans are serialized to string that is sent as hidden field with all requests, and the JSF engine is detecting that your beans are not serializable.
NotSerializableException
仅在服务器尝试实际序列化bean时发生!
NotSerializableException
occurs only when the server tries to actually serialize your beans!
这篇关于使用@ViewScoped时的java.io.NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!