【问题标题】:Primefaces Picklist is always empty when using Postconstruct使用 Postconstruct 时,Primefaces Picklist 始终为空
【发布时间】:2014-04-21 07:25:11
【问题描述】:

我下面的代码不起作用,我正在使用 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);
}
}

【问题讨论】:

    标签: jsf jsf-2 primefaces postconstruct picklist


    【解决方案1】:

    在 PostContruct 中,你的 dao 类没有注入你的 bean。当你想在你的 bean 中初始化某些东西时,试试 preRenderView。

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

    PreRenderView Example

    编辑

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

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

    【讨论】:

    • 删除 tanidao = new TrvrTaniDAO();现在我得到了 taniList 的 nullpointerexception
    • 这是正常的,因为TaniDao没有被spring注入。你的AutoWired注解没有设置TaniDao的值。你如何集成 jsf 和 spring ?如果 AutoWired 设置了 TaniDao 的值,您是否尝试过 preRender.preRender。
    • 我在下面写到另一个 bean 但我收到错误说 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 senin jsfile spring contextlerini haberleştiğinden emin misin? Çünkü muhtemelen ikisi entegre edilmemiş。
    • 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 injection edememem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2017-11-21
    • 2012-05-11
    • 2018-12-24
    • 2011-10-26
    • 2016-12-11
    相关资源
    最近更新 更多