0

I have a @ViewScoped bean with a simple testing method, my problem is when that when I open the page for the first time it's intialized correctly and everything is fine but when I browse to another page the pageOnLoad method is still running and the console is printing out values. Is this a normal behaviour of @ViewScoped bean ? How to detect that the view has been changed and interrupt the method process?

@ManagedBean 
@ViewScoped
public class GeneralBean implements Serializable{

private static final long serialVersionUID = 5832997040927489736L;


public void pageOnLoad() {

    for (int i=0;i<999999;i++){
        System.out.println(i);
    }
}
}

my xhtml page:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions" >
<h:head>
<script type="text/javascript">
$(document).ready(function() {
    loadInitialList();
});
</script>
</h:head>
<h:body>
 <h:form>
  <p:remoteCommand name="loadInitialList" actionListener="#{generalBean.pageOnLoad}" update="generalForm" /> 
 </h:form>

 <h:form id="generalForm">
 </h:form>
</h:body>
</html>
hamza-don
  • 455
  • 5
  • 26
  • A view scoped bean is not shared across different browser's tabs / windows / different clients. I do not understand any relationship with `` – Tiny Jan 27 '16 at 17:30
  • I have put the remoteCommand component to make my own ajax status with an animated picture which will be hidden when the `pageOnLoad` method will finish it's job. – hamza-don Jan 27 '16 at 17:38
  • My problem is that when I open the page the is called and it is printing the `System.out.println(i);` values but even when I browse to another page that loop is still running and printing in my console. – hamza-don Jan 27 '16 at 17:40
  • "*that loop is still running*". It should not happen. The loop iterations should be according to a new instance of the specified view scoped bean or that loop is still going on, when you open the page backed by that view scoped bean in another tab - the loop is likely not finished before the same page is opened in another tab. They are completely different independent instances of the bean and will cause no problem like concurrent access. – Tiny Jan 27 '16 at 17:46
  • Opening the same page in different tab is running a new loop while the old loop is still running also, it's likely they are running in parallel. – hamza-don Jan 27 '16 at 18:03
  • They are not running concurrently as they know nothing about each other - both are independent instances. – Tiny Jan 27 '16 at 18:05
  • Ok I see. Do you know what to do to stop the old running instance? – hamza-don Jan 27 '16 at 18:07
  • Choose another verbose scoped bean like a session scoped bean which will share the same instance across different tabs / windows. (Wisely choosing a **right** bean scope is fully transparently dependent upon the business requirements of projects being developed. You may use a request scoped bean depending upon the requirements). – Tiny Jan 27 '16 at 18:17
  • @BalusC Yes that is what I was searching for. – hamza-don Jan 28 '16 at 19:41

0 Answers0