防止对应用程序的某些部分进行组件树序列化 [英] Prevent component tree serialization for certain parts of application

查看:90
本文介绍了防止对应用程序的某些部分进行组件树序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以明确拒绝JSF序列化某些组件树?目前,我正在将不可序列化的对象传递给<h:inputText>:

Is it possible to explicitly deny JSF from serializing some component trees? At the moment I am passing a non-serializable object to a <h:inputText>:

<h:inputText value="#{nonSerializableBean.nonSerializableClassInstance}" />

单击几下后会发生(恢复视图期间):

What happens after a few clicks is that I get (during view restoration):

javax.faces.FacesException: Unexpected error restoring state for component
with id configurationForm:j_idt292:j_idt302:field.  Cause:
java.lang.IllegalStateException: java.lang.InstantiationException:
my.namespace.NonSerializableClass

我认为是因为JSF无法还原nonSerializableClassInstance:

I think this occurs because JSF cannot restore the nonSerializableClassInstance:

Caused by: java.lang.IllegalStateException: java.lang.InstantiationException: com.foobar.utils.text.Period
at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:110)
at javax.faces.component.ComponentStateHelper.restoreState(ComponentStateHelper.java:292)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1444)
at javax.faces.component.UIOutput.restoreState(UIOutput.java:255)
at javax.faces.component.UIInput.restoreState(UIInput.java:1359)

一个额外的问题:不可以将支持bean序列化吗?然后应该防止这些序列化/反序列化吗?

A bonus question: Is it ok not to make backing beans Serializable? Should this then prevent serialization/deserialization of these?

某些背景:

在JSF中,我们需要提供一堆第三方类.问题是我们不能在JSF页面上直接使用这些类,因为它们没有实现Serializable接口,因此,如果JSF运行时决定对页面和组件树进行序列化/反序列化,则它们将/应该失败.这些类是封闭的",我们不允许对其进行修改.

We have a bunch of 3rd party classes that we need to provide forms for in JSF. The problem is that we cannot directly use these classes on JSF pages, because they do not implement Serializable interface, and thus will/should fail if JSF runtime decides to serialize/deserialize the page and the component-tree. The classes are "closed" and we are not allowed to modify them.

运行Mojarra 2.0.2.

Running Mojarra 2.0.2.

推荐答案

Javabeans通过规范应该实现Serializable. JSF只是遵循/遵守此规范.

Javabeans are by spec supposed to implement Serializable. JSF just follows/adheres this spec.

这些类是封闭的",我们不允许对其进行修改.

最好的选择是将其包装为实现Serializable并相应地实现writeObject()readObject()的类的transient属性.

Your best bet is to wrap it as a transient property of a class which implements Serializable and implement the writeObject() and readObject() accordingly.

public class SerializableClass implements Serializable {

    private transient NonSerializableClass nonSerializableClass;

    public SerializableClass(NonSerializableClass nonSerializableClass) {
        this.nonSerializableClass = nonSerializableClass;
    }

    public NonSerializableClass getNonSerializableClass() {
        return nonSerializableClass;
    }

    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
        oos.writeObject(nonSerializableClass.getProperty1());
        oos.writeObject(nonSerializableClass.getProperty2());
        // ...
    }

    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
        nonSerializableClass = new NonSerializableClass();
        nonSerializableClass.setProperty1((String) ois.readObject());
        nonSerializableClass.setProperty2((String) ois.readObject());
        // ...
    }

}

最后改用该类.您最终可以将其设置为extends NonSerializableClass,然后通过稍微不错的IDE自动生成委托方法.

Finally use that class instead. You could eventually let it extends NonSerializableClass and then autogenerate delegate methods by a bit decent IDE.

无论哪种方式,它只会有很多不透明的样板代码,但是由于您不允许修改这些类...(我个人只是将第3方的内容推送给它们,即所谓的实施Serializable的Javabean是因为它们违反了标准/规范.

Either way, it's only going to be a lot of opaque and boilerplate code, but since you're not allowed to modify those classes... (I would personally just push that 3rd party stuff to have them their so-called Javabeans to implement Serializable since it are them who's breaking the standards/specs).

这篇关于防止对应用程序的某些部分进行组件树序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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