如何在托管bean上获取变量?

时间:2013-05-23 17:23:24

标签: jsf managed-bean

我正在尝试创建一个登录屏幕,所以我得到了banbanNumber并从用户传递并在数据库中检查它。这是有效的,但是当我尝试在另一个页面上使用`doldur()来获取有关数据库的一些信息时,它会返回null。

如何解决这种情况以及为什么ibanNumber会返回null?

不是:当点击“KullaniciİslemMenusu.xhtml”页面上的“hesapişlemleri”按钮然后打开“hesapişlemleri.xhtml”并填写dataTable

时添加jsf页面
@ManagedBean(name = "login")

public class LoginBean {

public LoginBean() {

}
private String ıban_number;
private String sifre;
private String paraCek;
private String paraYatir;


public String getParaCek() {
    return paraCek;
}

public void setParaCek(String paraCek) {
    this.paraCek = paraCek;
}

public String getParaYatir() {
    return paraYatir;
}

public void setParaYatir(String paraYatir) {
    this.paraYatir = paraYatir;
}

public String getIban_number() {
    return ıban_number;
}

public void setIban_number(String ıban_number) {
    this.ıban_number = ıban_number;
}

public String getSifre() {
    return sifre;
}

public void setSifre(String sifre) {
    this.sifre = sifre;
}
db_connection dbLogin = new db_connection();
private PreparedStatement psLogin;
private ResultSet rsLogin;

public String getLogin() {


    try {

        System.out.println(getIban_number() + " " + getSifre());
        psLogin = dbLogin.getCon().prepareStatement("select * from kullanici_giris where ibanNumber=? and sifre=?");
        psLogin.setString(1, getIban_number());
        psLogin.setString(2, getSifre());
        System.out.println("Veri tabanına ulaşıldı");
        rsLogin = psLogin.executeQuery();

        while (rsLogin.next()) {
            return "true";

        }

    } catch (SQLException ex) {
        Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
    }

    return "false";
}

public List<getAccountInfo> doldur() {

    System.out.println(getIban_number());
    List<getAccountInfo> liste = new ArrayList<getAccountInfo>();
    try {
        psLogin = dbLogin.getCon().prepareStatement("SELECT * FROM hesap_bilgi where ibanNumber = ? ");
        psLogin.setString(1, getIban_number());
        rsLogin = psLogin.executeQuery();


        while (rsLogin.next()) {
            getAccountInfo obj = new getAccountInfo();

            obj.setIbanNumber(rsLogin.getString(1));
            obj.setToplamButce(rsLogin.getString(2));
            obj.setHarcananButce(rsLogin.getString(3));
            obj.setKalanButce(rsLogin.getString(4));


            liste.add(obj);


        }
    } catch (SQLException ex) {
    }


    return liste;
}
}




    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Kullanici İslem Menusu</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid columns="1">

                <h:commandButton value="Hesap İşlemleri" action="hesap_islemleri"/>
                <h:commandButton value="Kişisel İşlemleri" action="kisisel_islemler"/>
                <h:commandButton value="Şubelerimiz" action="subelerimiz"/>
            </h:panelGrid>

        </h:form>
    </h:body>
</html>



<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>


            <h:dataTable value="#{login.doldur()}" var="c">
                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="ıbanNumber"/>

                    </f:facet>
                    <h:outputLabel value="#{c.ibanNumber}"/>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="ToplamButce"/>
                    </f:facet>
                    <h:outputLabel value="#{c.toplamButce}"/>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="HarcananButce"/>
                    </f:facet>
                    <h:outputLabel value="#{c.harcananButce}"/>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="KalanButce"/>
                    </f:facet>
                    <h:outputLabel value="#{c.kalanButce}"/>
                </h:column>
            </h:dataTable>

        </h:form>
    </h:body>
</html>

2 个答案:

答案 0 :(得分:0)

您的iBanNumber从用户传递并存储在运行第一页时初始化的bean实例中。当您从另一个页面调用它时,会为您的托管bean创建一个新实例。也许这就是为什么,第一次在bean中设置的值第二次不再可用。尝试将托管bean的范围更改为SessionScoped。这样,bean的值将在整个会话期间保留。您可以阅读有关JSF会话here的更多信息。

答案 1 :(得分:0)

你获得房产的方式很奇怪。

例如,如果您有托管bean(name =“mBean”)和属性“String property”;你有getter和setter“getProperty”和“setProperty”,好吗?

因此,当您通过Expession语言从xhtml页面调用它时,您将使用,例如:

<h:outputText value="#{mBean.property}" />

所以,

1 - 当你使用doldur()时它是否真正起作用于dataTable?难道你不需要像“收集doldur”这样的财产以及它的吸气剂和制定者“getDoldur”和“setDoldur”吗?

2 - 同样的思维方式;你不必声明“StringıbanNumber”,然后你有getter和setter“getIbanNumber”和“setIbanNumber”然后

<h:outputLabel value="#{c.ibanNumber}"/> 

先检查这些事情!