【问题标题】:changing values of selectOneMenu in JSF在 JSF 中更改 selectOneMenu 的值
【发布时间】:2013-11-05 10:05:24
【问题描述】:

我是 JSF 的新手,我被困在一个地方。我有一个包含两个项目的下拉列表。

  1. 银行

  2. 现金

现在,如果我选择银行,则另一个下拉菜单应填充与银行相关的项目,如果我选择现金,现金项目应显示在第二个下拉菜单中。

如何在 JSF 中做到这一点?我必须在哪里进行更改?

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 从哪里获取这些数据以填充下拉列表。?
  • 只需通过一些基础教程即可牢牢掌握基本知识。从这里开始:stackoverflow.com/tags/selectonemenu/info 一旦你有了具体的代码和具体的技术编程问题,随时欢迎在这里提问。
  • 我正在从数据库 MySQL 获取数据
  • 我可以使用 Javascript 并在其中添加方法吗?
  • 更好的是,以自动生成正确的 HTML/JS 代码的方式编写 JSF 代码。还是您错过了 JSF 的全部意义?再次,请参阅我之前评论中的链接。它包含一个填充子菜单的具体示例。

标签: jsf selectonemenu


【解决方案1】:

你可以使用 primefaces 来改进 jsf 开发

 <p:selectOneMenu id="cboCountries" value="#{beanCountries.ValueSelected}" effect="fade">
       <f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
       <f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
       <p:ajax event="change" update="cboCities" listener="beanCountries.listCities()"/> //notice "update" refresh an especific DOM Element of an especific ID
 </p:selectOneMenu>

 <p:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}" effect="fade">
       <f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
       <f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array 
 </p:selectOneMenu>

" 是 primefaces 的一个特殊标记,event = "change" 将调用您从第一个 "ComboBox" 中选择一个元素,然后属性更新将刷新第二个 ComboBox,并且属性 "listener" 将执行逻辑您想要执行的操作,在这种情况下,方法“listCities()”将使用 DB o Arrays 中的值填充“listOfCities”。

所以在没有primefaces的jsf中会是这样的:

<h:form>
        <h:selectOneMenu value="#{beanCountries.ValueSelected}">
           <f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
           <f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
        </h:selectOneMenu>

        <h:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}">
               <f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
               <f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array 
         </h:selectOneMenu>
         <h:commandButton value="Submit" action="beanCountries.listCities()"/>
</h:form>

这种方式是使用一个按钮(在 jsf 中),并且 action 属性将执行一个 java 方法,然后刷新页面。这也可以通过 selectOneMenu 组件的“ValueChangeListener”属性实现

【讨论】:

  • 为了避免重大误解:仅使用标准 JSF 组件也可以做到这一点。 OP 没有提到他已经在使用 PrimeFaces 的任何地方。另请参阅问题评论中的链接。
  • 根据您的编辑:您到底是如何完全错过&lt;f:ajax&gt; 的?为什么您忽略了阅读 JSF 2.x 书籍/教程/资源以及问题评论中的链接?
  • 你是对的 BalusC,f:ajax 将是他想要的解决方案。
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
相关资源
最近更新 更多