Send a List<Entity> on Redirect

Asked

Viewed 127 times

1

I have a list and I need to send it to another page to fill a table, when the redirect ends the page loads only the table does not fill because the list has been emptied, as I keep it with the information?

Minhabean

@ManagedBean(name = "lotesEnvBean")
@ViewScoped
public class LoteEnviadoBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private LoteEnvRepository envRepository;

    private List<LoteEnvEntity> lotesEnviados;
    private List<LoteEnvEntity> lotesSelecionados = new ArrayList<LoteEnvEntity>();
    private List<LoteEnvDetalheEntity> lotesEnvDetalhe;
    private Collection<Object> selecao;

    private Date dataDe;
    private Date dataAte;
    private Flash flash;

    private UIComponent component;

    @PostConstruct
    public void init() {
        this.lotesEnviados = envRepository.findall();
    }

    public void selectionListener(AjaxBehaviorEvent event) {
        AbstractExtendedDataTable dataTable = (AbstractExtendedDataTable) event
            .getComponent();
        Object originalKey = dataTable.getRowKey();
        lotesSelecionados.clear();
        for (Object selectionKey : selecao) {
            dataTable.setRowKey(selectionKey);
            if (dataTable.isRowAvailable()) {
                lotesSelecionados.add((LoteEnvEntity) dataTable.getRowData());
            }
        }
        dataTable.setRowKey(originalKey);
    }

    public Collection<Object> getSelecao() {
        return selecao;
    }

    public void setSelecao(Collection<Object> selecao) {
        this.selecao = selecao;
    }

    public void setSelectionItems(List<LoteEnvEntity> selectionItems) {
        this.lotesSelecionados = selectionItems;
    }

    public List<LoteEnvEntity> getLotesEnviados() {
        return lotesEnviados;
    }

    public void setLotesEnviados(List<LoteEnvEntity> lotesEnviados) {
        this.lotesEnviados = lotesEnviados;
    }

    public List<LoteEnvEntity> getLotesSelecionados() {
        return lotesSelecionados;
    }

    public Date getDataDe() {
        return dataDe;
    }

    public void setDataDe(Date dataDe) {
        this.dataDe = dataDe;
    }

    public Date getDataAte() {
        return dataAte;
    }

    public void setDataAte(Date dataAte) {
        this.dataAte = dataAte;
    }

    public void setLotesSelecionados(List<LoteEnvEntity> lotesSelecionados) {
        this.lotesSelecionados = lotesSelecionados;
    }

    public List<LoteEnvDetalheEntity> getLotesEnvDetalhe() {
        return lotesEnvDetalhe;
    }

    public void setLotesEnvDetalhe(List<LoteEnvDetalheEntity> lotesEnvDetalhe) {
        this.lotesEnvDetalhe = lotesEnvDetalhe;
    }

    public UIComponent getComponent() {
        return component;
    }

    public void setComponent(UIComponent component) {
        this.component = component;
    }

    public Flash getFlash() {
        return flash;
    }

    public void setFlash(Flash flash) {
        this.flash = flash;
    }

    public List<LoteEnvEntity> findByDate() {
        this.lotesEnviados = envRepository.findallByDate(this.dataDe,
            this.dataAte);
        return this.lotesEnviados;
    }

    public String exibirLotesSelecionados() {

        FacesContext context = FacesContext.getCurrentInstance();
        System.out.println("#########################################################----" + this.lotesSelecionados.size());
        if(this.lotesSelecionados.size() <= 0) {
            context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro","Deve-se selecionar ao menos um lote."));
            return "";
        }

        this.lotesEnvDetalhe = this.envRepository.exibirLotesSelecionados(this.lotesSelecionados);


        if (this.lotesEnvDetalhe.size() > 0)
            return "lotesEnviadosListagem.xhtml?faces-redirect=true";
        else
            context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro","Não foi possível exibir o(s) lote(s)."));

            return "";
        }
    }
}
  • I started working with Java EE this week, before that I had little contact with web development, and I’m having to jump in here. In this case by using Flashcontet then?

  • Where’s your bean Managed?? Just put the gets and sets on it and refer it to the page

  • You can use the FacesContext.getInstance().getExternalContext().getFlash() to access the Flash Scope. Access both before redirect to send the list, and on PostConstruct to retrieve it.

  • @Wakim thanks! would have an XD example?

1 answer

2


The Flash Scope is often used when one wants to keep data accessible only until the next request. What is advantageous if, at the end of an action of a Managed Bean, you redirect to another page and need to pass data to that new page.

During the action of Bean, before browsing occurs, you can store data in the Flash Scope and retrieve them on the next request, the one that will actually render the contents of the View that you redirected.

The data saved on Flash Scope will only last until the next request, after this will be deleted. This is because the storage is done in the session and then removed at the end of the request subsequent.

Practical example:

public String exibirLotesSelecionados() {

    FacesContext context = FacesContext.getCurrentInstance();

    if(this.lotesSelecionados.size() <= 0) {
        context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro","Deve-se selecionar ao menos um lote."));

        return "";
    }

    this.lotesEnvDetalhe = this.envRepository.exibirLotesSelecionados(this.lotesSelecionados);


    if (this.lotesEnvDetalhe.size() > 0) {
         // Adiciono a lista no Flash Scope para o proximo Bean recuperar
        context.getExternalContext().getFlash().put("lista", lotesSelecionados);
        return "lotesEnviadosListagem.xhtml?faces-redirect=true";
    } else {
        context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro","Não foi possível exibir o(s) lote(s)."));
    }

    return "";
}

In the PostConstruct of Managed Bean of lotesEnviadosListagem.xhtml

@PostConstruct
public void init() {
    List<LoteEnvDetalheEntity> lotesEnviados = (List<LoteEnvDetalheEntity>) FacesContext.getInstance().getExternalContext().getFlash().get("lista");

    // Guardar a lista para usar no dataTable
}

More details in the documentation of Flash

Browser other questions tagged

You are not signed in. Login or sign up in order to post.