0

I have two pages of jsf. dashboard.xhtml has a <p:poll /> tag that updates "form1" every second, while input.xhtml is just a basic crud page that updates display in dashboard.xhtml. I'm wondering because when the dashboard.xhtml is open on a different tab or a second window of browser, input.xhtml keeps losing its state and causes the view to expire ViewExpiredException. When I tried to put <o:enableRestorableView /> in template_2.xhtml, the ViewExpiredException is gone but my input values is resetting when I submit the form. The resetting is not occuring when dashboard.xhtml is not open. When it's open, the input resets randomly.

What I want to accomplish is when I input some data in the input.xhtml, I want it to display in dashboard.xhtml in realtime. Anybody knows how to do this without (ViewExpiredException)? The application runs in WebSphere 8.5(MyFaces 2.0), Primefaces 6.0, OmniFaces 1.8.3

dashboard.xhtml:

     <ui:composition template="/WEB-INF/template_1.xhtml"
            xmlns:p="http://primefaces.org/ui"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:o="http://omnifaces.org/ui">
        <ui:define name="content">
            <h:form id="form1">
               <p:poll interval="1" listener="#{dashboardBacking.updateValues}" update="form1"
                        global="false" />

               <!-- dashboard content -->
            </h:form>
         </ui:define>
        </ui:composition>

input.xhtml:

<ui:composition template="/WEB-INF/template_2.xhtml"
    xmlns:p="http://primefaces.org/ui"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:o="http://omnifaces.org/ui">
 <ui:define name="content">
    <h:form id="form2">

       <p:panelGrid>
          <p:row>
            <p:column>
              <p:inputText value="#{inputBacking.values.description}" />
            </p:column>
          </p:row>
          <p:row>
            <p:column>
              <p:commandButton id="#{inputBacking.save}" update="form2"/>
            </p:column>
          </p:row>
       </p:panelGrid>
    </h:form>
 </ui:define>
</ui:composition>

Backing bean (dashboard.xhtml):

@ManagedBean
@ViewScoped
public class DashboardBacking {
   @EJB
   private Dashboard dashboard;

   private DashboardValues values;

   @PostConstruct
   public void postConstruct(){
        values = new DashboardValues();
        updateValues();
   }

   public void updateValues(){
     DashboardValues newValues = dashboard.getValue();
     if(!newValues.exactlyEqual(values)){
        values = newValues;
     }

   }

   public DashboardValues getValues(){
       return values;
   }
}

Backing bean (input.xhtml):

@ManagedBean
@ViewScoped
public class InputBacking {

   @EJB
   private DashboardDao dao;

   private DashboardValues values;

   public void save(){
     dao.save(values)
   }

   //Getters and Setters
}

Dashboard singleton class:

@Singleton
@Startup
public class Dashboard {
   @EJB
   private DashboardDao dao;

   private DashboardValue value;

   @Schedule(second="*/5") //update dashboard value every 5 seconds
   private void updater(){
    value = dao.getLatest();
   }

   public DashvoardValue getValue(){
      return value;
   }
}

template_1.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html">
    <f:metadata>
        <o:enableRestorableView  />
    </f:metadata>
    <h:head>
    </h:head>
    <h:body>
      <ui:insert name="content"/>
    </h:body>
</f:view>
</html>

template_2.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html">
    <h:head>
    </h:head>
    <h:body>
      <ui:insert name="content"/>
    </h:body>
</f:view>
</html>
Mark Vincent Osea
  • 679
  • 2
  • 9
  • 17
  • 2
    In other words, the `` keeps creating new views instead of reusing the existing view. This can in turn have many causes which can't be nailed down based on the information provided so far. Try creating a [mcve] based on http://stackoverflow.com/tags/jsf/info. This related question is at least the best you can get: http://stackoverflow.com/q/3642919 – BalusC Nov 04 '16 at 08:28
  • @BalusC I'm not sure if my poll is creating new views because I have an implementation where I store the old values and compare it to the new values that I'm getting. It seems that it reuses that view when I first open the page. – Mark Vincent Osea Nov 04 '16 at 08:39
  • Again, the root cause of your problem isn't visible in the information provided so far. There are way too many red herrings and hidden details in the information provided so far which only leads to multiple possible invisible causes. Imagine us having a scratchpad project with everything set to bare defaults and most recent versions, how exactly can we reproduce your problem? You'd better reframe your question based on that. Again, see http://stackoverflow.com/tags/jsf/info for guidelines. – BalusC Nov 04 '16 at 10:06
  • @BalusC I think your right on `` created new views. My question is why does it create new views for other `@ViewScoped` bean? As I further investigate, when dashboard.xhtml is open and I click the save button on input.xhtml, the `@PostConstruct` is called again instead of reusing the existing view. – Mark Vincent Osea Nov 07 '16 at 08:04
  • 1
    Then continue here: http://stackoverflow.com/q/5541813 – BalusC Nov 07 '16 at 08:14
  • @BalusC None of those solutions works for me. The ViewScoped backing bean works as expected if `` is not active. Do you think it's a primefaces bug? or there's a problem on the version of jsf that i'm using. – Mark Vincent Osea Nov 07 '16 at 13:05
  • Restart at first comment. – BalusC Nov 07 '16 at 13:11
  • Hi @BalusC. I was able to figure out the root cause. It was because of the my JSF version wont allow my application to open multiple tabs while doing POST request on both tabs. It was indeed of the JSF verison of Websphere which is MyFaces 2.0.2, where `org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION` is not supported. Is there a work around in version 2.0.2 to handle this situation? Thank a lot! – Mark Vincent Osea Nov 09 '16 at 01:58

0 Answers0