JSFJSF用戶界面組件模型
JSF UI組件示例
JSF <h:inputText>標籤
JSF <h:outputText>標籤
JSF <h:form>標籤
JSF <h:commandButton>標籤
JSF <h:inputtextarea>標籤
JSF <h:commandLink>標籤
JSF <h:inputSecret>標籤
JSF <h:inputHidden>標籤
JSF <h:inputFile>標籤
JSF <h:graphicImage>標籤
JSF <h:message>標籤
JSF <f:ajax>標籤
JSF單選按鈕
JSF表單組合框
JSF列表框
JSF多選列表框
JSF輸出格式化
JSF輸出樣式
JSF <h:attribute>標籤
JSF <h:setPropertyActionListener>標籤
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.yiibai.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.yiibai;
/**
*
* @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.yiibai;
/**
*
* @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.yiibai.MyValueChangedListener" />
<f:selectItems value="#{country.countryInMap}" />
</h:selectOneMenu>
</h:form>
</h:body>
</html>
運行項目
在ValueChangedEvent 項目上點擊右鍵,選擇 【運行】,在Tomcat啓動完成後,打開瀏覽器訪問以下地址:
http://localhost:8084/ValueChangedEvent/
如果程序沒有錯誤,應該會看到如下界面 -