简体   繁体   中英

NullPointerException using JSF 2.3 websocket using JSF navigation

I have some simple navigation using primefaces menu. However I have jsf websockets on both pages. This causes a NullPointerException when navigating between pages. When navigating to Portfolio if there are websockets present in the Portfolio page the NullPointerException is returned when attempting to navigate to it and back to the Stocks page. If I remove all of the websockets from the Portfolio page, then the navigation kind of works - the first click causes the below exception but the second works. If I remove all websockets from both pages the navigation works without issue, but obviously I need the websockets for the page functionality.

EDIT: I've now made a minimal reproducible example and have changed the code below. Note that h:commandLink are included and they also cause the same issue when selecting page2.

java.lang.NullPointerException
at com.sun.faces.push.WebsocketFacesListener.processEvent(WebsocketFacesListener.java:118)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:147)
at javax.faces.event.ComponentSystemEvent.processListener(ComponentSystemEvent.java:134)
at com.sun.faces.application.ApplicationImpl.processListenersAccountingForAdds(ApplicationImpl.java:2340)
at com.sun.faces.application.ApplicationImpl.invokeViewListenersFor(ApplicationImpl.java:2158)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:337)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:292)
at javax.faces.application.ApplicationWrapper.publishEvent(ApplicationWrapper.java:748)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:113)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:223)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:748)

This is my menu page:

<?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://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:head>
</h:head>

<h:body>

    <div class="column side">
        <h2>Side</h2>
        <h:form>
            <h:commandLink value="Page1" actionListener="#{menuViewTest.selectPageOne}" />
            <h:commandLink value="Page2" actionListener="#{menuViewTest.selectPageTwo}" />
        </h:form>
    </div>

    <div class="column middle">
        <h2>Middle</h2>
            <ui:include src="#{menuViewTest.view}">
            </ui:include>
    </div>
</h:body>
</f:view>
</html>

Backing bean for MenuTest:

@Named("menuViewTest")
@SessionScoped
public class MenuViewTest implements Serializable {

private static final long serialVersionUID = -1015364935820045523L;
private String view;

public void selectPageOne() {
    setView("page1.xhtml");
}

public void selectPageTwo() {
    setView("page2.xhtml");
}

public String getView() {
    return view;
}

public void setView(String view) {
    this.view = view;
}

  public String getSelectPageOne(){
    return "page1.xhtml";
}

public String getSelectPageTwo(){
    return "page2.xhtml";
}

}

Page1:

<ui:composition  xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core"
          xmlns:p="http://primefaces.org/ui"
          xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<script type="text/javascript">

    function notifyListener(message, channel, event) {
        console.log("notifyListener message: " + message);
        console.log("notifyListener channel: " + channel);
        console.log("notifyListener event: " + event);
    }
</script>

<h2>Page 1</h2>

<f:websocket channel="notify" onmessage="notifyListener" />

</ui:composition>

Page 2:

<ui:composition  xmlns="http://www.w3.org/1999/xhtml"
             xmlns:h="http://xmlns.jcp.org/jsf/html"
             xmlns:f="http://xmlns.jcp.org/jsf/core"
             xmlns:p="http://primefaces.org/ui"
             xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<script type="text/javascript">

        function notifyListener(message, channel, event) {
        console.log("notifyListener message: " + message);
        console.log("notifyListener channel: " + channel);
        console.log("notifyListener event: " + event);
    }
</script>

<h2>Page 2</h2>

<f:websocket channel="notify2" onmessage="notifyListener" />
</ui:composition>

The websocket exception is fixed by upgrading to the latest stable release of Glassfish (5.1.0) JSF version 2.3 with mojarra 2.3.9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM