首页 > 解决方案 > 我应该如何初始化 f:selectOneRadioButton 组件?

问题描述

我认为这是唾手可得的成果,但我和我的同事每天都在讨论诸如此类的事情。

所以我的视图上有多个单选按钮,大约 10...

我应该直接在构造函数中将单选按钮标签和值初始化为 Map,创建封装方法并从构造函数调用它,在静态初始化块中构建列表,还是应该在视图中硬编码?有很多方法可以实现目标,但我不知道什么是最佳实践

public RadioButtonBean(){
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
}

或者

public RadioButtonBean(){
    this.loadRadioButtonSelection();
}

private void loadRadioButtonSelection(){
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
    this.radioButtonSelection.put(“Age”,”Age”);
}

与视图

<f:selectOneRadioButton value=“#{radioButtonBean.selection}”&gt;
     <f:selectItems value=“#{radioButtonBean.radioButtonSelection}”/>
 </f:selectOneRadioButton>

或者就这个

<f:selectOneRadioButton value=“#{radioButtonBean.selection}”&gt;
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
     <f:selectItem value=“Age” label=“Age”/>
 </f:selectOneRadioButton>

标签: jsfdesign-patternsxhtmlsoftware-design

解决方案


推荐阅读