2013-05-26 20 views
0

我想从数据表中传递的数据使用相同的支持bean与requestscope另一个页面,JSF的传球这是可能的,而无需使用数据模型?我使用的Servlet 3.0数据到其他页面不能正常工作

这里是我的数据表页面

<p:dataTable var="entity" value="#{collegeController.allCollege}"> 
     <p:column headerText="Code"> 
      #{entity.collegeCode} 
     </p:column> 
     <p:column headerText="Description"> 
      #{entity.collegeDesc} 
     </p:column> 
     <p:column headerText=""> 
      <p:commandLink value="Edit" action="#{collegeController.prepareEdit(entity)}"/> 
     </p:column> 
    </p:dataTable> 

,这里是我支持bean

@ManagedBean 
@RequestScoped 
public class CollegeController implements Serializable { 

    private String redirect = ".jsf?faces-redirect=true"; 
    private CollegeCatalog entity; 

    public CollegeController() { 
    } 

    public String prepareEdit(CollegeCatalog selectedEntity) { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     s.beginTransaction(); 

     entity = (CollegeCatalog) s.load(CollegeCatalog.class, selectedEntity.getCollegeKey()); 

     return "update" + redirect; 
    } 

    public List getAllCollege() { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = s.beginTransaction(); 

     String query = "" 
       + "FROM CollegeCatalog entity " 
       + "WHERE entity.deleted = FALSE"; 

     Query q = s.createQuery(query); 

     List l = q.list(); 

     tx.commit(); 

     return l; 
    } 

    /** 
    * @return the entity 
    */ 
    public CollegeCatalog getEntity() { 
     if (entity == null) { 
      entity = new CollegeCatalog(); 
     } 
     return entity; 
    } 

    /** 
    * @param entity the entity to set 
    */ 
    public void setEntity(CollegeCatalog entity) { 
     this.entity = entity; 
    } 
} 

,这是我更新页面(这是我想要展示选定的数据)

<h:form> 
    <p:outputLabel value="Code:" for="txtCode"/> 
    <p:inputText id="txtCode" value="#{collegeController.entity.collegeCode}"/> 
    <br/> 
    <p:outputLabel value="Description:" for="txtDesc"/> 
    <p:inputText id="txtDesc" value="#{collegeController.entity.collegeDesc}"/> 
    <br/><br/> 
    <p:commandButton value="Update" action="#{collegeController.update()}"/> 
    <p:commandButton value="Back" action="index.jsf?faces-redirect=true"/> 
</h:form> 

它总是返回null

回答

0

尝试用f:setPropertyActionListener@ViewScoped

像这样的事情

<p:column headerText=""> 
    <p:commandLink value="Edit" action="#{collegeController.action" > 
     <f:setPropertyActionListener value="#{collegeController.entity}" target="#{collegeController.targetEntity}"> 
    </p:commandLink> 
</p:column> 
+0

它也不起作用。 – borj

+0

看http://stackoverflow.com/questions/4994458/how-can-i-pass-a-parameter-to-a-commandlink-inside-a-datatable我使用#3从链接 –

+0

你给我正如你在我的数据表中看到的,不幸的是它不适合我。 – borj

3

您发送到您的collegeController.action方法的参数不会丢失,因为你有一个@RequestScoped管理bean和整个管理bean将被创建在每一个请求。此外,在您的实际设计中,将在每个collegeController.allCollege方法调用中重新创建列表。

解决你的问题:

  1. 您的托管bean范围更改范围较广。在这种情况下,@ViewScoped就好了。有关管理bean作用域更多信息:Communication in JSF 2 - Managed Bean Scopes

  2. 移动,吸气以外的所有业务逻辑。由于您正在使用JSF 2并希望在创建bean时加载列表,因此可以使用@PostConstruct方法并在其中加载列表。在JSF中使用托管bean时,请记住不要在您的getter/setter中放置任何业务逻辑。更多信息:Why JSF calls getters multiple times

考虑到这些意见并将它们应用到你的代码,管理bean将类似于此:

@ManagedBean 
@ViewScoped 
public class CollegeController implements Serializable { 

    private String redirect = ".jsf?faces-redirect=true"; 
    private CollegeCatalog entity; 
    private List<CollegeCatalog> collegeCatalogList; 

    public CollegeController() { 
    } 

    @PostConstruct 
    public void init() { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = s.beginTransaction(); 
     String query = "" 
       + "FROM CollegeCatalog entity " 
       + "WHERE entity.deleted = FALSE"; 
     Query q = s.createQuery(query); 
     collegeCatalogList = (List<CollegeCatalog>)q.list(); 
     tx.commit(); 

     entity = new CollegeCatalog(); 
    } 

    public String prepareEdit(CollegeCatalog selectedEntity) { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     s.beginTransaction(); 

     entity = (CollegeCatalog) 
      s.load(CollegeCatalog.class, selectedEntity.getCollegeKey()); 

     return "update" + redirect; 
    } 

    public List<CollegeCatalog> getAllCollege() { 
     return collegeCatalogList; 
    } 

    /** 
    * @return the entity 
    */ 
    public CollegeCatalog getEntity() { 
     return entity; 
    } 

    /** 
    * @param entity the entity to set 
    */ 
    public void setEntity(CollegeCatalog entity) { 
     this.entity = entity; 
    } 
} 

不过,请注意,这种方法只是帮助你发送entity您的<p:commandLink action="#{collegeController.prepareEdit(entity)}"/>中的参数添加到托管bean。如果不是重定向您刚才转发的页面,那会更好。

如果你还是喜欢做这个使用重定向,这种做法不会帮助你将数据发送到另一个页面。这将是更好,如果你使用,而不是<h:button>(甚至更好,一个<h:link>)将重定向到其他页面,您可以处理参数存在。这里更好地解释:Communication in JSF 2 - Processing GET Request Parameters

+0

tnx的澄清,我更新了我的托管bean,仍然没有传递任何数据要被目标页面读取。 – borj

+0

@borj你读过整个答案吗?就你而言,它将套装最后一段。 –

相关问题