【问题标题】:How to pass the selected value of dropdown list from xhtml page to jsf bean?如何将下拉列表的选定值从xhtml页面传递给jsf bean?
【发布时间】:2013-07-04 09:01:33
【问题描述】:

我的 Wep 页面中有一个下拉列表,它是:

 <h:form>
        <h:panelGrid columns="2">
            <h:outputText value="Açılacak hesabın para birimi:"></h:outputText>
            <h:selectOneMenu value="currency" >
                <f:selectItem itemValue="choose" itemLabel="Seçiniz..." />
                <f:selectItem itemValue="tl" itemLabel="Türk Lirası(TL)" />
                <f:selectItem itemValue="usd" itemLabel="Amerikan Doları(USD)" />
                <f:selectItem itemValue="euro" itemLabel="Euro" />
            </h:selectOneMenu>

            <h:outputText value="Açılacak hesabın cinsi:"></h:outputText>

            <h:selectOneMenu value="vade" >
                <f:selectItem itemValue="choose" itemLabel="Seçiniz..." />
                <f:selectItem itemValue="vadesiz" itemLabel="Vadesiz mevduat hesabı" />
                <f:selectItem itemValue="vadeli" itemLabel="Vadeli Mevduat Hesabı" />
            </h:selectOneMenu>
            <h:commandButton value="Onayla" action="#{events.createAccount}" ></h:commandButton>
        </h:panelGrid>           
    </h:form>

然后通过单击按钮,我将转到 Events.java bean 并在那里处理一些信息。但我需要函数 createAccount() 中这些下拉列表的值。这是我的事件 bean

@Named(value = "events")
@Dependent
public class Events {

/**
 * Creates a new instance of Events
 */
public Events() {
}

public void createAccount(){

}
}

我该怎么做?

谢谢

【问题讨论】:

    标签: java jsf jakarta-ee javabeans


    【解决方案1】:

    这是一个非常基本的 JSF 点,在你的 selectOneMenu 组件中,在你的托管 bean 中定义一个值:

    <h:selectOneMenu value="#{events.currency}" >
          <f:selectItem itemValue="choose" itemLabel="Seçiniz..." />
          <f:selectItem itemValue="tl" itemLabel="Türk Lirası(TL)" />
          <f:selectItem itemValue="usd" itemLabel="Amerikan Doları(USD)" />
          <f:selectItem itemValue="euro" itemLabel="Euro" />
    </h:selectOneMenu>
    

    现在在您的托管 bean 中,您只需定义一个属性 currency

    private String currency;
    
    public String getCurrency() {
        return currency;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    

    现在在您的 Events 托管 bean 的方法 createAccount 中,您只需使用定义的 currency 值。
    更多示例和教程请参考这里:https://stackoverflow.com/tags/jsf/info

    【讨论】:

    • 谢谢,但有一个小问题:在托管 bean 中,如果我将字符串变量“currency”静态化,那么它可以工作,但如果没有静态变量,它就无法工作。为什么会这样?当我调试时,我看到如果在获取或设置后它不是静态的,那么每次它也会转到“私人字符串货币”行;即使它已设置,现在它也变为空。为什么会这样?
    • 这是范围的问题,请阅读 JSF 中的范围。要暂时解决此问题并查看它是否有效,请将您的托管 bean 范围设置为“会话”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多