Valuechangelistener Doubt in JSF

后端 未结 3 1795
灰色年华
灰色年华 2020-12-10 16:38

HI,

Please see the following code:

                

        
3条回答
  •  无人及你
    2020-12-10 16:54

    The ValueChangeListener will only be called when the form is submitted, not when the value of the input is changed. Thus, if you want to run this listener when the value is modified, you have two solutions:

    1. Submit your form when the onchange event is fired (this is what you did in your code);
    2. Use an Ajax call instead, by using some dedicated components (already integrated in JSF2, with , or third-parties libraries such as Richfaces, Primefaces...).

    Here is an example with Richfaces:

    
        
        
    
    

    Regarding the code of your listener, it seems correct, but why question is why do you need a ValueChangeListener here? Indeed, this listener is usefull when you want to track a modification of a value. That's why the ValueChangeEvent provides both getOldValue() and getNewValue() methods.

    In your code, you do not care about the old value, so basically, you could "simply" do an action instead of a valueChangeListener (ex. with Richfaces):

    
        
        
    
    

    Finally, regarding the difference between the valueChangeListener attribute and is that the first binds a Java method (#{myBean.myMethod}), while the second binds a Java class (type="com.foo.MyListenerClass") which implements the ValueChangeListener interface. So the second one could be more generic than the first one...

提交回复
热议问题