Handle two lists of a Drag and Drop component on different ManagedBeans

2

I'm using the Drag and Drop DataTable component. Contextualizing is as follows:

I have a registration based on steps. Where in the first step I enter the information of an Edital X, which is manipulated by the editalBean, already in the second step I have the Drag and Drop component with the Disciplines to be chosen, so far so good. If I do not even suggest the documentation I can make it work fine. But here comes the problem: The method of my disciplinesBean needs to be passed a center (entity) as a parameter and this center is in the editalBean. My solution was to put the list of selected disciplines in the Bean disciplines and the complete list of disciplines in the editalBean (which is where I can pass the parameter).

The method that handles these two lists is this (when they are in the same bean):

public void onDisciplinaDrop(DragDropEvent ddEvent) {
        Disciplina disciplina = ((Disciplina) ddEvent.getData());

        listaSelecionadoss.add(disciplina);
        listaCompleta.remove(disciplina);
    }

Then the discipline is added to one table and removed from another. But as I put the full list of disciplines on another bean, I do not know how to remove it.

My method onDisciplinaDrop is in the Beanclass and myComplete list is in the editalBean. How can I do to make the correct removal?

My codes:

EditalBean.java

@ManagedBean(name="editalBean")
@SessionScoped
public class EditalBean {

    private Edital edital;
    private EditalDAO editalDAO = new EditalDAO();
    private Centro centro;
    private CentroDAO centroDAO = new CentroDAO();
    private DisciplinaDAO disciplinaDAO = new DisciplinaDAO();

    private List<Disciplina> listaPorCentro;

    public List<Disciplina> getListaPorCentro() {
        listaPorCentro = disciplinaDAO.getListaDisciplinaPorCentro(edital.getAno(), edital.getPeriodo(), centro);
        return listaPorCentro;
    }

    public void setListaPorCentro(List<Disciplina> listaPorCentro) {
        this.listaPorCentro = listaPorCentro;
    }

DisciplineBean.java

@ManagedBean(name="disciplinaBean")
@SessionScoped
public class DisciplinaBean {

    private Disciplina disciplina;
    private Disciplina disciplinaSelecionada;
    private List<Disciplina> droppedDisciplinas = new ArrayList<Disciplina>();
    private DisciplinaDAO disciplinaDAO = new DisciplinaDAO();

    public void onDisciplinaDrop(DragDropEvent ddEvent) {
        if(droppedDisciplinas != null){
            droppedDisciplinas.add(d);
        }else {
            System.out.println("droppedDisciplinas é nulo.");
        }
    }

If you need more details try to explain better. Thank you.

    
asked by anonymous 29.01.2015 / 03:41

1 answer

1

Then your difficulty is to access in a Bean the objects that were instantiated in another Bean

In order to serve user requests on features that are so interconnected on the same page, it is okay to use the same bean and recommend that you do so, so you eliminate the need to inject objects from one bean into another, which generates a rather weird code.

However, if you want to use two beans, one way to inject the instance of a bean into another bean is:

@ManagedBean
@SessionScoped
public class DisciplinaBean {

    //...

    @ManagedProperty(value="#{editalBean}")
    private EditalBean editalBean;

    // É esquisito, mas este set é necessário
    public void setEditalBean(EditalBean editalBean) {
        this.editalBean = editalBean;
    }

    private void metodoQualquerUsandoOBeanIntegado() {
        Centro centro = editalBean.getCentro();
    }

    //...

}

Note: You do not need to pass name="editalBean" to annotation @ManagedBean of class EditalBean , because by convention the instance name of this bean will already be editalBean , so you are repeating yourself.

Another note: Look for ViewScoped beans instead of SessionScoped . The advantage is that a ViewScoped is created in the page request and its scope is limited to a browser tab, while the SessionScoped lasts for the entire session and besides the objects not being collected from memory the user may have a bad experience with the try working on two tabs at the same time.

SessionScoped should be used only for session data, as for user preferences and credentials.

Remembering that for my code above it works, it needs EditalBean to really be a comprehensive scope, just like the SessionScoped you are using. So recapping my general suggestion:

Conclusion:

Use a single bean to handle user interactions with tightly integrated features on the same page, so you avoid having to inject one bean into the other (which would result in poor code) and avoid having to use SessionScoped and can use ViewScoped which facilitates better use of available memory and favors a better user experience.

    
29.01.2015 / 16:18