JSF中的ViewScop Bean中的NotSerializableException

时间:2013-12-11 11:49:47

标签: jsf-2

下面编写的代码是使用JSF2.0 / Primefaces从数据库下载文件。下面的代码是SessionScoped Bean运行完全正常,我可以下载存储的文件

@ManagedBean
@SessionScoped
public class FileProcessBB{

private ScreenDisplayData screenDisplayData;

    public void selectAttachment() {
        AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
        InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
        String contentType = attachedFileTable.getAttachedFileContentType();
        String fileName = attachedFileTable.getAttachedFileName();
        screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
    }
}

但我希望在ViewScoped Bean中实现相同的功能。为了实现这一点,我做了如下所示的必要更改(为ViewScoped Bean实现Serializable)

@ManagedBean
@ViewScoped
public class FileProcessBB implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 6137719800118206851L;

private ScreenDisplayData screenDisplayData;

    public void selectAttachment() {
        AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
        InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
        String contentType = attachedFileTable.getAttachedFileContentType();
        String fileName = attachedFileTable.getAttachedFileName();
        screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
    }
}

ScreenDisplayData

public class ScreenDisplayData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3339259618782904262L;

    private StreamedContent attachedFileStreamContent;
    private AttachedFileBO selectedAttachedFileBO;


    //getter setter

}

我也在AttachedFileBO类中实现了Serializable。 AttachedFileTable是一个Entity类,因此它已经实现了Serializable

我在哪里缺少Serializable?

我得到的例外情况如下

[ERROR   ] SRVE0777E: Exception thrown by application class 'java.io.ObjectOutputStream.writeObject0():1200'
java.io.NotSerializableException: java.io.ByteArrayInputStream
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1200)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at java.util.HashMap.writeObject(HashMap.java:867)
  at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
  at java.lang.reflect.Method.invoke(Method.java:613)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at java.util.HashMap.writeObject(HashMap.java:867)
  at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
  at java.lang.reflect.Method.invoke(Method.java:613)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at org.apache.myfaces.shared_impl.util.StateUtils.getAsByteArray(StateUtils.java:273)
  at org.apache.myfaces.shared_impl.util.StateUtils.construct(StateUtils.java:235)
  at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.getViewState(HtmlResponseStateManager.java:314)
  at org.apache.myfaces.application.jsp.JspStateManagerImpl.getViewState(JspStateManagerImpl.java:626)
  at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:443)
  at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:344)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:88)
  at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:358)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:609)
  at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1159)
  at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
  at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
  at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
  at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1221)
  at [internal classes]

1 个答案:

答案 0 :(得分:1)

在模型中,您根本不应该使用InputStream风格的属性。您应该使用byte[]代替。

替换不可序列化的基于InputStream的属性,例如

private ByteArrayInputStream content;

通过基于序列化byte[]的属性,如

private byte[] content;

在一个和另一个表单之间进行转换应该完全在方法范围内完成。