@Stateless Bean Session Acts Like @Singleton Bean

Try the EJB Beans session example. I want to see their differences. Below is a diagram of the main projects;

http://img109.imageshack.us/img109/8262/85220418.png

A project is like a shopping cart. When I call a managed bean to get the result of adding Urun objects via a nested EJB, the result list should be the last Urun object. Reason for my EJB @Stateless bean. But when I launch the application, it saves all newly added Urun objects. But he has no status, he must keep the last object of Urun in every action. And then, when I open the application page in another web browser, it lists the list of added Urun objects. But managed by bean and Ejb has no analogues in my model. But it acts like a Singleton bean. Where is my problem?

@Stateless
public class AlisverisSepetiEJB {

    List<Urun> urunler=new ArrayList<>();

    public List<Urun> getUrunler() {
        return urunler;
    }

    public void setUrunler(List<Urun> urunler) {
        this.urunler = urunler;
    }

    public void urunEkle(Urun urun){
        urunler.add(urun);
    }

}

@ManagedBean(name="bean")
@RequestScoped
public class JSFYonetimliNesne {

    public JSFYonetimliNesne(){
        System.out.println("Yönetimli nesne çalıştı");
    }

   @EJB
   AlisverisSepetiEJB alisverisSepeti;

   Urun urun=new Urun();
   List<Urun> urunler;


    public List<Urun> getUrunler() {
        return alisverisSepeti.getUrunler();
    }

    public void setUrunler(List<Urun> urunler) {
        this.urunler = urunler;
    }

    public Urun getUrun() {
        return urun;
    }

    public void setUrun(Urun urun) {
        this.urun = urun;
    }

    public void sepeteKoy(){
        alisverisSepeti.urunEkle(urun);
        urun=new Urun();
    }

}

public class Urun {

   String urunAdi;
   Long fiyat;
   Long gramaj;

    public Long getFiyat() {
        return fiyat;
    }

    public void setFiyat(Long fiyat) {
        this.fiyat = fiyat;
    }

    public Long getGramaj() {
        return gramaj;
    }

    public void setGramaj(Long gramaj) {
        this.gramaj = gramaj;
    }

    public String getUrunAdi() {
        return urunAdi;
    }

    public void setUrunAdi(String urunAdi) {
        this.urunAdi = urunAdi;
    }

}

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html>
<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>Alışveriş Sepeti</title>
    </h:head>
    <h:body>
        Alışveriş Sepeti
        <h:form>

            <h:panelGrid columns="2">
            <h:outputLabel value="Ürün adı : "/>
            <h:inputText value="#{bean.urun.urunAdi}"/>
            <h:outputLabel value="Ürün fiyatı : "/>
            <h:inputText value="#{bean.urun.fiyat}"/>
            <h:outputLabel value="ÜRün gramajı : "/>
            <h:inputText value="#{bean.urun.gramaj}"/>
            </h:panelGrid>

            <h:commandButton action="#{bean.sepeteKoy}" value="Sepete Ekle"/>

            <br><h:outputLabel value="Sepetteki Ürünler"/></br>


            <h:dataTable value="#{bean.urunler}" var="item" border="1">

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün adı"/>
                </f:facet>
            <h:outputText value="#{item.urunAdi}"/>
            </h:column>

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün fiyatı"/>
                </f:facet>
            <h:outputText value="#{item.fiyat}"/>
            </h:column>

            <h:column >
                <f:facet name="header">
                    <h:outputLabel value="Ürün gramajı"/>
                </f:facet>
            <h:outputText value="#{item.gramaj}"/>
            </h:column>

            </h:dataTable>

        </h:form>

    </h:body>
</html>
+3
source share
1 answer

, AlisverisSepetiEJB ( , ). - EJB @Stateless !

List<Urun> urunler = new ArrayList<>(); , bean , - , ; - , , EJB. , @Stateless EJB, , / .

, (, Session -) EJB @Stateful.

+5

All Articles