【发布时间】:2017-03-03 05:10:18
【问题描述】:
我正在使用 JSF 和 PrimeFaces。 在尝试使用 PrimeFaces 组件 p:selectOneMenu 时,我发现了一些问题。
<h:form>
<p:panel header="Select an offer and the date for the report" style="margin-bottom: 10px">
<h:panelGrid columns="2" cellpadding="5px">
<p:outputLabel for="offer" value="Offer: "/>
<p:selectOneMenu id="offer" value="#{partnerSales.selectedOffer}" style="width: 200px;">
<p:ajax event="change" update="year" listener="#{partnerSales.onOfferChange}"/>
<f:selectItem itemLabel="Select an offer: " noSelectionOption="true" itemValue=""/>
<f:selectItems value="#{partnerSales.allOffers}"/>
</p:selectOneMenu>
<p:outputLabel for="year" value="Year: "/>
<p:selectOneMenu id="year" value="#{partnerSales.selectedYear}" style="width: 200px">
<p:ajax update="month"/>
<f:selectItem itemLabel="Select a year: " itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{partnerSales.years}"/>
</p:selectOneMenu>
<p:outputLabel for="month" value="Month: "/>
<p:selectOneMenu id="month" value="#{partnerSales.selectedMonth}" style="width: 200px">
<f:selectItem itemLabel="Select a month: " itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{partnerSales.months}"/>
</p:selectOneMenu>
</h:panelGrid>
<p:separator/>
<p:commandButton value="Send" actionListener="#{partnerSales.displayData}"/>
</p:panel>
</h:form>
所以,我有一个 selectOneMenu 组件,它有几个选项。默认情况下,第一项是“选择报价:”,其值为空,不用于选择。此外,我使用侦听器来捕获值变化以更新下一个组件。但是,当我尝试更改值时,什么也没有发生。但是当我从下拉菜单中选择第一项时,侦听器会打印出它被访问过。错误在哪里?我无法理解这种奇怪的行为)
【问题讨论】:
-
如果您希望直接处理该值,则需要在 ajax 事件中添加一个 process="@this",否则应使用 commandButton 提交。如果您想使用 commandButton 提交,您的代码是正确的,如果您使用普通数据类型作为值。但也许您尝试将 SelectOneMenu 与自定义对象一起使用。那是行不通的,那么你需要一个转换器。您能向我们展示您的 bean 吗?您是否收到任何错误消息?
-
@lastresort 是的,我想使用命令按钮。你是对的,我使用自定义对象。所以,你的回答是对的,我已经添加了转换器,这是有效的!非常感谢!
标签: java ajax primefaces