W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們可以處理h:inputText或h:selectOneMenu的值更改事件。
要注冊(cè)事件處理程序偵聽器,請(qǐng)傳遞UI組件的valueChangeListener屬性中的托管bean方法的名稱。
或者實(shí)現(xiàn)ValueChangeListener接口,并將實(shí)現(xiàn)類名稱傳遞給UI Component的valueChangeListener屬性。
以下代碼顯示如何將方法從Managed Bean注冊(cè)到valueChangeListener方法
public void localeChanged(ValueChangeEvent e){ //assign new value to country selectedCountry = e.getNewValue().toString(); }
注冊(cè)方法
<h:selectOneMenu value="#{userData.selectedCountry}" onchange="submit()" valueChangeListener="#{userData.localeChanged}" > <f:selectItems value="#{userData.countries}" /> </h:selectOneMenu>
以下代碼顯示了如何實(shí)現(xiàn)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()); } }
并注冊(cè)到f:valueChangeListener標(biāo)簽。
<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文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/demo.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: