Error: java.io.NotSerializableException: javax.faces.view.facelets.ValidatorHandler when validating composite component

Error: java.io.NotSerializableException: javax.faces.view.facelets.ValidatorHandler when validating composite component

我在 Tomcat 7.0 中使用 JSF 实现 Mojarra 2.0.3-FCS,当我尝试将任何类型的 JSF 验证器与复合组件一起使用时发生错误。 在这个例子中,我在 inputText 上使用了 f:validateLength 并且一旦我输入了一个小于最小值的值,就会发生异常而不是显示一个错误消息,你能帮我理解它的来源吗错误:

index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:util="http://java.sun.com/jsf/composite/util"
      xmlns:ui="http://java.sun.com/jsf/facelets">
   <h:head>
      <title>#{msgs.loginHeading}</title>
      <h:outputStylesheet library="css" name="styles.css" />
   </h:head>
   <h:body>
      <util:login namePrompt="#{msgs.namePrompt}"
            passwordPrompt="#{msgs.passwordPrompt}"
            name="#{user.name}"
            password="#{user.password}"
            loginAction="#{user.login}"
            loginButtonText="#{msgs.loginButtonText}">

         <f:validateLength minimum="4" for="nameInput"/>
         <f:actionListener type="com.corejsf.LoginActionListener" for="loginButton"/>

         <f:facet name="heading" styleClass="header">
            <div class="prompt">#{msgs.loginPrompt}</div>
         </f:facet>

         <f:facet name="error" styleClass="error">
            <h:messages layout="table" styleClass="error"/>
         </f:facet>

         <!-- Child component -->
         <h:link outcome="register">#{msgs.registerLinkText}</h:link>

      </util:login>
      <ui:debug/>
   </h:body>
</html>

login.xhtml(CC 实现)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:composite="http://java.sun.com/jsf/composite">

   <composite:interface>
      <composite:editableValueHolder name="nameInput" targets="form:name"/>
      <composite:editableValueHolder name="passwordInput" targets="form:password"/>
      <composite:editableValueHolder name="inputs"
            targets="form:name form:password"/>
      <composite:actionSource name="loginButton" targets="form:loginButton"/>

      <composite:attribute name="name"/>
      <composite:attribute name="password"/>

      <composite:attribute name="namePrompt"/>
      <composite:attribute name="passwordPrompt"/>

      <composite:attribute name="loginValidate" 
         method-signature="void validateLogin(ComponentSystemEvent e)
            throws javax.faces.event.AbortProcessingException"/>

      <composite:attribute name="loginAction" 
         method-signature="java.lang.String action()"/>

      <composite:facet name="heading"/>
      <composite:facet name="error"/> 
   </composite:interface>

   <composite:implementation>
      <h:outputScript library="components/util" name="login.js" target="head"/>
      <h:form id="form" onsubmit="return checkForm(this, '#{cc.clientId}')">
         <composite:renderFacet name="heading"/>
         <h:panelGrid columns="2">
            #{cc.attrs.namePrompt}
            <h:panelGroup>
               <h:inputText id="name" value="#{cc.attrs.name}"/>
               <h:message for="name"/>
            </h:panelGroup>

            #{cc.attrs.passwordPrompt}

            <h:panelGroup>
               <h:inputSecret id="password" value="#{cc.attrs.password}" size="8"/>
               <h:message for="password"/>
            </h:panelGroup>
         </h:panelGrid>

         <p>
            <h:commandButton id="loginButton"
               value="#{cc.attrs.loginButtonText}"
               action="#{cc.attrs.loginAction}"/>
         </p>

      </h:form>

      <composite:renderFacet name="error"/>

      <p><composite:insertChildren/></p>

      <p>#{cc.resourceBundleMap.footer}</p>
   </composite:implementation>
</html>

下面是堆栈跟踪:

java.io.NotSerializableException: javax.faces.view.facelets.ValidatorHandler
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at java.util.ArrayList.writeObject(ArrayList.java:742)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at java.util.ArrayList.writeObject(ArrayList.java:742)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1377)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1173)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1377)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1173)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at java.util.HashMap.writeObject(HashMap.java:1129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    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:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

注意:相同的代码适用于 GlassFish 3.1.2

看起来只是使用的 JSF 实现中的错误。

I am using the JSF implementation Mojarra 2.0.3-FCS

这已经快 5 岁了(2010 年 7 月)。 currently 已经是 2.1.29(2014 年 7 月)。或者,由于您使用的是 Servlet 3.0 兼容容器 (Tomcat 7),请选择最新的 2.2.x,目前是 2.2.9(2014 年 12 月)。

NB: the same code is working with GlassFish 3.1.2

这进一步证实了这是 Mojarra 中的错误。它在幕后使用 Mojarra 2.1.6。

作为初学者,当您看到来自 API/impl 本身而不是您的代码的异常时,请始终查看您是否使用的是最新版本的 API/impl 并升级它。如果仍然不能解决问题,请查看其 issue tracker,或在此处提问 :)