将Bean中的数据用于JSF

时间:2014-12-09 00:53:20

标签: java jsf

我已经创建了一个bean来从Netbeans JavaDB获取数据。但在阅读了互联网和一些书籍之后,我仍然无法了解如何将bean中的数据转化为JSF,任何人都可以帮忙。

这是我的代码。

package beans;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class RankBean
{

    String error;
    List<Object> allData=new ArrayList<Object>();

    public String FORENAME,SURNAME;
    public Integer SCORE;
    Object obj=new Object();

    public void setObj(Object obj) {
        this.obj = obj;
    }
    public String getError() {
        return error;
    }
    public List getDb_Data()
    {
        int i=0;
        try
        {
                String SQLQuery;
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/nestasark","","");
                Statement statement = conn.createStatement();                
                SQLQuery="SELECT FORENAME,SURNAME,SCORE FROM USERS";
                ResultSet resultSet=statement.executeQuery(SQLQuery);
                while(resultSet.next())
                {
                    DataFields d=new DataFields(resultSet.getString(1),resultSet.getString(2), resultSet.getInt(3));                    
                    allData.add(i,d);
                    i++;
                }

        }
        catch(Exception ex)
        {
                error="<b>Contact Administrator :</b><br/>" + ex;
                System.out.println("Your query is not working" + ex);
        }
       return allData;
    }
    public String getFname()
    {
        this.FORENAME=((DataFields)obj).FName;
        return this.FORENAME;
    }
    public Integer getScore() {
        this.SCORE=((DataFields)obj).Score;
        return this.SCORE;
    }

    public String getLname() {
        this.SURNAME=((DataFields)obj).LName;
        return this.SURNAME;
    }

    public class DataFields
    {
        public String FName,LName;
        public Integer Score;

        public DataFields(String  fname,String  lname, Integer score)
        {
            this.FName=fname;
            this.LName=lname;
            this.Score=score;
        }
    }

}

以下是JSF代码的一部分:

                <div class="panel panel-default">
                    <div class="panel-heading">
                        Top Players
                    </div>
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th class='col-md-1'>
                                    Place
                                </th>
                                <th>
                                    User
                                </th>
                                <th>
                                    Points
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <th>#{RankBean.getDb_Data}</th>



                        </tbody>
                    </table>
                </div>
            </div>
            <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Stats
                    </div>
                    <ul class="list-group">
                        <li class="list-group-item">
                            1st globally
                        </li>
                        <li class="list-group-item">
                            9999 total points
                        </li>
                        <li class="list-group-item">
                            #{orgBean.memberCount} students
                        </li>
                        <li class="list-group-item">
                            #{orgBean.org.groupCount} groups
                        </li>
                    </ul>
                </div>

在地方用户和积分表中,我已将#{RankBean.getDb_Data}仅用于测试它是否正在运行,但它没有。 感谢

SSJ

1 个答案:

答案 0 :(得分:0)

如果您尝试使用jsf创建表结构。然后只需使用数据表: -

http://www.tutorialspoint.com/jsf/jsf_display_datatable.htm

还有其他几个标记库也是相同的。