0

我下面的代码不起作用,我正在使用 primefaces 选择列表和 postconstruct 注释来使用 try catch 块来初始化方法。但是我的picklistbean是空的,我尝试了所有方法让它工作,但没有一个工作。谁能为我提供选择列表的工作示例,或者在我的代码中我遗漏了什么?我被这个问题困扰了很长时间,如果有人帮助我,我会很高兴。

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);
}
}
4

1 回答 1

0

在 PostContruct 中,您的 dao 类不会注入您的 bean。当您想要初始化 bean 中的某些内容时,请尝试使用 preRenderView。

也不要tanidao = new TrvrTaniDAO();在你的 bean 中使用。TaniDao 应该在春天用Autowired.

PreRenderView 示例

编辑

此外,如果您应该使用 jsf 托管属性注入 TrvrTaniDAO。

@ManagedProperty(value = "#{TrvrTaniDAO}")
private TrvrTaniDAO tanidao;
于 2014-04-21T08:25:44.697 回答