日期:2014-05-16  浏览次数:20330 次

jsf 事件之 值更改事件(实现组建之间的联动)
    jsf的六个生命周期:请求--->回复视图-->应用请求值-->过程验证-->更新模型值-->调用应用程序-->呈现相应-->响应
    值更改事件发生在 阶段“过程验证”之后
    下面的实例实现:下拉框是一组国家列表,当选中一个国家时候,再另一个下拉框中填充该国家的主要城市,并且更改另一个输入框中显示的当前国家的名称:
1,后台bean模型
package com.jsf.bean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
public class RegisterAction {
	    //改变下拉框后,输出框中的内容id
	    private String result;
	    //下拉框国家的id
	    private String selectedContent = null;
	    //国家列表
	    private ArrayList countryItems = null;
        //下拉框城市的id	    
	    private String selectCity = null;
	    //城市下拉框中的列表
	    private List cityItems = null;
	    //定义三个城市的类表
        private static List china = new ArrayList();
        private static List america = new ArrayList();
        private static List japan = new ArrayList();
        static{
        	china.add(new SelectItem("北京","北京",null));
        	china.add(new SelectItem("上海","上海",null));
        	china.add(new SelectItem("广州","广州",null));
        	china.add(new SelectItem("深圳","深圳",null));
        	america.add(new SelectItem("纽约","纽约",null));
        	america.add(new SelectItem("华盛顿","华盛顿",null));
        	america.add(new SelectItem("旧金山","旧金山",null));
        	america.add(new SelectItem("加州","加州",null));
        	japan.add(new SelectItem("东京","东京",null));
        	japan.add(new SelectItem("神户","神户",null));
        	japan.add(new SelectItem("广岛","广岛",null));
        	japan.add(new SelectItem("长崎","长崎",null));
        }
	    public String getSelectCity() {
			return selectCity;
		}
		public void setSelectCity(String selectCity) {
			this.selectCity = selectCity;
		}
		public String getSelectedContent() {
	    return selectedContent;
	    }
	    public List getCityItems() {
			return cityItems;
		}
		public void setCityItems(List cityItems) {
			this.cityItems = cityItems;
		}
		public void setSelectedContent(String selectedContent) { //保持下拉框的状态不改变
	    this.selectedContent = selectedContent;
	    }
	    public String getResult() {
	        return result;
	    }
	    public void setResult(String result) {
	        this.result = result;
	    }
	    //国家下拉类表中的值改变之后的action事件
	    public void myEvent(ValueChangeEvent e) { //下拉框改变时触发的事件
	    	System.out.println("you change the value fo ");
	        FacesContext context = FacesContext.getCurrentInstance();
	        String clientId = e.getComponent().getId();
	        if (clientId.equals("select")) {
	            setResult(e.getNewValue().toString());
	            if(e.getNewValue().toString().equals("中国")){
	            	setCityItems(china);
	            }
	            if(e.getNewValue().toString().equals("美国")){
	            	setCityItems(america);
	            }
	            if(e.getNewValue().toString().equals("日本")){
	            	setCityItems(japan);
	            }
	        }
	    }
	    //在创建bean之后,呈现视图之前给国家列表中的内容赋值
	    public Collection getCountryItems() { //给下拉框赋值
	        SelectItem aa= new SelectItem();
	        if (countryItems == null) {
	        countryItems = new ArrayList();
	        countryItems.add(new SelectItem("中国", "中国", null));
	        countryItems.add(new SelectItem("美国", "美国", null));
	        countryItems.add(new SelectItem("日本", "日本", null));
	        }
	        return countryItems;
	    }
	    public void setCountryItems(ArrayList<SelectItem> countryItems) {
	        this.countryItems = countryItems;
	    }

}


2,页面regist.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:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://