You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Wolfgang <wk...@ebi.ac.uk> on 2007/10/22 09:34:36 UTC

ValueChangeListener is executed three times

Hi,

I have three different SelectOneMenu components with ValueChangeListeners.
Each ValueChangeListener calles the same method!

If you now change the value from one of the selectOneMenus all three 
ValueChangeListener
will be called, so the processValueChangePosResult method is called 
three times.

I tried it also with three different ValueChangeListener methods. But 
each time you change
one of the selectOneMenu's values all three changeListeners will be called.

I suppose it depends on the onchange="submit()" method,  because it 
appears in all three menus...

Any suggestions?
Thanks!
Wolfgang


<h:selectOneMenu id="itemPosPicr"
                            rendered="#{currentData.renderedPicrPos}"
                            
valueChangeListener="#{siteChanger.processValueChangePosResult"
                            onchange="submit()">
               <f:selectItems value="#{currentData.itemPosPicr}"/>
           </h:selectOneMenu>

public void processValueChangePosResult(ValueChangeEvent value) throws 
IOException {

       String selectedValue = (String) value.getNewValue();
       System.out.print(selectedValue);
             
FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.google.de"); 

   }


Re: ValueChangeListener is executed three times

Posted by David Brainard <sd...@gmail.com>.
Wolfgang wrote:
> Hi,
>
> I have three different SelectOneMenu components with 
> ValueChangeListeners.
> Each ValueChangeListener calles the same method!
>
> If you now change the value from one of the selectOneMenus all three 
> ValueChangeListener
> will be called, so the processValueChangePosResult method is called 
> three times.
>
> I tried it also with three different ValueChangeListener methods. But 
> each time you change
> one of the selectOneMenu's values all three changeListeners will be 
> called.
>
> I suppose it depends on the onchange="submit()" method,  because it 
> appears in all three menus...
>
> Any suggestions?
> Thanks!
> Wolfgang
>
>
> <h:selectOneMenu id="itemPosPicr"
>                            rendered="#{currentData.renderedPicrPos}"
>                            
> valueChangeListener="#{siteChanger.processValueChangePosResult"
>                            onchange="submit()">
>               <f:selectItems value="#{currentData.itemPosPicr}"/>
>           </h:selectOneMenu>
>
> public void processValueChangePosResult(ValueChangeEvent value) throws 
> IOException {
>
>       String selectedValue = (String) value.getNewValue();
>       System.out.print(selectedValue);
>             
> FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.google.de"); 
>
>   }
>
>
Hi Wolfgang,

There are two workaround for this problem:
1) rather than doing onchange="submit" you can call another javascript 
method which will click an invisible button(use CSS to hide the button) 
which will have the required method to call as action binding. Here the 
method which we want to call is cald only once.
eg:
jsp:call clickMe() a js method

<h:selectOneMenu id="itemPosPicr_3" onchange="clickMe()"
            
valueChangeListener="#{selectOneBean.processValueChangePosResult}">
            <f:selectItems value="#{selectOneBean.map3}" />
        </h:selectOneMenu>

button: click is the method we want to call.

<t:commandButton forceId="true" id="displayRecords"
            action="#{selectOneBean.click}" styleClass="searchButton">
        </t:commandButton>

js: Use this to click . this will be inside the js method clickMe()

document.getElementById("displayRecords").click();

2) another way is to use the same way as you had implemented but add a 
default value to the dropdown so that we can check with a condition.

public void processValueChangePosResult(ValueChangeEvent value) throws 
IOException {
        String selectedValue = (String) value.getNewValue();
        if(!selectedValue.equals("default")){
            System.out.println(selectedValue);
        }
    }

Where default is the value of the default selected item. This will help 
us to prevent the line ; System.out.println(selectedValue);  to be 
executed 3 times.

Anyhw we cant prevent the method being cald 3 times but we can assure 
that the portion to be executed is done only once.


Personally i prefer the 1st way. I have implemented like that in many 
places.


Regards,
David Brainard Sounthiraraj.