简体   繁体   English

如何在JSF页面中显示包含数据库数据的数据表

[英]How to display data table in JSF page with database data

I have a table in a database which is used for storing application configuration data. 我在数据库中有一个表,用于存储应用程序配置数据。

This is the table structure - it's very simple example: 这是表结构-这是非常简单的示例:

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

I want to display the table data in this way: 我想以这种方式显示表数据:

<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> 

I tried to display the data using this JSF code and this Java code, but the result was not what I want: 我尝试使用此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; 
    }

How I can display the data the way I want? 如何以所需方式显示数据?

Best wishes 最好的祝愿

You must not assign the get method with the parenthesis. 您不能为get方法分配括号。 You must use a List attribute from your managed bean. 您必须使用托管bean中的List属性。

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

Your managed bean should look like this: 您的托管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...
}

Second, you set the design of every column in your table, not the design of the rows. 其次,设置表中每一列的设计,而不是行的设计。 You're setting 1 column with 2 values and another column with the real output. 您将为1列设置2个值,为另一列设置实际输出。

Fixing your datatable code: 修复您的数据表代码:

<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 is the StackOverflow JSF expert. @BalusC是StackOverflow JSF专家。 He has a very nice example about using JSF DataTable in his blog entry . 在他的博客文章中,他有一个很好的有关使用JSF DataTable的示例。

Besides some design flaws that I already remarked in your previous questions, at least you have to use the correct value attribute for your dataTable. 除了我在前面的问题中已经提到的一些设计缺陷外,至少您还必须为dataTable使用正确的value属性。

Replace: 更换:

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

with: 有:

value="#{DashboardController.dashboardList}"

The "get" prefix will automatically be added. “ get”前缀将自动添加。 The brackets can be omitted. 括号可以省略。

getDataList() you can write some normal getter method. getDataList()您可以编写一些常规的getter方法。 you write some of the method dataList() and implement your business code in that method. 您编写一些方法dataList()并在该方法中实现您的业务代码。

Method declaration in the xhtml or jsp file in dataTable in jsf. 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM