Неправильный расчет нумерации страниц на странице JSF

Я хочу реализовать код из этого учебника

Я заполнил таблицу Oracle 14 строками данных, но когда я пытаюсь перемещаться между страницы JSF каждый раз, когда вторая страница пуста:

enter image description here

enter image description here

Это код страницы JSF:




    
                   
                     
        
    
    

        

NVIDIA.com History Center



Сеансы:

   import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/* include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UICommand;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean; 
import javax.faces.event.ActionEvent;

import org.glassfish.osgicdi.OSGiService;

// source http://balusc.blogspot.com/2008/10/effective-datatable-paging-and-sorting.html
@Named("SessionsController")
@ViewScoped
public class Sessions implements Serializable {

    /* Call the Oracle JDBC Connection driver */
    @Resource(name = "jdbc/Oracle")
    private DataSource ds;
    // Data.
    private List dataList;
    // Rows
    private int totalRows;
    // Paging.
    private int firstRow;
    private int rowsPerPage;
    private int totalPages;
    private int pageRange;
    private Integer[] pages;
    private int currentPage;
    // Sorting.
    private String sortField;
    private boolean sortAscending;

    /* Constructor */
    public Sessions() {
        /* Set default properties */
        rowsPerPage = 10; // Default rows per page (max amount of rows to be displayed at once).
        pageRange = 10; // Default page range (max amount of page links to be displayed at once).
        sortField = "ASESSIONID"; // Default sort field.
        sortAscending = true; // Default sort direction.
    }

    public static class ActiveSessionObj {

        /* Oracle table structure
        CREATE TABLE ACTIVESESSIONSLOG(
        ASESSIONID VARCHAR2(30 ) NOT NULL,
        USERID VARCHAR2(30 ),
        ACTIVITYSTART TIMESTAMP(6),
        ACTIVITYEND TIMESTAMP(6),
        ACTIVITY CLOB
        )
         */
        private String aSessionID;
        private String userID;
        private Date activityStart;
        private Date activityEnd;
        private String activity;

        public ActiveSessionObj(String aSessionID, String userID, Date activityStart, Date activityEnd, String activity) {
            this.aSessionID = aSessionID;
            this.userID = userID;
            this.activityStart = activityStart;
            this.activityEnd = activityEnd;
            this.activity = activity;
        }

        public String getaSessionID() {
            return aSessionID;
        }

        public void setaSessionID(String aSessionID) {
            this.aSessionID = aSessionID;
        }

        public String getactivity() {
            return activity;
        }

        public void setactivity(String activity) {
            this.activity = activity;
        }

        public Date getactivityEnd() {
            return activityEnd;
        }

        public void setactivityEnd(Date activityEnd) {
            this.activityEnd = activityEnd;
        }

        public Date getactivityStart() {
            return activityStart;
        }

        public void setactivityStart(Date activityStart) {
            this.activityStart = activityStart;
        }

        public String getuserID() {
            return userID;
        }

        public void setuserID(String userID) {
            this.userID = userID;
        }

        private ActiveSessionObj() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }

    // Paging actions -----------------------------------------------------------------------------
    public void pageFirst() {
        page(0);
    }

    public void pageNext() {
        page(firstRow + rowsPerPage);
    }

    public void pagePrevious() {
        page(firstRow - rowsPerPage);
    }

    public void pageLast() {
        page(totalRows - ((totalRows % rowsPerPage != 0) ? totalRows % rowsPerPage : rowsPerPage));
    }

    public void page(ActionEvent event) {
        page(((Integer) ((UICommand) event.getComponent()).getValue() - 1) * rowsPerPage);
    }

    private void page(int firstRow) {
        this.firstRow = firstRow;
        loadDataList(); // Load requested page.
    }

    // Sorting actions ----------------------------------------------------------------------------
    public void sort(ActionEvent event) {
        String sortFieldAttribute = (String) event.getComponent().getAttributes().get("sortField");

        // If the same field is sorted, then reverse order, else sort the new field ascending.
        if (sortField.equals(sortFieldAttribute)) {
            sortAscending = !sortAscending;
        } else {
            sortField = sortFieldAttribute;
            sortAscending = true;
        }

        pageFirst(); // Go to first page and load requested page.
    }

    // Loaders ------------------------------------------------------------------------------------
    private void loadDataList() {

        // Load list and totalCount.
        try {
            dataList = list(firstRow, rowsPerPage, sortField, sortAscending);
            totalRows = countDBRowNum(); //count the tablerows
        } catch (Exception e) {
            throw new RuntimeException(e); 
        }

        // Set currentPage, totalPages and pages.
        currentPage = (totalRows / rowsPerPage) - ((totalRows - firstRow) / rowsPerPage) + 1;
        totalPages = (totalRows / rowsPerPage) + ((totalRows % rowsPerPage != 0) ? 1 : 0);
        int pagesLength = Math.min(pageRange, totalPages);
        pages = new Integer[pagesLength];

        // firstPage must be greater than 0 and lesser than totalPages-pageLength.
        int firstPage = Math.min(Math.max(0, currentPage - (pageRange / 2)), totalPages - pagesLength);

        // Create pages (page numbers for page links).
        for (int i = 0; i < pagesLength; i++) {
            pages[i] = ++firstPage;
        }
    }

    // Getters ------------------------------------------------------------------------------------
    public List getdataList() {
        if (dataList == null) {
            loadDataList(); // Preload page for the 1st view.
        }
        return dataList;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public int getFirstRow() {
        return firstRow;
    }

    public int getRowsPerPage() {
        return rowsPerPage;
    }

    public Integer[] getPages() {
        return pages;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPages() {
        return totalPages;
    }

    // Setters ------------------------------------------------------------------------------------
    public void setRowsPerPage(int rowsPerPage) {
        this.rowsPerPage = rowsPerPage;
    }

    // Actions ------------------------------------------------------------------------------------
    /**
     * Returns list of ActiveSessionObj items starting at the given first index with the given row count,
     * sorted by the given sort field and sort order.
     * @param firstRow First index of rows to be returned.
     * @param rowCount Amount of rows to be returned.
     * @param sortField Field to sort the data on.
     * @param sortAscending Whether to sort data ascending or not.
     * @return list of ActiveSessionObj items starting at the given first index with the given row count,
     * sorted by the given sort field and sort order.
     * @throws DAOException If something fails at DAO level.
     */
    public List list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws SQLException {

        String SqlStatement = null;

        if (ds == null) {
            throw new SQLException();
        }

        Connection conn = ds.getConnection();
        if (conn == null) {
            throw new SQLException();
        }

        String sortDirection = sortAscending ? "ASC" : "DESC";

        SqlStatement = "SELECT c.*" +
                          " FROM (SELECT b.*, rownum rn" +
                                 " FROM (SELECT a.*" +
                                          " FROM activeSessionsLog a" +
                                         " ORDER BY %s %s) b" +
                                 " WHERE rownum <= ?) c" +
                       " WHERE rn >= ?";

        String sql = String.format(SqlStatement, sortField, sortDirection);

        PreparedStatement ps = null;
        ResultSet resultSet = null;
        List dataList = new ArrayList();

        try {
            conn.setAutoCommit(false);
            boolean committed = false;
            try {
                ps = conn.prepareStatement(sql);
                ps.setInt(1, firstRow);
                ps.setInt(2, rowCount);


                resultSet = ps.executeQuery();
                /* take the result from the SQL query and insert it into Array List collection */
                dataList = ActiveSessionsArrayList(resultSet);

                conn.commit();
                committed = true;

            } finally {
                if (!committed) {
                    conn.rollback();
                }
            }

        } finally {
            ps.close();
            conn.close();
        }

        return dataList;
    }

    /**
     * Returns total amount of rows in table.
     * @return Total amount of rows in table.
     * @throws DAOException If something fails at DAO level.
     */
    public int countDBRowNum() throws Exception {

        String SqlStatement = null;

        if (ds == null) {
            throw new SQLException();
        }

        Connection conn = ds.getConnection();
        if (conn == null) {
            throw new SQLException();
        }

        SqlStatement = "SELECT count(*) FROM ACTIVESESSIONSLOG";

        PreparedStatement ps = null;
        ResultSet resultSet = null;
        int count = 0;

        try {
            conn.setAutoCommit(false);
            boolean committed = false;
            try {
                SqlStatement = "SELECT count(*) FROM ACTIVESESSIONSLOG";

                ps = conn.prepareStatement(SqlStatement);
                resultSet = ps.executeQuery();

                if (resultSet.next()) {
                    count = resultSet.getInt(1);
                }

                conn.commit();
                committed = true;
            } finally {
                if (!committed) {
                    conn.rollback();
                }
            }
        } finally {
            ps.close();
            conn.close();
        }

        return count;
    }

    /**
     * Map the current row of the given ResultSet to ActiveSessionObj.
     * @param resultSet The ResultSet of which the current row is to be mapped to ActiveSessionObj.
     * @return The mapped ActiveSessionObj from the current row of the given ResultSet.
     * @throws SQLException If something fails at database level.
     */
    private static ArrayList ActiveSessionsArrayList(ResultSet rs) throws SQLException {
        ArrayList list = new ArrayList<>();


        try {
            while (rs.next()) {
                list.add(new ActiveSessionObj(
                        rs.getString("ASESSIONID"),
                        rs.getString("USERID"),
                        timestampToDate(rs.getTimestamp("ACTIVITYSTART")),
                        timestampToDate(rs.getTimestamp("ACTIVITYEND")),
                        rs.getString("ACTIVITY")));
            }
        } catch (Exception x) {
            x.printStackTrace();
        }

        return list;


        /*
        CREATE TABLE ACTIVESESSIONSLOG(
        ASESSIONID VARCHAR2(30 ) NOT NULL,
        USERID VARCHAR2(30 ),
        ACTIVITYSTART TIMESTAMP(6),
        ACTIVITYEND TIMESTAMP(6),
        ACTIVITY CLOB
        )
         */
    }
    // select all button handle
    private Map selectedIds = new HashMap();

    // get the list from the JSF page
    public Map getSelectedIds() {
        return selectedIds;
    }

    // call this method from the JSF page to delete the selected rows
    public String deleteAction() {

        // do something with the selected rows
        return null;
    }

    private static Date timestampToDate(java.sql.Timestamp ts) {
        Date d = null;
        try {
            d = new Date(ts.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return d;
    }
}

Может быть, алгоритм нумерации страниц неверен или SQL-запрос не исправлен? Я не трогал алгоритм разбивки на страницы из исходного кода.

С наилучшими пожеланиями

РЕДАКТИРОВАТЬ

Неверный SQL-запрос.

РЕДАКТИРОВАТЬ 2 Я обновил код SQL, но теперь первая страница неверна, а вторая и так далее отображает данные. Проблема где-то здесь:

РЕДАКТИРОВАТЬ 3 Рабочий код

список общедоступных списков (int firstRow, int rowCount, String sortField, boolean sortAscending) выдает SQLException {

String SqlStatement = null;

if (ds == null) {
    throw new SQLException();
}

Connection conn = ds.getConnection();
if (conn == null) {
    throw new SQLException();
}

int countrow = firstRow + rowCount;
String sortDirection = sortAscending ? "ASC" : "DESC";

SqlStatement = "SELECT c.*" +
                  " FROM (SELECT b.*, rownum rn" +
                         " FROM (SELECT a.*" +
                                  " FROM activeSessionsLog a" +
                                 " ORDER BY %s %s) b" +
                         " WHERE rownum <= ?) c" +
               " WHERE rn > ?";

String sql = String.format(SqlStatement, sortField, sortDirection);

PreparedStatement ps = null;
ResultSet resultSet = null;
List dataList = new ArrayList();

try {
    conn.setAutoCommit(false);
    boolean committed = false;
    try {
        ps = conn.prepareStatement(sql);
        ps.setInt(1, countrow);
        ps.setInt(2, firstRow);


        resultSet = ps.executeQuery();
        /* take the result from the SQL query and insert it into Array List collection */
        dataList = ActiveSessionsArrayList(resultSet);

        conn.commit();
        committed = true;

    } finally {
        if (!committed) {
            conn.rollback();
        }
    }

} finally {
    ps.close();
    conn.close();
}

return dataList;

}

6
задан Peter Penzov 12 April 2012 в 13:08
поделиться