java - selectOneMenu 和 selectItems

标签 java jsf

我正在尝试使用 selectOneMenu 制作过滤器。

我有两个类别,当一个类别被选中时,必须过滤显示的结果,还有第二个类别。

JSF 代码:

<div id="box-categories" class="box-left">
            <b>Filter by topic</b>
            <h:selectOneMenu id="cat1" binding="#{interfaceContainer.documentFormContainer.selectOnes['cat1'].selectOne}" rendered="true"  onchange="javascript:refreshResults(); return false;">
                                        <f:selectItems value="#{interfaceContainer.documentFormContainer.selectOnes['cat1'].items}" />
                                </h:selectOneMenu>
            <b>and subtopic</b>
 <h:selectOneMenu id="cat2" binding="#{interfaceContainer.documentFormContainer.selectOnes['cat2'].selectOne}" rendered="true"  onchange="javascript:refreshResults(); return false;" value="#{interfaceContainer.documentFormContainer.selectOnes['cat2'].value}">
                                        <f:selectItems value="#{interfaceContainer.documentFormContainer.selectOnes['cat2'].items}" />
                                </h:selectOneMenu>
        </div>  

但是当我尝试使用此 java 代码获取值时遇到问题:

public String getStringValue(){
      if ( this.selectOne ==null || this.getSelectOne().getValue()==null)
          return "";
      return this.getSelectOne().getValue().toString();
  }

我意识到问题出在 getValue() 上,因为调试时, this.getSelectOne() 是正确的值,但 this.getSelectOne().getValue() 为 null。

有什么想法吗? 提前致谢

最佳答案

UIInput#getValue()当您尝试在更新模型值阶段之前的任何阶段访问它时,将返回 null。您显然是在 JSF 生命周期中的“错误”时刻访问它。不管怎样,在没有 Ajax 帮助的情况下在 JSF 中创建依赖下拉菜单是很糟糕的。

长话短说,具体操作方法如下:Populate child menu's (带有完整且有效的代码示例)。

关于java - selectOneMenu 和 selectItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3235499/

相关文章:

jsf - 转换错误设置值 "UserID"为 'null Converter'

java - Jboss 7 Jaas模块,成功登录后角色被覆盖

ajax - p :ajax update of ID that doesn't exists

java - 聚合查询错误15982 : "exception: field path references must be prefixed with a ' $'"

java - 为什么我需要添加 PATH Env. Windows 中用户运行 javac 的变量?

java - 在 Facelets 页面中显示异常堆栈跟踪

java - 如何将动态创建的 HtmlInputText 组件的值绑定(bind)到 bean 属性?

java - 无法启动 Activity ComponentInfo (LOGCAT)

java - 逻辑运算符表现出奇怪的行为

Java(匿名与否)内部类 : is it good to use them?