0

My code below doesn't work, I'm using primefaces picklist and postconstruct annotation to init method with try catch block. However my picklistbean is empty, I tried all the ways to make it work but none of them worked. Can anyone provide me working example for picklist, or in my code am I missing something ? I'm stuck to this problem for so long, I'll be glad if someone helps me.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;


@ManagedBean(name = "pickListBeanTani")
@ViewScoped
public class PickListBeanTani implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private DualListModel<TrvrTani> tanis;

@ManagedProperty(value = "#{TrvrTaniDAO}")
private TrvrTaniDAO tanidao;

public TrvrTaniDAO getTanidao() {
    return tanidao;
}


public void setTanidao(TrvrTaniDAO tanidao) {
    this.tanidao = tanidao;
}


private List<TrvrTani> sourcetani;
private List<TrvrTani> targettani;

@PostConstruct
public void init(){

    try {
        sourcetani = new ArrayList<TrvrTani>();
        targettani = new ArrayList<TrvrTani>();

        tanidao = new TrvrTaniDAO();
        List<TrvrTani> taniList = tanidao.findAll();
        System.out.println("tanılist" +taniList);
        for (TrvrTani tani : taniList) {
            sourcetani.add(new TrvrTani(tani.getTaniid(), tani.getTaniadi(), tani
                    .getTanikodu()));
        }

        tanis = new DualListModel<TrvrTani>(sourcetani, targettani);

    } catch (Exception e) {
        throw e;
    }

}


public List<TrvrTani> getSourcetani() {
    return sourcetani;
}

public void setSourcetani(List<TrvrTani> sourcetani) {
    this.sourcetani = sourcetani;
}

public List<TrvrTani> getTargettani() {
    return targettani;
}

public void setTargettani(List<TrvrTani> targettani) {
    this.targettani = targettani;
}

public DualListModel<TrvrTani> getTanis() {
    return tanis;
}

public void setTanis(DualListModel<TrvrTani> tanis) {
    this.tanis = tanis;
}


public void onTransferTani(TransferEvent event) {
    StringBuilder builder = new StringBuilder();
    for (Object item : event.getItems()) {
        builder.append(((TrvrTani) item).getTaniadi()).append("<br />");

        int tanisize = tanis.getTarget().size();
        System.out.println(" ************target*************  : "
                + tanis.getTarget().size());
        for (int h = 0; h < tanisize; h++) {

            /* elemanin adi, id si ve kodu */
            String taniadi = tanis.getTarget().get(h).getTaniadi();
            System.out.println(" ************taniadi1*************  : "
                    + taniadi);
            Long taniidp = tanis.getTarget().get(h).getTaniid();
            System.out.println(" ************taniid2*************  : "
                    + taniidp);
            String tanikodu = tanis.getTarget().get(h).getTanikodu();
            System.out.println(" ************tanikodu3*************  : "
                    + tanikodu);
        }

    }

    FacesMessage msgtani = new FacesMessage();
    msgtani.setSeverity(FacesMessage.SEVERITY_INFO);
    msgtani.setSummary("Tanı Eklendi");
    msgtani.setDetail(builder.toString());

    FacesContext.getCurrentInstance().addMessage(null, msgtani);
}
}
Burak
  • 13
  • 1
  • 3
  • 11

1 Answers1

0

In PostContruct,your dao class doesn't inject in your bean.Try preRenderView when you want to initialize somethings in your bean.

Also dont use tanidao = new TrvrTaniDAO(); in your bean.TaniDao should be injected by spring with Autowired.

PreRenderView Example

EDIT

Also if you should inject TrvrTaniDAO with jsf managed property.

@ManagedProperty(value = "#{TrvrTaniDAO}")
private TrvrTaniDAO tanidao;
mstzn
  • 2,881
  • 3
  • 25
  • 37
  • After removing tanidao = new TrvrTaniDAO(); now I'm getting nullpointerexception for taniList – Burak Apr 21 '14 at 08:49
  • It is normal because of TaniDao doesnt be injected by spring.Your AutoWired annotation doesnt set value of TaniDao. How do you integrate jsf and spring ? And did you try preRender.preRender will work if AutoWired set value of TaniDao. – mstzn Apr 21 '14 at 09:19
  • I wrote below to another bean but I get error saying HTTP Status 500 - Unable to set property pickListBeanTani for managed bean userOS `@ManagedProperty("#{pickListBeanTani}") private PickListBeanTani pickListBeanTani; public void setPickListBeanTani(PickListBeanTani pickListBeanTani) { this.pickListBeanTani = pickListBeanTani; }` – Burak Apr 21 '14 at 11:05
  • Burak senin jsf ile spring contextlerini haberleştiğinden emin misin? Çünkü muhtemelen ikisi entegre edilmemiş. – mstzn Apr 21 '14 at 11:08
  • applicationcontext.xml dosyasında beanleri tanımladım ayrıca faces-config.xml dosyasına org.springframework.web.jsf.el.SpringBeanFacesELResolver yazdım Şu an tek sıkıntı managedproperty ile inject edememem – Burak Apr 21 '14 at 11:14
  • web.xml de gerekli tanımlamalar(ContextLoaderListener, RequestContextListener) eklenmiş mi ? Bir de şurdaki ornekte kendi konfigurasyonlarını kontrol edebilir misin ? – mstzn Apr 21 '14 at 12:42
  • ' org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener ' evet eklemişim – Burak Apr 21 '14 at 12:51
  • Problemi anladım sanırım.Spring autowired yerine jsf managed property ile inject etmeyi dener misin ? Cevabımı guncelledim.Ayrıca projeyi ayağakaldırırken setTanidao methoduna breakpoint koyabilir misin? – mstzn Apr 21 '14 at 12:56
  • tanıdao null gelmiyor code u editledim fakat yine boş geliyor picklist tanidaoya managedbean ve viewscoped ekledim Ilk başta tanılist doluyor kodda fakat ikincisinde catche düşüyor – Burak Apr 21 '14 at 13:36
  • ViewScope chicken-egg dediğimiz bir durum var.Bu nedenle bir kere create edilmesi gerekirken birden fazla create edilebiliyor.Onun için her sferinden postConstructa düşüyor olabilir.PostConstruct methodundan if (!FacesContext.getCurrentInstance().isPostback()) { } şekliden postback yapılıp yapılmadığını kontrol etmen gerekiyor.Try-catch'i bu if'in içine koyarsan her seferinde buraya girmeyecektir. – mstzn Apr 21 '14 at 13:49
  • herhangi bir scope tanımlamadığımda picklist dolu geliyor – Burak Apr 21 '14 at 14:51
  • Teşekkürler yardımlarınız için maklesef bir sonuca ulaşamadı – Burak Apr 21 '14 at 15:36
  • I will be glad If someone has opinion about my problem – Burak Apr 21 '14 at 15:37
  • Hocam belirttiğim gibi viewScopedan kaynaklanan bir durum.Siz scope belirtmeyince default scope Request oluyor.Bu nedenle postConstruct methoduna bir kere giriyor ve liste doluyor.Ama viewScope'da birden fazla postConstruct methoduna giriyor.Önceki yorumumda belirttiğim isPostback kontrolu yaparsanı sayfanı duzgun çalışır. – mstzn Apr 21 '14 at 18:18
  • o şekilde yaptığım zaman unsupportedoperationexception alıyorum – Burak Apr 22 '14 at 06:44
  • English dude english. – Sarz Feb 16 '15 at 07:22