How to change a list of another ManagedBean

2

I've developed a search area on my system where the user can add an item to their list of items. Like a shopping cart.

The search area uses a different ManagedBean than the user panel that has a dataTable that is loaded in another managedBean.

What I want: I'm already persisting the item correctly, however I use it in the SessionScoped user pane and wanted the dataTable list to update the new item immediately.

The way it is now I have to logout the user and log back in for the load item. I can not change to viewScoped because I have other components (dialog and confirmDialog) on the page that need data in memory.

Bean for Search page with method that registers a new item:

@ManagedBean(name = "api")
@SessionScoped
public class AplicacaoBean implements Serializable{
private static final long serialVersionUID = 1L;

// Pega o usuário logado
@ManagedProperty(value = "#{uBean.usuario}")
private Usuario usuario;

//Outros atributos e métodos

public String acompanharCaso() {
    Patrulheiro patrul = new Patrulheiro();
    if (usuario.getEmailAddress() != null
            && usuario.getEmailAddress() != "") {

        try {
            patrul = (Patrulheiro) buscarPatrulheiroPorEmail();
            assocLocaliza = new PatrulDesapLocaliza();

            assocLocaliza.setDesaparecido(desaparecido);
            assocLocaliza.setPatrulheiro(patrul);
            List<PatrulDesapLocaliza> acompanhamentos = Arrays
                    .asList(assocLocaliza);
            patrul.setPatrulLocalizaDesap(acompanhamentos);

            new PatrulheiroJPA().gravarAtualizar(patrul);
            FacesUtil
                    .addSuccessMessageWithDetail("frmDialogo",
                            "Dados enviados",
                            "Agora você está acompanhando esse caso! Verifique no painel de usuário");
        } catch (Exception e) {
            e.printStackTrace();
            // Não é um Patrulheiro
            FacesUtil.addErrorMessageWithDetail("frmDialogo", "Atenção",
                    "Você deve ser um Patrulheiro para acompanhar o caso!");
        }

    } else {
        // Nao está logado
        FacesUtil.addErrorMessageWithDetail("frmDialogo", "Atenção",
                "Logue-se como Patrulheiro para acompanhar o caso!");
    }
    return null;
}
}

Bean for User page with list that should receive the new item:

@ManagedBean(name = "patrulBean")
@SessionScoped
public class PatrulheiroBean implements Serializable{
  private static final long serialVersionUID = 1L;
  // Pega o usuário logado
  @ManagedProperty(value = "#{uBean.usuario}")
  private Usuario usuario;

  //Outros métodos e atributos

  @PostConstruct
  public void inicializar() {
      try {
          limpar();
          //lista que deve receber o novo desaparecido adicionado
          listaDesaparecido = new DesaparecidoJPA()
                  .buscarDesaparecidosPorIdPatrulheiro(usuario);

      } catch (Exception e) {
          System.out.println("Não foi possível resgatar os dados da lista de acompanhados");
          e.printStackTrace();
      }
  }

  public void limpar() {
      desaparecido = new Desaparecido();
      descricao = new DescricaoDesaparecido();
      patrulheiro = new Patrulheiro();
      assocLocaliza = new PatrulDesapLocaliza();
  }
}

I would like to load and change this user panel list in another bean, or when the user enters your panel the list will be updated?

    
asked by anonymous 24.11.2015 / 15:23

2 answers

0

I was able to resolve this problem after trying to use @ManagedProperty(value = "#{patrulBean}") which caused me the errors in the comments.

I used the f:event tag with type preRenderView and made the call to the method that loads the list every time the page is called:

<ui:composition template="/WEB-INF/template/Layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<f:metadata>
    <f:event type="preRenderView" listener="#{patrulBean.iniciarLista}" />
</f:metadata>
...

More: JSF 2 PreRenderViewEvent example

Thanks for the help also Nilson

    
26.11.2015 / 05:15
1

Include the ManagedBean as a property and update its collection.

@ManagedBean(name = "api")
@SessionScoped
public class AplicacaoBean implements Serializable{
private static final long serialVersionUID = 1L;

@ManagedProperty(value = "#{uBean.usuario}")
private Usuario usuario;

@ManagedProperty(value = "#{patrulBean}")
private PatrulheiroBean patrulheiroBean;

public String acompanharCaso() {
    //...
    patrulheiroBean.atualizaColecao();
}
    
24.11.2015 / 16:56