JSF中的Valuechangelistener怀疑 [英] Valuechangelistener Doubt in JSF

查看:154
本文介绍了JSF中的Valuechangelistener怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

请参见以下代码:

                <h:selectOneMenu id="countries" value="#{countryBean.selectedCountry}" onchange="submit()
                                    valueChangeListener="#{countryBean.changeCountry}">
                    <f:selectItems value="#{countryBean.countries }" />
                </h:selectOneMenu>  

后备豆

public void changeCountry(ValueChangeEvent event){      
    String newValue = (String)event.getNewValue();
    String oldValue = (String)event.getOldValue();

    System.out.println("New Value : " + newValue);
    System.out.println("Old Value : " + oldValue);

    if ("1".equals(newValue)){
        this.countries = new ArrayList<SelectItem>();
        this.cities.add(new SelectItem("1","Delhi"));
        this.cities.add(new SelectItem("2","Mumbai"));
    }
    if ("2".equals(newValue)){
        this.cities = new ArrayList<SelectItem>();
        this.cities.add(new SelectItem("1","Mossco"));
    }       
}

请让我知道实施是否正确.一切正常. 我的问题是:

Please let me know if the implementation is correct. It is working fine. My questions are:

  • h:selectOneMenu 标签内添加 f:valueChangeListener 标签有什么好处.我使用了普通属性valueChangeListener =#{countryBean.changeCountry}".
  • 是否需要使用此代码 onchange ="submit()来更改值.
  • 通过实现ActionListener接口编写自定义侦听器与仅使用UIComponent标记中的属性(action ="methodName")有什么区别? 请给我解释一下.
  • What is the advantage of adding the f:valueChangeListener tag inside the h:selectOneMenu tag. I have used the normal attribute valueChangeListener="#{countryBean.changeCountry}".
  • Is it necessary to use onchange="submit() this code to change the values.
  • What is difference between writing the custom listeners by implementing the ActionListener interface and just using the attribute in the UIComponent tags (action="methodName"). Please explain me.

推荐答案

ValueChangeListener仅在提交表单时调用,而在输入值更改时不调用.因此,如果要在修改值后运行此侦听器,则有两种解决方案:

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. 在触发onchange事件时提交表单(这是您在代码中所做的事情);
  2. 通过使用一些专用组件(已与<f:ajax>集成在JSF2中,或诸如Richfaces,Primefaces等第三方库中)来使用Ajax调用.
  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 <f:ajax>, or third-parties libraries such as Richfaces, Primefaces...).

以下是Richfaces的一个示例:

Here is an example with Richfaces:

<h:selectOneMenu id="countries" value="#{countryBean.selectedCountry}" valueChangeListener="#{countryBean.changeCountry}">
    <a4j:support event="onchange" .../>
    <f:selectItems value="#{countryBean.countries }" />
</h:selectOneMenu>

关于您的侦听器的代码,这似乎是正确的,但是为什么要问为什么在这里为什么需要ValueChangeListener ?确实,当您要跟踪值的修改时,此侦听器很有用.这就是ValueChangeEvent同时提供getOldValue()getNewValue()方法的原因.

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.

在您的代码中,您无需关心旧值,因此,基本上,您可以简单地"执行操作而不是valueChangeListener(例如,使用Richfaces):

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):

<h:selectOneMenu id="countries" value="#{countryBean.selectedCountry}">
    <a4j:support event="onchange" actionListener="#{countryBean.changeCountry}"/>
    <f:selectItems value="#{countryBean.countries }" />
</h:selectOneMenu>

最后,关于valueChangeListener属性和<f:valueChangeListener>之间的区别是,第一个绑定了Java方法(#{myBean.myMethod}),而第二个绑定了实现ValueChangeListener的Java类(type="com.foo.MyListenerClass").界面.因此第二个可能比第一个更通用...

Finally, regarding the difference between the valueChangeListener attribute and <f:valueChangeListener> 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...

这篇关于JSF中的Valuechangelistener怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆