如何从两个bean

时间:2016-03-15 16:24:58

标签: jsf jsf-2 primefaces datatable

我有一个数据表,其中的值来自DB。 在我的班级myBean中,我有一个类型为User的变量来存储所选行。它只能选择一行。现在我想从另一个名为printUser的bean调用此变量来获取所选用户。

但它始终打印null

查看

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

}

希望你能解决我的问题。 非常感谢 请原谅我的英文

1 个答案:

答案 0 :(得分:2)

这是在同一页面和另一页面中使用所选行的实体的示例。该示例使用Lombok Getter,Setter和Data注释来实现简洁性:

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;

    }

ManagedBean作为控制器:

    @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_ )
      {
      }

    }

另一个使用第一个ManagedBean的ManagedBean(如果你真的想要的话):

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

      ...    
    }

无状态会话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();
      }
    }

索引页面(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>

另一个页面(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>