Manipulating two lists of a Drag and Drop component in different Managedbeans

Asked

Viewed 164 times

2

I am using the Datatable Drag And Drop component. Contextualizing is as follows:

I got a rap sheet based on footsteps. Where in the first step I enter the information of an Edict X, what is manipulated by 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 don’t even suggest documentation I can make it work. 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 editalBean. My solution was to put the list of selected disciplines in the disciplinesBean and the complete list of disciplines in 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 complete list of disciplines in another bean, I don’t know how to remove it.

My onDisciplinaDrop method is in the disciplineBean and my listComplete is in editalBean. How can I 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;
    }

Disciplinabean.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 I try to explain better. Thank you.

  • The general need I understood but the specific problem was not clear to me. I imagine that your difficulty lies in using in a Bean the objects that were instantiated in another Bean. Is that it? Two suggestions: consider using a single bean for resources on the same page that interact so much with each other; and consider using Viewscoped because Sessionscoped can greatly harm the user experience.

  • Good morning @Caffé, my problem is exactly this, through Disciplinabean managing to manipulate an object that is in Editalbean, so the best thing would be to create a Bean type: formCadastroBean (since it is a registration form) and within it I work with both Beans? Is that about right? And what’s the difference between Session Scoped and View Scoped, they interfere with that?

1 answer

1


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

To meet user requests in resources so interconnected on the same page, there is no problem in using the same bean and I recommend that you do so, so you eliminate the need to inject objects of 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 to:

@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();
    }
    
    //...

}

Observing: you don’t need to pass name="editalBean" for annotation @ManagedBean class EditalBean, because by convention the instance name of this bean will already be in fact editalBean, so that you are repeating yourself.

Another observation: try to use Beans Viewscoped 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 Sessionscoped lasts throughout the session and in addition to the objects not being collected from memory the user may have a bad experience while trying to work on two tabs at the same time.

Sessionscoped should only be used for actual session data, such as user preferences and your credentials.

Remembering that for my code above to work, it needs the EditalBean be in fact of a comprehensive scope, such as the Sessionscoped you are using. So, recapturing my general suggestion:

Completion:

Use a single bean to handle user interactions with very integrated features of the same page, so you avoid having to inject a bean into the other (which would result in bad code) and avoid having to use Sessionscoped, can use Viewscoped that facilitates better use of available memory and favors a better user experience.

  • Gave straight. Thank you very much @Caffé, excellent reply!

Browser other questions tagged

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