<tr>
<td><h2><h:outputText value="Card:" /></h2></td>
<td>
<h:selectOneMenu required="true" requiredMessage="Card not selected" value="#{myBean.card}">
<f:selectItem itemValue="#{null}" itemLabel="Please select an Card..." />
<f:selectItems value="#{myBean.cards}" var="card" itemLabel="#{card.number} - #{card.currencyCode}#{card.availableBalance}" />
<f:ajax render="@form" listener="#{myBean.selectCard}" />
</h:selectOneMenu>
</td>
</tr>
<tr>
<td><h2><h:outputText rendered="#{(myBean.card != null)}" value="Item: " /></h2></td>
<td>
<h:selectOneMenu rendered="#{(myBean.card != null)}" required="true" requiredMessage="Item not selected" value="#{myBean.selectedItem}">
<f:selectItem itemValue="#{null}" itemLabel="Please select a Item..." />
<f:selectItems value="#{myBean.items}" var="item" itemValue="#{item}" itemLabel="#{item.name}" />
<f:ajax render="@form" listener="#{myBean.selectItem}" />
</h:selectOneMenu>
</td>
</tr>
<tr>
<td><h2><h:outputText rendered="#{(myBean.selectedItem != null)}" value="Service ID: " /></h2></td>
<td><h:outputFormat rendered="#{(myBean.selectedItem != null)}" value="#{myBean.selectedItem.serviceId}" /></td>
</tr>
当我更改项目下拉列表的值时,它仍会显示默认项目。 这是bean,但Card下拉确实可以正常工作。 但它会重新渲染页面并显示所有应该在选择项目后才出现的项目。 我在googl上到处寻找,没有运气。
@ManagedBean(name = "myBean")
@RequestScoped
public class MyBean {
private Item selectedItem;
private Card card;
public Item getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(Item selectedItem) {
this.selectedItem = selectedItem;
}
public Card getCard() {
return card;
}
public void setCard(Account card) {
this.card = card;
}
/*More code*/
}
这是转换器类。卡片下拉列表具有类似的转换器,并且可以按预期工作,但由于某种原因,物品掉落不起作用。
FacesConverter(forClass=Item.class)
public class ItemConverter implements Converter {
@Override
public Object getAsObject(
final FacesContext context,
final UIComponent component,
final String value) {
BillPaymentTransaction transaction = context.getApplication().evaluateExpressionGet(context, "#{itemChooser}", BillPaymentTransaction.class);
for (Item item : transaction.getItems()) {
if (String.valueOf(item.getName()).equals(value)) {
return item;
}
}
throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Item", value)));
}
@Override
public String getAsString(
final FacesContext context,
final UIComponent component,
final Object value) {
return ((Item)value).getName();
}
}