1

为什么没有view范围faces-config.xml?如果我不使用 faces-config.xml,我将如何设置 bean 来查看范围?

我使用了以下 bean 文件:

@ManagedBean
@ViewScoped
public class StatusBean2 implements Serializable  {

    /**
     * 
     */
    private static final long serialVersionUID = -7528998562821856315L;
    private EntityManager em;

    public StatusBean2() {
        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("FreeBird");
        em = emf.createEntityManager();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext()
                .getSession(true);
        int toIndex = 5;
       session.setAttribute("toIndex", toIndex);

    }
    public List<Status> getStatusList() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext()
                .getSession(true);
        User user = (User) session.getAttribute("userdet");
        Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
                + user.getEmail() + "' ORDER BY s.timeMillis desc",
                Status.class);
        List<Status> results = query.getResultList();
        Query query1 = em.createQuery("SELECT f FROM Friend f WHERE f.email='"
                + user.getEmail() + "'", Friend.class);
        List<Friend> results1 = query1.getResultList();
        Iterator<Friend> it = results1.listIterator();
        while (it.hasNext()) {
            String email = it.next().getFriendEmail();
            Query query2 = em.createQuery(
                    "SELECT s FROM Status s WHERE s.email='" + email
                            + "' ORDER BY s.timeMillis desc", Status.class);
            List<Status> results2 = query2.getResultList();
            results.addAll(results2);

        }
        Collections.sort(results);

        int toIndex = (int) session.getAttribute("toIndex");
        List<Status> subList = results.subList(0, toIndex);

        return subList;

    }
}

使用注释时遇到的异常如下:

Apr 16, 2013 8:41:23 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/Home.xhtml]
java.io.NotSerializableException: com.bean.StatusBean2
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:293)
    at com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:167)
    at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:119)
    at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:155)
    at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:221)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:406)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:127)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:117)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Apr 16, 2013 8:41:23 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/FreeBird_New2] threw exception
java.io.NotSerializableException: com.bean.StatusBean2
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeArray(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:293)
    at com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:167)
    at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:119)
    at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:155)
    at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:221)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:406)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:127)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:117)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

2 回答 2

4

在托管 bean 上使用@ViewScoped注释:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class AViewScopedBean {
    //managed bean contents...
}

如果您不喜欢注释配置(真的很奇怪),您可以在 faces-config.xml 上设置视图范围

<managed-bean>
    <managed-bean-name>aViewScopedBean<managed-bean-name>
    <managed-bean-class>some.package.AViewScopedBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
</managed-bean>

请注意,这只适用于 JSF 2。检查您的faces-config文件是否配置为处理 JSF 2.x 版本:

<!-- relevant part of faces-config.xml file for this Q/A -->
<faces-config ... version="2.1">

根据您的问题更新进行编辑:

错误消息非常简单:

java.io.NotSerializableException: com.bean.StatusBean2

这意味着您com.bean.StatusBean2还必须实现该Serializable接口。从java.io.Serializable文档:

在遍历图时,可能会遇到不支持 Serializable 接口的对象。在这种情况下,NotSerializableException将抛出并标识不可序列化对象的类。(这是你得到的错误)

您可以在此处了解有关 Java 序列化的更多信息:Java 序列化

从您的问题来看:是否有必要实现可序列化?,BalusC 已经发布了一个很好的答案/解释:JSF backing bean should be serializable?

于 2013-04-16T15:01:04.097 回答
2

检查您(例如: http: //java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsdxsd )中列出的内容应该会告诉您范围是允许的托管 bean 范围:faces-config.xmlview

  <xsd:complexType name="faces-config-managed-bean-scopeOrNoneType">
    <xsd:annotation>
      <xsd:documentation>
        <![CDATA[[
        Defines the legal values for the <managed-bean-scope>
        element's body content, which includes all of the scopes
        normally used in a web application, plus the "none" value
        indicating that a created bean should not be stored into
        any scope.  Alternatively, an EL expression may be used
        as the value of this element.  The result of evaluating this
        expression must by of type java.util.Map.

        ]]>
      </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleContent>
      <xsd:restriction base="javaee:string">
        <xsd:pattern value="view|request|session|application|none|#\{.*\}"/>
      </xsd:restriction>
    </xsd:simpleContent>
  </xsd:complexType>

假设 jsf 2.0 或更高版本

这本身并不是一个新的答案,只是另一种方式来自己找出它是允许的

- - - - - - - - 编辑 - - - - - - - -

检查这篇文章

请注意,bean 需要实现 Serializable,因为它将存储在视图映射中,而视图映射又存储在会话中。一些 servletcontainer 配置将会话存储在磁盘而不是内存中。当您将 JSF 视图状态保存方法配置到客户端而不是(默认)服务器时,这也是强制性的。

解释为什么视图范围需要Serializable.

于 2013-04-16T15:15:58.947 回答