0

I've got a datatable with values in which I get from my DB. In my class myBean, I've got an Variable of type User to store the selected row. It just can be selected one row. Now I want to call this Variable from an other bean which is called printUser to get the selected User.

But it always prints null.

View

<p:dataTable id="userDT" var="user" value="#{myBean.getUserList()}" selection="#{myBean.selectedUser}" 
             rowKey="#{user.id}" >

    <p:column selectionMode="single" style="width:16px;text-align:center"/>

    <p:column width="200" headerText="ID">
        <h:outputText value="#{user.id}" />
    </p:column>

    <p:column width="200" headerText="Firstname">
        <h:outputText value="#{user.firstname}" />
    </p:column>

    <p:column width="250" headerText="Lastname">
        <h:outputText value="#{user.lastname}" />
    </p:column>

</p:dataTable>

myBean

@Named(value = "myBean")
@ManagedBean
@SessionScoped
public class myBean implements Serializable {
    private static final long serialVersionUID = 1L;

    private User selectedUser = new User();

    public myBean() {

    }

    public List<User> getUserList() {

        ...    
    }

    public Patient getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser= selectedUser;

    }

}

User.java

public class User {

    private Integer id;
    private String firstname;
    private String lastname;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

}

printUser

@Named(value = "printUser")
@ManagedBean
@RequestScoped
public class printUser {

    public printUser() {

    }

    public void getSelectedUserData(){
        myBean bean = new myBean();
        User user = new User();

        user = bean.getSelectedUser();

        System.err.println("UserID: " + user.getID());
    }

}

Hope you undertand my Problem. Thanks alot Excuse my English

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mihawk
  • 815
  • 3
  • 14
  • 31
  • 1
    You can just inject myBean into printUser. But you should clean up your annotations - either use CDI beans or managed beans. You use both. And also use uppercase for class names – Jaqen H'ghar Mar 15 '16 at 16:49
  • Please take a step back, find a good JSF2(.2) with CDI tutorial and use that. In the mean time try to pick up some basic java code knowledge – Kukeltje Mar 15 '16 at 19:22

1 Answers1

2

This is an example to use the entity of the selected row in the same page and in another page. The example use Lombok Getter,Setter and Data annotations for shortness:

The entity for the DataTable:

    @Entity
    @Data
    @TableGenerator( name = "GEN_TestEntity1", table = "ID_Generator", pkColumnName = "GEN_KEY", pkColumnValue = "GEN_TestEntity1", valueColumnName = "GEN_VALUE" )
    @NamedQuery( name = TestEntity1.QUERY_ALL_TESTENTITY1, query = "SELECT te1 FROM TestEntity1 te1" )
    public class TestEntity1 implements Serializable
    {
      public static final String QUERY_ALL_TESTENTITY1 = "query_All_TestEntity1";
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue( strategy = GenerationType.TABLE, generator = "GEN_TestEntity1" )
      private int id;

      @Column
      private String name;

    }

The ManagedBean as a controller:

    @ManagedBean
    @SessionScoped
    public class EntityBean
    {
      @EJB
      private EntitySER entitySER;

      @Getter
      @Setter
      private TestEntity1 selectedEntity;

      public List<TestEntity1> getAllTestEntity1()
      {
        return entitySER.getAllTestEntity1();
      }

      public void onRowSelect( SelectEvent event_ )
      {
      }

    }

Another ManagedBean which uses the first one (if you really want it):

    @ManagedBean
    @RequestScoped
    public class AnotherBean
    {
      @ManagedProperty( value="#{entityBean}" )
      private EntityBean entityBean;

      ...    
    }

The stateless session bean:

    @Stateless
    @LocalBean
    public class EntitySER
    {
      @PersistenceContext
      private EntityManager em;

      public List<TestEntity1> getAllTestEntity1()
      {
        Query q = em.createNamedQuery( TestEntity1.QUERY_ALL_TESTENTITY1 );
        return q.getResultList();
      }
    }

The index page (index.xhtml):

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
      <h:head>
        <title>Page 1</title>
      </h:head>
      <h:body>
        <h:form id="form">
          <p:dataTable id="table_TestEntity1" value="#{entityBean.allTestEntity1}" var="entity" selection="#{entityBean.selectedEntity}" 
                       rowKey="#{entity.id}" selectionMode="single">
            <p:ajax event="rowSelect" listener="#{entityBean.onRowSelect}" update=":form:entID :form:entName"/>
            <p:column>
              #{entity.id}
            </p:column>
            <p:column>
              #{entity.name}
            </p:column>
          </p:dataTable>
          <p>
            <h:outputLabel id="entID" value="#{entityBean.selectedEntity.id}"/>:
            <h:outputLabel id="entName" value="#{entityBean.selectedEntity.name}"/>
          </p>
          <p>
            <h:commandButton value="Page 2" action="/faces/another.xhtml"/>
          </p>
        </h:form>
      </h:body>
    </html>

The another Page (another.xhtml):

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html">
      <h:head>
        <title>Another Page</title>
      </h:head>
      <h:body>
        <p>
          The selected entity: #{entityBean.selectedEntity.id}:#{entityBean.selectedEntity.name}
        </p>
        <h:form>
          <h:commandButton value="Back" action="/faces/index.xhtml"/>
        </h:form>
      </h:body>
    </html>
The Bitman
  • 1,279
  • 1
  • 11
  • 25