JSF 值更改事件示例

2018-09-27 15:55 更新

JSF教程 - JSF值更改事件示例


我们可以处理h:inputText或h:selectOneMenu的值更改事件。

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

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

以下代码显示如何将方法从Managed 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.tutorialspoint.test.LocaleChangeListener"
      />
   <f:selectItems value="#{userData.countries}" />
</h:selectOneMenu>

例子

以下代码来自demo.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="cn.w3cschool.common.MyValueChangedListener" />
             <f:selectItems value="#{country.countryInMap}" />
           </h:selectOneMenu>      
    </h:form>
    </h:body>
</html>

以下代码来自MyValueChangedListener.java。

package cn.w3cschool.common;


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 {
    
    UserBean country = (UserBean) FacesContext.getCurrentInstance().
      getExternalContext().getSessionMap().get("country");

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

下面的代码来自UserBean.java。

package cn.w3cschool.common;


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 UserBean 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;
  }
}
下载 Value_Changed_Event.zip

运行

将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹,并运行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成启动后,在浏览器地址栏中键入以下URL。

http://localhost:8080/simple-webapp/demo.xhtml


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号