0

I am sending some variables from an .xhtml page to a controller which sends data to another page that has another controller that handles the data of that page I have tried to use the setters and getters. the problem is that I don't really know to do with @PostConstruct.

XHTML file

<h:form id="frmStart">
 <p:growl id="growl" sticky="true" showDetail="true" />
 <p:panel id="pnlDatos">
 <p:dataTable id="tblObjeto" var="object" 
              value="#{parentControlador.listObjects}" 
              widgetVar="tblObjeto"
              emptyMessage="No hay datos">
  <f:facet name="header">
                            Objetos     
  </f:facet>
                      
                        

                          
<p:column headerText="Contar" width="20">
 <p:commandButton class="pi pi-list" id="openDialogContar" value="Ver"
                                             action="#{parentControlador.FindItems(object)}"
                                             />
</p:column>
</p:dataTable>
</p:panel>
</h:form>

ParentControlador controller code

    @Inject Item item;
    
public String FindItems(ObjectParent objectParent) {
        String type="";
        String result="";
        //variable that i am going to send
                ItemControlador itemControlador = new ItemControlador();
                itemControlador.setCorte(objectParent);
        try {
            
       if(item.listarItemsA(objectParent.getCorte_id()).isEmpty()){
                    this.listaItems = item.listarItemsB(objectParent.getCorte_id());
                    type = "A";
                }else{
                    this.listaItems = item.listarItemA(objectParent.getCorte_id());
                    type = "B";
                } 
                this.objeto = objectParent;  

                result = type == "A"? "A.xhtml":"B.xhtml"; 
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
                            "The   objectParent doesn't have items "+e.getMessage()));
        }
        return result;
    }

This is the controller that handles the view for "A.xhtml", "B.xhtml"Item

     @PostConstruct
     private Parent parent;
    public void init() {
    //From what I see, this is executed when a .xhtml page is using the controller to retrieve the dataTable
    //
                if(item.listarItemsB(objectParent.getCorte_id()).isEmpty()){
                    this.listaObjetos = item.listarItemA(objectParent.getCorte_id());
                }else{
                    this.listaObjetos = item.listarItemsA(objectParent.getCorte_id());
                }
                 
    }
    
    public Parent getCorte() {
        return corte;
    }

    public void setCorte(Parent parent) {
        this.parent = parent;
    }

How do I send data form an xhtml view that handles one controller X to view the data, send to another xhtml page that uses another controller Y, and show in the other page that uses Y controller to view the data of the data sent?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Melany
  • 71
  • 5
  • can you please format your code and put the xhtml and java in two separate blocks. – Kukeltje Apr 24 '19 at 18:30
  • dont know how to do that i have commented it so you can understand.. – Melany Apr 24 '19 at 19:51
  • Just click the edit button below the question – Kukeltje Apr 24 '19 at 20:16
  • Possible duplicate of [How to choose the right bean scope?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Selaron Apr 24 '19 at 22:18
  • @Selaron: What about https://stackoverflow.com/questions/5052869/jsf-2-0-pass-data-between-beans-or-pages – Kukeltje Apr 25 '19 at 06:02
  • Possible duplicate of [JSF 2.0 pass data between beans (or pages?)](https://stackoverflow.com/questions/5052869/jsf-2-0-pass-data-between-beans-or-pages) – Kukeltje Apr 25 '19 at 06:02
  • @Kukeltje the `scope` was my first thought because no scope information is provided in question. Passing objects to a `ViewScoped` bean within the current view won't work for example. Thus general information on scope might help the OP, while your link will also do. – Selaron Apr 25 '19 at 06:20

1 Answers1

0

In case you need to make some data shared between two .xhtml views, you may put your data in a @SessionScoped controller which will have the same state among both views within the same browser-session and is visible to both views. Simply from page1.xhtml submit your data to the @SessionScoped controller and access it from page2.xhtml

Ahmad Aboud
  • 51
  • 1
  • 5
  • this is not the **@SessionScoped** its about **@PostConstruct.** and how to send data between controllers ??? – Melany Apr 25 '19 at 00:37
  • 1
    @Melany: it might be you did not understand the answer. Effectively poster is stating that if you use session scoped, you do not need to pass anything on since it is 'just available' due to the long(er) scope. It is one of the solutions thougn And a session scope is effectively too long. A DeltaSpike ViewAccessSCoped would be better since it is shorter. But other solutions exist, see the duplicate – Kukeltje Apr 25 '19 at 06:01
  • Thanks for the answer but no, thats not the correct answer i am looking for something like this, ``` ``` – Melany Apr 26 '19 at 00:23
  • @Melany: I suppose that the answer solves the problem described by "**how do i send data form an xhtml view that handles one controller X to view the data, send to another xhtml page that uses another controller Y, and show in the other page that uses Y controller to view the data of the data sent**" if this is the only problem. It seems that you are focusing on the method of solution rather than the solution itself. – Ahmad Aboud Apr 26 '19 at 13:35