Firefox stores array contents inside JSF-Viewscoped-Managed-Bean even after page refresh

This strange behavior only occurs in Firefox (specifically Firefox 8). Therefore, I have a dataTable , which I can do multiple selection . A submit button that displays a list of selected items in and dataList . If the user hasnโ€™t selected anything, an msg error message appears asking the user to select something. A dialog will not appear if the user has not selected anything. This code does it all. However, FireFox behaves strangely if you follow these steps:

  • Click to select an item on dataTable
  • Then refresh the page (F5 or Ctl + R) on the page (you can see that the selection is cleared)
  • Then click "Submit", it will show everything that I just selected.

This is uninteresting since the update should clear everything that you just select due to the nature of the @ViewScoped bean. This behavior is observed only in Firefox. IE 8 behaves right for me. Is this a mistake, or am I doing something wrong here?

Mojarra 2.1 + PrimeFaces3.0 Final + Tomcat 7

UPDATE I did some debugging when I refresh the page, the value of the selectedFoods array becomes null , but for some odd reason, when it reaches public void checkSelection() , it hold the value of the previous selection. So strange.

Here is my code.

 <p:growl id="messages" showDetail="true" /> <p:messages id="msgs"/> <h:form id="form"> <p:dataTable value="#{viewBean.foodList}" var="item" selection="#{viewBean.selectedFoods}" selectionMode="multiple" rowKey="#{item}"> <p:column> #{item} </p:column> <f:facet name="footer"> <p:commandButton value="Submit" update=":form:display :dataList" action="#{viewBean.checkSelection}"/> </f:facet> </p:dataTable> <p:dataList id="display" value="#{viewBean.selectedFoods}" var="item" itemType="disc"> #{item} </p:dataList> </h:form> <p:dialog id="dialog1" widgetVar="dialog1" dynamic="true" width="200"> <p:dataList id="dataList" value="#{viewBean.selectedFoods}" var="item" itemType="disc"> #{item} </p:dataList> </p:dialog> 

Here is my driven bean

 @ManagedBean @ViewScoped public class ViewBean implements Serializable { private List<String> foodList; private String[] selectedFoods; @PostConstruct public void init() { foodList = new ArrayList<String>(); foodList.add("Pizza"); foodList.add("Pasta"); foodList.add("Hamburger"); } public void checkSelection(){ RequestContext requestContext = RequestContext.getCurrentInstance(); if(selectedFoods.length > 0){ requestContext.execute("dialog1.show()"); }else{ FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error", "Please select")); requestContext.addPartialUpdateTarget("messages"); } } //setter, getter } 
+4
source share
1 answer

Your code is ok. What you see is related to what should be a Firefox function (I was able to reproduce this on FF4). The selection model for p:dataTable implemented using a hidden form field. When you reload the page, Firefox tries to save and restore the values โ€‹โ€‹of the form fields that have been changed so that you do not lose what you entered. You can observe this by adding <h:inputText/> to your view, typing something in the input and reloading.

I'm not sure if the Firefox team was meant to do this to apply to hidden form fields, but I believe they had a good chance. I plan to file an error report using Primefaces to either initialize hidden input or read input at boot to make a p:dataTable . Any decision should result in the visualized selection and the hidden selection model being synchronized.

+1
source

Source: https://habr.com/ru/post/1389727/


All Articles