JSF值变化的事件

在JSF中,我们可以处理<h:inputText><h:selectOneMenu>的值变化的事件。

要注册事件处理程序侦听器,请在UI组件的valueChangeListener属性中传递托管bean方法的名称。

或者实现ValueChangeListener接口,并将实现类名传递给UI组件的valueChangeListener属性。

以下代码显示了如何将托管Bean的方法注册到valueChangeListener方法。

public void localeChanged(ValueChangeEvent e){
   //assign new value to country
   selectedCountry = e.getNewValue().toString(); 
}

注册方法

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

以下代码显示了如何实现ValueChangeListener监听器方法。

public class LocaleChangeListener implements ValueChangeListener {
   @Override
   public void processValueChange(ValueChangeEvent event)
      throws AbortProcessingException {
     //access country bean directly
     UserData userData = (UserData) FacesContext.getCurrentInstance().
        getExternalContext().getSessionMap().get("userData"); 
     userData.setSelectedCountry(event.getNewValue().toString());
   }
}

并注册到<f:valueChangeListener>标签。

<h:selectOneMenu value="#{userData.selectedCountry}" onchange="submit()">
   <f:valueChangeListener type="com.zyiz.test.LocaleChangeListener"
      />
   <f:selectItems value="#{userData.countries}" />
</h:selectOneMenu>

实例

打开NetBeans,创建一个名称为:ValueChangedEvent 的Web项目,其结构如下所示 -

以下是文件:User.java 文件中的代码 -

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zyiz;

/**
 *
 * @author Maxsu
 */
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(name = "country")
@SessionScoped
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private static Map<String, String> countries;
    private String localeCode = "en"; //default value 

    static {
        countries = new LinkedHashMap<String, String>();
        countries.put("United Kingdom", "en"); //label, value
        countries.put("French", "fr");
        countries.put("German", "de");
    }

    public void countryLocaleCodeChanged(ValueChangeEvent e) {
        localeCode = e.getNewValue().toString();
    }

    public Map<String, String> getCountryInMap() {
        return this.countries;
    }

    public String getLocaleCode() {
        return localeCode;
    }

    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }
}

以下是文件:MyValueChangedListener.java 文件中的代码 -

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zyiz;

/**
 *
 * @author Maxsu
 */
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;

public class MyValueChangedListener implements ValueChangeListener {

    @Override
    public void processValueChange(ValueChangeEvent event)
            throws AbortProcessingException {

        User country = (User) FacesContext.getCurrentInstance().
                getExternalContext().getSessionMap().get("country");

        country.setLocaleCode(event.getNewValue().toString());

    }

}

以下是文件:index.xhtml 文件中的代码 -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
    <h:body>
        <h:form>
            Selected country locale : 
            <h:inputText id="country" value="#{country.localeCode}" size="20" />
            Select a country {method binding}: 
            <h:selectOneMenu value="#{country.localeCode}" onchange="submit()"
                             valueChangeListener="#{country.countryLocaleCodeChanged}">
                <f:selectItems value="#{country.countryInMap}" />
            </h:selectOneMenu>
            Select a country: 
            <h:selectOneMenu value="#{country.localeCode}" onchange="submit()">
                <f:valueChangeListener type="com.zyiz.MyValueChangedListener" />
                <f:selectItems value="#{country.countryInMap}" />
            </h:selectOneMenu>      
        </h:form>
    </h:body>
</html>

运行项目

ValueChangedEvent 项目上点击右键,选择 【运行】,在Tomcat启动完成后,打开浏览器访问以下地址:

http://localhost:8084/ValueChangedEvent/

如果程序没有错误,应该会看到如下界面 -


上一篇:JSF数据表(ui:repeat)创建表

下一篇:JSF操作事件

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程