Java JSF 支持 bean 应该是可序列化的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3372687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:41:16  来源:igfitidea点击:

JSF backing bean should be serializable?

javaserializationjsf

提问by egbokul

I'm getting these messages:

我收到这些消息:

[#|2010-07-30T11:28:32.723+0000|WARNING|glassfish3.0.1|javax.faces|_ThreadID=37;_ThreadName=Thread-1;|Setting non-serializable attribute value into ViewMap: (key: MyBackingBean, value class: foo.bar.org.jsf.MyBackingBean)|#]

[#|2010-07-30T11:28:32.723+0000|警告|glassfish3.0.1|javax.faces|_ThreadID=37;_ThreadName=Thread-1;|将不可序列化的属性值设置到ViewMap中:(键:MyBackingBean,值类:foo.bar.org.jsf.MyBackingBean)|#]

Do these mean that my JSF backing beans should implement Serializable? Or are they refering to some other problem?

这些是否意味着我的 JSF 支持 bean 应该实现 Serializable?还是他们指的是其他问题?

采纳答案by BalusC

Yes, you understood it correctly. The view is basically stored in the session scope. The session scope is in JSF backed by the Servlet's HttpSession. All session attributes are supposed to implement Serializable, this because the average servletcontainer may persist session data to harddisk among others to be able to share with other servers in a cluster, or to survive heavy load, or to revive sessions during server restart.

是的,你理解正确。视图基本上存储在会话范围内。会话范围在由 Servlet 支持的 JSF 中HttpSession。所有会话属性都应该实现Serializable,这是因为一般的 servletcontainer 可以将会话数据持久化到硬盘等,以便能够与集群中的其他服务器共享,或者在重负载下生存,或者在服务器重启期间恢复会话。

Storing raw Java objects on harddisk is only possible if the respective class implements Serializable. Then ObjectOutputStreamcan be used to write them to harddisk and ObjectInputStreamto read them from harddisk. The servletcontainer manages this all transparently, you actually don't need to worry about it. JSF is just giving a warning so that you understand the risks.

只有在相应的类实现了Serializable. 然后ObjectOutputStream可用于将它们写入硬盘和ObjectInputStream从硬盘读取它们。servletcontainer 透明地管理这一切,您实际上不必担心它。JSF 只是发出警告,以便您了解风险。

回答by user2823705

Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable. Source: https://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html

使用会话、应用程序或对话范围的 Bean 必须是可序列化的,但使用请求范围的 Bean 不必是可序列化的。来源:https: //docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html

回答by Ambrish

Yes, Backing Beans / Managed Beans are basically meant to persist the state of the view, so it should be implemented serialization, but UI Components may not allow it to serializable and JSF Runitime will show you errror/ warning message. one thing you can do is mark such component transient in your MBeans.

是的,Backing Beans / Managed Beans 基本上是为了保持视图的状态,所以它应该实现序列化,但 UI 组件可能不允许它序列化并且 JSF Runitime 会向您显示错误/警告消息。您可以做的一件事是在 MBean 中标记此类组件瞬态。

Thanks.

谢谢。