combobean.java

来自「OperaMasks是一种基于J2EE的Web开发技术」· Java 代码 · 共 132 行

JAVA
132
字号
package demo.form.single;

import javax.faces.model.SelectItem;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Label;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SelectItems;
import org.operamasks.faces.component.form.impl.UICombo;

@ManagedBean(name = "ComboBean", scope = ManagedBeanScope.REQUEST)
public class ComboBean {
    @Bind
    private String province = "guangdong";

    @Bind
    @SelectItems
    private SelectItem[] provinces = new SelectItem[] {
            new SelectItem("guangdong", "广东"),
            new SelectItem("heilongjiang", "黑龙江"),
            new SelectItem("beijing", "北京"), 
            new SelectItem("shanghai", "上海") };

    @Bind
    private String city = "shenzhen";

    @Bind(id = "province")
    private UICombo proCombo;
    
    @Bind(id = "city")
    private UICombo city_comboBox;

    @Bind
    @SelectItems
    private SelectItem[] cities;
    
    @Bind
    private String resultTxt;
    
    @Action
    private void province_onselect() {
        cities = queryCities(province);
        initCityCombo();
        displayResult();
    }

    @Action
    private void city_onselect() {
        displayResult();
    }
    
    private void initCityCombo() {
        if ("guangdong".equals(province)) {
          if (city_comboBox != null) {
              city_comboBox.setValue("shenzhen");
              city_comboBox.enable();
          }
          
      } else if ("heilongjiang".equals(province)) {
          if (city_comboBox != null) {
              city_comboBox.setValue("harbin");
              city_comboBox.enable();
          }          
      } else {
          if (city_comboBox != null) {
              city = "";
              city_comboBox.disable();
          }
      }
    }
    private SelectItem[] queryCities(String province) {
        if ("guangdong".equals(province)) {
            return new SelectItem[] {
                    new SelectItem("guangzhou", "广州"),
                    new SelectItem("shenzhen", "深圳"),
                    new SelectItem("zhuhai", "珠海") };
        } else if ("heilongjiang".equals(province)) {
            return new SelectItem[] {
                    new SelectItem("harbin", "哈尔滨"),
                    new SelectItem("qiqihaer", "齐齐哈尔"),
                    new SelectItem("daqing", "大庆") };
        } else {
            return new SelectItem[] {};
        }
    }
    
    @BeforeRender
    public void beforeRender(boolean isPostBack) {
        if (cities == null) {
            displayResult();
        }
    }
    
    public void displayResult() {
        if (cities == null) {
            cities = queryCities(province);
        }
        resultTxt = "您选择的是:" + getProvinceLabel(proCombo.getValue().toString());
        
        if (cities.length > 0) {            
            resultTxt += "省 " + getCityLabel(city_comboBox.getValue().toString()) + "市";
        } else {
            resultTxt += "市";
        }
    }
    
    private String getProvinceLabel(String prov) {
        for (SelectItem si : provinces) {
            if (si.getValue().equals(prov)) {
                return si.getLabel();
            }
        }
        return "";
    }
    
    private String getCityLabel(String city) {
        if (cities == null) {
            cities = queryCities(province);
        }
        SelectItem[] sis = cities;
        for (SelectItem si : sis) {
            if (si.getValue().equals(city)) {
                return si.getLabel();
            }
        }
        return "";
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?