【问题标题】:How to Compare the values JSF selectonemenu如何比较值 JSF selectonemenu
【发布时间】:2016-12-26 15:43:57
【问题描述】:

我正在开发一个 JSF 应用程序 我有 2 个 selectOnemenu 控件和提交按钮。 如果两个字段的值相等,我需要禁用按钮

 <h:selectOneMenu id="from" value="#{currencyBean.history.fromCurrency}" >
    <f:selectItems value="#{currencyBean.currency}"   var="c" itemValue="#{c}" itemLabel="#{c.name}"/>
</h:selectOneMenu>
<p:outputLabel for="to" value="to:" />
<h:selectOneMenu id="to" value="#{currencyBean.history.toCurrency}" >
    <f:selectItems value="#{currencyBean.currency}" var="c" itemValue="#{c}" itemLabel="#{c.name}"/>
</h:selectOneMenu>
<p:commandButton id="calculateButton"  value="Convert" update=":convert :historyPanel" onstart="PF('statusDialog').show()" onsuccess="PF('statusDialog').hide()" validateClient="true" ajax="true"  action="#{currencyBean.Calculate()}" />

我尝试将 onchange 与 ajax 一起使用,但每次我更改一个下拉列表时,第二个下拉列表的值在 backbean 中变为空,因此我无法读取它。

这是我的豆豆

@Named(value = "currencyBean")
@RequestScoped
public class CurrencyBean implements Serializable {

    /**
     * Creates a new instance of CurrencyBean
     */
    private History history;
    private Currency currency;
    private Date currentDate;
    @Inject
    private Loginbean loginBean;

    private List<History> historyList;

    public List<History> getHistoryList() {
        int userId = loginBean.getUserId();
        if (userId != -1) {

            return new HistoryHelper().GetHistoryListByUser(userId);
        }
        return null;
    }

    public CurrencyBean() {
        history = new History();
    }

    public History getHistory() {
        return history;
    }

    public void setHistory(History history) {
        this.history = history;
    }

    public Currency[] getCurrency() {
        return Currency.values();
    }

    public Date getCurrentDate() {
        return new Date();
    }

    public void Calculate() {
        int userId = loginBean.getUserId();
        if (userId != -1) {
            new CurrencyClient().Convert(userId, this.history);
        }

    }

}

有什么线索吗?

【问题讨论】:

  • 你能不能更精确一点,并准确输入你是如何实现 onchange 方法的?以及您的支持 bean 的外观,尤其是它的范围
  • 我已经更新了我的问题以添加我的 backbean。关于 onchange 实现,我只是尝试在日志中写入 from 和 to 两个字段的值,但是如果我从字段更新,则 to 值为 null,如果我更新到字段,则 from 值为 null
  • 这个 onchange 实现在哪里?在 selectOneMenu 中? onchange 属性中叫什么

标签: jsf primefaces selectonemenu


【解决方案1】:

我的假设是您的所有问题都来自您的托管 bean 范围。您有 @Request 范围,因此您的托管 bean 的每个请求都将从容器中删除,因此当您定义 onchange="submit()" 时(这只是我的假设,因为您尚未定义如何实现 onchange 属性)并且您从一个 selectBox 中选择值此组件的组件值已更新,但第一个仍为空。当您选择从第一个 selectBox 更新的第二个 selectBox 值时,不再存在,因为在第一次请求后已删除托管 bean。您应该尝试更广泛的范围,例如 @ViewScope。如果没有帮助,则需要更多信息,如实现 onchange 属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多