【问题标题】:JSF - associating value to checkboxes built dynamicallyJSF - 将值与动态构建的复选框相关联
【发布时间】:2011-08-21 16:24:51
【问题描述】:

我找到了一个与我想要做的非常接近的教程:http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/

唯一的变化是我在我的支持 bean 中而不是在我的 .xhtml 文件中构建我的复选框,但是没有正确设置“值”属性。

sn-p 来自我的 .xhtml 文件:

<h:form>
<h:panelGrid binding="#{myBean.myGrid}"></h:panelGrid>
<h:commandButton id="submit" type="submit">
</h:form>

来自 myBean 的 sn-p:

public HtmlPanelGrid getMyGrid()
{
  resultGrid = new HtmlPanelGrid();
  resultGrid.setColumns(2)
  List children = resultGrid.getChildren();

  FacesContext myFacesInstance = FacesContext.getCurrentInstance();
  Application myApp = myFacesInstance.getApplication();
  ExpressionFactory expFactory = myApp.getExpressionFactory();

  SelectItem tempSelectItem;
  String valuStringExpression;
  ValueExpression valExpression;

  //create panel for the checkboxes
  HtmlSelectManyCheckbox checkboxPanel = new HtmlSelectManyCheckbox();
  checkboxPanel.setLayout("pageDirection");

   List<SelectItem> checkChoiceList = new ArrayList<SelectItem>();

   for (int i=0;i<numChoices;i++)
   {
    valStringExpression = "#{myBean.responseValue["+i+"]}";
valExpression = expFactory.createValueExpression(myFacesInstance.getELContext(), valStringExpression, String.class);        
    tempSelectItem = new SelectItem(valExpression,choiceLabels.get(i));
    checkChoiceList.add(tempSelectItem);
   }

  UISelectItems checkboxList = new UISelectItems();
  checkboxList.setValue(checkChoiceList);
  checkboxPanel.getChildren().add(checkboxList);

  children.add(checkboxPanel);

return resultGrid;
}

问题是每个复选框的 value 属性没有正确映射到我的“#{myBean.responseValue[”+i+“]}”。

如果我从网页中拉出“查看源代码”,复选框标签的值显示为:

value="ValueExpression[#{myBean.responseValue[0]}]"

我已经在谷歌上搜索了几个小时,我被难住了。非常感谢任何帮助或想法!

【问题讨论】:

  • 我已经合并了您的注册和未注册帐户,因此您现在应该能够接受并支持 BalusC 的回答。

标签: jsf-2


【解决方案1】:

SelectItem 构造函数的第一个参数需要 实际 选择项值,而不是某个值表达式。相应地修复它:

Object value = valExpression.getValue(elContext);
tempSelectItem = new SelectItem(value, choiceLabels.get(i));

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多