ViewScoped Bean原因NotSerializableException [英] ViewScoped Bean cause NotSerializableException

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

问题描述

你好,我正在使用ViewScoped Bean,问题是调用它时会收到NotSerializableException.

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

这是我的Managed 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.

这种方法的优点是历史就是真实的历史.返回到先前的视图版本(使用后退按钮)时,实际上您会在那时获得确切的Backing 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天全站免登陆