ViewScoped Bean 导致 NotSerializableException [英] ViewScoped Bean cause NotSerializableException

查看:16
本文介绍了ViewScoped Bean 导致 NotSerializableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用的是 ViewScoped Bean,问题是调用它时我收到 NotSerializableException.

Hello I'm using a ViewScoped Bean the Problem is that when call it I get the NotSerializableException.

这是我的托管 Bean 的代码:

This is the code of my Managed Bean :

@ManagedBean(name="demandesBean")
@ViewScoped
public class DemandesBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value="#{demandeService}")
    private DemandeService demandeService; //A Spring Service

    @ManagedProperty(value="#{loginBean}")
    private LoginBean loginBean;

    private DemandeVO newDemande;

    @PostConstruct
    public void initData() {
        newDemande = new DemandeVO();
    }

    public void doAjouterDemande(ActionListener event) {
        demandeService.createDemande(newDemande, loginBean.getUsername());
        newDemande = new DemandeVO();
    }

    public List<DemandeVO> getListDemande() {
        return demandeService.getAllDemandesByUser(loginBean.getUsername());
    }

    public DemandeService getDemandeService() {
        return demandeService;
    }

    public void setDemandeService(DemandeService demandeService) {
        this.demandeService = demandeService;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    public DemandeVO getNewDemande() {
        return newDemande;
    }

    public void setNewDemande(DemandeVO newDemande) {
        this.newDemande = newDemande;
    }
}

我收到以下异常:

GRAVE: Exiting serializeView - Could not serialize state: com.bull.congesJBPM.serviceImpl.DemandeServiceImpl
java.io.NotSerializableException: com.bull.congesJBPM.serviceImpl.DemandeServiceImpl

有什么办法可以解决这个问题吗??请帮忙 !

Any fix for this problem ?? Please Help !

推荐答案

另一个问题是 MyFaces 默认情况下执行状态的序列化,即使状态正在保存在服务器上(默认).这反过来要求视图范围的支持 bean 是可序列化的.

Another problem is that MyFaces by default does serialization of state, even when state is being saved on the server (the default). This on its turn requires view scoped backing beans to be serializable.

这种方法的优点是历史就是真正的历史.当您返回到之前的视图版本(使用后退按钮)时,您实际上会获得当时支持 bean 的确切版本.

The pros of this approach is that history is truly history. When you go back to a previous view version (using the back-button), you actually get the exact version of the backing bean at that time.

缺点是它似乎中断了服务注入(与这个问题无关,是一个主要的性能损失).注入 EJB 服务时会发生完全相同的问题.

The con is that it seems to break injection of services (and unrelated to this problem, is a major performance hit). The exact same problems happens when injecting an EJB service.

您可以在 web.xml 中放置一个上下文参数来禁用此行为:

There's a context parameter that you can put in web.xml to disable this behavior:

<context-param>
    <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
    <param-value>false</param-value>
</context-param>

参见http://wiki.apache.org/myfaces/Performance

顺便说一句,Mojarra 有一个类似的设置,但默认值为 false.

Incidentally, Mojarra has a similar setting but there the default is false.

这篇关于ViewScoped Bean 导致 NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆