2012-03-09 88 views
1

我在用於存儲應用程序配置數據的數據庫中有一個表。如何使用數據庫數據在JSF頁面中顯示數據表

這是表結構 - 這是非常簡單的例子:

SessionTTL    MaxActiveUsers   
---------------------- ---------------------- 
30      787      

我想用這種方式來顯示錶中的數據:

<table border="1"> 
    <tr> 
    <td>SessionTTL</td> 
    <td>30</td> 
    </tr> 
    <tr> 
    <td>MaxActiveUsers</td> 
    <td>787</td> 
    </tr> 
    <tr> 
    <td>option</td> 
    <td>value</td> 
    </tr> 
    <tr> 
    <td>option</td> 
    <td>value</td> 
    </tr> 
</table> 

我嘗試使用這個JSF代碼來顯示數據和這個Java代碼,但結果不是我想要的:

   <h:dataTable id="books" 
         columnClasses="list-column-center, 
         list-column-right, list-column-center, 
         list-column-right" headerClass="list-header" 
         rowClasses="list-row" styleClass="list- 
         background" value="#{DashboardController.getDashboardList()}" var="store"> 
       <h:column> 
         <h:outputText value="Session Timeout"/> 
         <h:outputText value="Maximum Logged Users"/> 
       </h:column> 
       <h:column>      
         <h:outputText value="#{store.sessionTTL} minutes"/> 
         <h:outputText value="#{store.maxActiveUsers}"/> 
       </h:column> 

      </h:dataTable> 




public List<Dashboard> getDashboardList()throws SQLException{ 

     List<Dashboard> list = new ArrayList<Dashboard>(); 

     if(ds == null) { 
       throw new SQLException("Can't get data source"); 
     } 

     Connection conn = ds.getConnection(); 

     if(conn == null) { 
       throw new SQLException("Can't get database connection"); 
     } 

     PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS"); 

     try{ 
      //get data from database   
      ResultSet result = ps.executeQuery(); 
      while (result.next()){ 
       Dashboard cust = new Dashboard(); 
       cust.setSessionTTL(result.getString("SessionTTL")); 
       cust.setMaxActiveUsers(result.getString("MaxActiveUsers")); 
       list.add(cust); 
      } 
     } 
     catch(Exception e1){ 
      // Log the exception. 
     } 
     finally{ 
      try{ 
       ps.close(); 
       conn.close(); 
      } 
      catch(Exception e2){ 
       // Log the exception. 
      } 
     } 
     return list; 
    } 

我如何顯示數據是我想要的方式?

最良好的祝願

回答

4

您不得將get方法與括號一起賦值。您必須使用託管bean中的List屬性。

value="#{DashboardController.getDashboardList()}" //WRONG! 

你管理的bean應該是這樣的:

public class DashboardController { 
    private List<Dashboard> lstDashboard; 
    public DashboardController() { 
     try { 
      lstDashboard = getDashboardList(); 
     } catch (Exception e) { 
      //log the exception or something else... 
     } 
    } 
    //getter and setter... 
    public List<Dashboard> getLstDashboard() { 
     return this.lstDashboard; 
    } 
    public void setLstDashboard(List<Dashboard> lstDashboard) { 
     this.lstDashboard = lstDashboard; 
    } 
    //your other methods here... 
} 

其次,您可以設置每列的設計在表中,而不是行設計。您將設置1個具有2個值的列和另一個具有實際輸出的列。

修復您的數據表代碼:

<h:dataTable id="books" 
    columnClasses="list-column-center, 
     list-column-right, list-column-center, 
     list-column-right" headerClass="list-header" 
     rowClasses="list-row" 
    styleClass="list-background" 
    value="#{DashboardController.lstDashboard}" 
    var="store"> 

    <h:column> 
     <f:facet name="header"> 
      <h:outputText value="Session Timeout" /> 
     </f:facet> 
     <h:outputText value="#{store.sessionTTL} minutes"/> 
    </h:column> 
    <h:column>      
     <f:facet name="header"> 
      <h:outputText value="MaxActiveUsers" /> 
     </f:facet> 
     <h:outputText value="#{store.maxActiveUsers}"/> 
    </h:column> 

</h:dataTable> 

@BalusC是StackOverflow的JSF專家。他在他的blog entry中使用JSF DataTable有一個很好的例子。

2

除此之外,我已經說過在你前面的問題,至少你必須使用正確的value屬性爲您的dataTable一些設計上的缺陷。

替換:

value="#{DashboardController.getDashboardList()}" 

有:

value="#{DashboardController.dashboardList}" 

「獲取」 前綴將自動添加。括號可以省略。

0

getDataList()你可以寫一些普通的getter方法。你寫了一些方法dataList()並在該方法中實現你的業務代碼。

jsf中dataTable中xhtml或jsp文件中的方法聲明。

<h:dataTable id="books" type="submit" value="#{DashboardController.dataList}" var="dashbord"> 

    <h: column name="ID"> 

     <f:facet name="header"> 

     <h:outputText value="#{dashbord.id}"/> 

     </f:facet> 

    </h:column> 

...your another columns... 

</h:dataTable> 
+0

歡迎來到Stack Overflow!這是一個乾淨的問答網站,每個人都可以在協議/不同意的情況下投票。這不是一個老式的論壇,每個人都會在協議/不同意的情況下以混亂的形式重複彼此,就像你試圖做的那樣。我建議不要在已經回答的問題上發佈答案,除非你確實需要添加大量的東西 - 在這種情況下似乎不是這種情況。只需通過發佈良好答案首先獲得15個代表,然後您可以在協議上投票表決其他人的帖子,而不是重複他們。 – BalusC 2013-04-16 12:18:52

相關問題