如何管理下拉之间的依赖关系?

时间:2013-03-29 11:51:45

标签: jsf jsf-2

我正在使用JSF2.0。我有三个下降:

  1. masterDropDown。
  2. childDropDownA
  3. childDropDownB
  4. 需要强制执行的业务规则是:

    1. 每当 masterDropDown 发生变化时,我需要填充 childDropDownA childDropDownB
    2. childDropDownA childDropDownB 的内容相同,当用户从 childDropDownA 中选择内容时, < em> childDropDownB 应该具有旧内容,但用户在 childDropDownA 中选择的内容除外。当用户首先选择 childDropDownB 时,适用相同的规则。
    3. 在JSF中实现这一目标的最简洁方法是什么?

1 个答案:

答案 0 :(得分:4)

普通JSF 2:

<h:form>
    <h:selectOneMenu id="ddlMaster" value="#{bean.ddlMasterSelected}">
        <f:listItems value="#{bean.ddlMasterData}" />
        <f:ajax listener="#{bean.fillChildren}" render="ddlChildA ddlChildB" />
    </h:selectOneMenu>
    <h:selectOneMenu id="ddlChildA" value="#{bean.ddlChildASelected}">
        <f:listItems value="#{bean.ddlChildAData}" />
        <f:ajax listener="#{bean.refillChildB}" render="ddlChildB" />
    </h:selectOneMenu>
    <h:selectOneMenu id="ddlChildB" value="#{bean.ddlChildBSelected}">
        <f:listItems value="#{bean.ddlChildBData}" />
        <f:ajax listener="#{bean.refillChildA}" render="ddlChildA" />
    </h:selectOneMenu>
</h:form>

托管bean

@ManagedBean
@ViewScoped
public class Bean {
    private Map<String, String> ddlMasterData;
    private Map<String, String> ddlChildAData;
    private Map<String, String> ddlChildBData;
    private String ddlMasterSelected;
    private String ddlChildASelected;
    private String ddlChildBSelected;

    @PostConstruct
    public void init() {
        //fill initial values for the drop down lists...
        //this is a raw idea, you must refine it
        DDLService ddlService = new DDLService();
        ddlMasterData = ddlService.getDDLMasterData();
        //empty data for children ddls
        ddlChildAData = new LinkedHashMap<String, String>();
        ddlChildBData = new LinkedHashMap<String, String>();
    }

    public void refillChildB(AjaxBehaviorEvent event) {
        //filling the children ddls
        DDLService ddlService = new DDLService();
        ddlChildASelected = ddlService.getDDLChildData();
        //same method to fill child data on ddlChildBSelected  because is same data
        ddlChildBSelected = ddlService.getDDLChildData();
    }

    public void fillChildren(AjaxBehaviorEvent event) {
        //when the user selects something from childDropDownA
        //childDropDownB should have its old contents
        //I assume it must be cleared, you can change this behavior though
        ddlChildBData = new LinkedHashMap<String, String>();
    }

    public void refillChildA(AjaxBehaviorEvent event) {
        //The same rule applies when the user selects childDropDownB first
        //I assume it must be cleared, you can change this behavior though
        ddlChildAData = new LinkedHashMap<String, String>();
    }
}