开发者

JavaBean Servlet Jsp Pagination

I am trying to display data into multiple pages. I am not sure what I am doing wrong over here. Can someone let me know what I am doing wrong over here. I can get all the data in one page but cant get it into multiple pages. Thank you

I have Employee jsp page shown as below

<script>
function setPageName() {

    document.EmployeeList.pageName.value="Employee";
    document.EmployeelList.submit();
}
</script>

<BODY text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000" onload="setPageName()">
<FORM NAME="EmployeeList" METHOD=POST ACTION="EmployeeServlet">

<INPUT TYPE=HIDDEN NAME="noRowsOnPage" VALUE="<%= empBean.getNoRowsOnPage()%>">
<INPUT TYPE=HIDDEN NAME="beginRowNumOnPage" VALUE="<%= empBean.getBeginRowNumOnPage()%>">
<INPUT TYPE=HIDDEN NAME="endRowNumOnPage"   VALUE="<%= empBean.getEndRowNumOnPage()%>">

<%
Collection dtlItems = empBean.getDetailItems();
Iterator it = dtlItems.iterator();
int i = 0;
String colorStr = null;


while(it.hasNext()){
    EmployeeItemBean empDtlItem = (EmployeeItemBean) it.next();
    }
%>

    <TR>
        <TD ><%= auditemailDtlItem.getSeqNoHTML()%></TD>
        <TD><%= auditemailDtlItem.getAuditorName()%>&nbsp;</TD>
        <TD><%= auditemailDtlItem.getEmail()%></TD>
    </TR>

<% 
  i++;
} %>

    <TR>
            <TD align="center">
                <% if(empBean.getBeginRowNumOnPage() == 1 || empBean.getBeginRowNumOnPage() == 0) {%>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="First" disabled>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Prev" disabled>
                <%} else {%>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="First">
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Prev">
                <%} %>

                <% if(empBean.getEndRowNumOnPage() == empBean.getTotalRowCount()) {%>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Next" disabled>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Last" disabled>
                <%} else {%>
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Next">
                    <INPUT TYPE=SUBMIT NAME="action" VALUE="Last">
                <%} %>
            </TD>
        </TR>

</TABLE>

My servlet looks like this:

public class EmployeeServlet {

    public void defaultAction(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException
    {

        EmployeeWebBean ewBean = new EmployeeWebBean();

        ewBean.initializePage();
        ewBean.retrieveSessionData(request);
        ewBean.retrieveSubmitData(request, response);
        ewBean.determineAction();
        ewBean.processActions();
        ewBean.populateModelBean();
        ewBean.dispatchToJSP(this, request,response);

        return;
    }
}

My employeeWebBean looks like this where all processing takes place:

private final static int ACTION_INITIAL_LOAD = 0;
    private final static int ACTION_GO = 1;
    private final static int ACTION_PREV = 2;
    private final static int ACTION_NEXT = 3;
    private final static int ACTION_FIRST = 4;
    private final static int ACTION_LAST = 5;
    private final static int PREV = 0;
    private final static int NEXT = 1;
    private final static int FIRST = 2;
    private final static int LAST = 3;

        public void initializePage()
    {
        _empBean = new EmployeeBean();
        _actionPick = ACTION_INITIAL_LOAD;
        _clkAction = new String();

        _beginRowNumOnPage = 0;
        _endRowNumOnPage = 0;
    }


    public void determineAction()
    {
        if(_clkAction != null){
            if(_clkAction.indexOf("Go") != -1)
                _actionPick = ACTION_GO;
            else if (_clkAction.indexOf("Next") != -1)
                _actionPick = ACTION_NEXT;
            else if (_clkAction.indexOf("Prev") != -1)
                _actionPick = ACTION_PREV;
            else if (_clkAction.indexOf("First") != -1)
                _actionPick = ACTION_FIRST;
            else if (_clkAction.indexOf("Last") != -1)
                _actionPick = ACTION_LAST;
            else
                _actionPick = ACTION_INITIAL_LOAD;
        }

    }


    public void retrieveSessionData(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        if (session.getAttribute("pageName") != null)
            _sessPageName = (String) session.getAttribute("pageName");
        _empBean.setPageName(_sessPageName);
    }



    public void retrieveSubmitData(HttpServletRequest request,HttpServletResponse response)
    {

        _clkAction =  request.getParameter("action");

        String _tempString = new String();

        _tempString = request.getParameter("actionLink");

        this.determineAction();

        _tempString = request.getParameter("noRowsOnPage");
        if (_tempString != null)
            _noRowsOnPage = Integer.valueOf(_tempString).intValue();
        else
            _noRowsOnPage = 0;


        _tempString = request.getParameter("beginRowNumOnPage");
        if (_tempString != null)
            _beginRowNumOnPage = Integer.valueOf(_tempString).intValue();
        else
            _beginRowNumOnPage = 0;


        _tempString = request.getParameter("endRowNumOnPage");
        if (_tempString != null)
            _endRowNumOnPage = Integer.valueOf(_tempString).intValue();
        else
            _endRowNumOnPage = 0;

        _pageName = request.getParameter("pageName");

        if (_actionPick == ACTION_GO || _actionPick == ACTION_PREV || _actionPick == ACTION_NEXT || _actionPick == ACTION_FIRST
                || _actionPick == ACTION_LAST)
        {
            _hdTxnType = request.getParameter("hdTxnType");
            _cbEmployee = request.getParameter("cbEmployee");
            _cbSort = request.getParameter("cbSort");
            _tbSearchVar = request.getParameter("tbSearchVar").trim();

        }

        if (_actionPick == ACTION_INITIAL_LOAD) { 
            _cbEmployee = new String("1");
            _cbSort = new String("1");
        }

    }

    public void populateModelBean()
    {
        _empBean.setTxnType(_hdTxnType);
        _empBean.setSelectedEmployee(_cbEmployee);
        _empBean.setSearchVar(_tbSearchVar);
        _empBean.setSelectedSort(_cbSort);
        _empBean.setActionPick(_actionPick);
        _empBean.setPageName(_sessPageName);
    }


    public boolean initialPage()
    {
        if(_actionPick == ACTION_INITIAL_LOAD)
            return true;
        else
            return false;
    }


    public void dispatchToJSP(EmployeeServlet employeeServlet, HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        RequestDispatcher dispatcher = null;
        // Put the object into request object to pass to JSP
        request.setAttribute("empBean", _empBean);
        if (_pageName == null || _pageName.trim().length() == 0 || _pageName.equals("Employee")){
            dispatcher = employeeServlet.getServletContext().getRequestDispatcher("/EmployeeList.jsp");
        }
            dispatcher.include(request, response);
    }

    public void processActions()
    {
        switch(_actionPick)
        {
            case ACTION_INITIAL_LOAD:
                    _empBean.getList(FIRST, 0, 0, _pageName,
                        _userProfile.getUserId().trim());

            case ACTION_GO:
                    _empBean.setSearchVar(_tbSearchVar);
                    _empBean.setSelectedEmployee(_cbEmployee);
                    _empBean.setSelectedSort(_cbSort);
                    _empBean.clearAllDtlItem();
                    _empBean.getList(FIRST, 0, 0, _pageName,
                        _userProfile.getUserId().trim());
                break;

            case ACTION_PREV:
                    _empBean.setSearchVar(_tbSearchVar);
                    _empBean.setSelectedEmployee(_cbEmployee);
                    _empBean.setSelectedSort(_cbSort);
                    _empBean.clearAllDtlItem();
                    _empBean.getList(PREV, _beginRowNumOnPage,
                    _endRowNumOnPage, _pageName, _userProfile.getUserId().trim());
                break;

            case ACTION_NEXT:
                    _empBean.setSearchVar(_tbSearchVar);
                    _empBean.setSelectedEmployee(_cbEmployee);
                    _empBean.setSelectedSort(_cbSort);
                    _empBean.clearAllDtlItem();
                    _empBean.getList(NEXT, _beginRowNumOnPage,
                    _endRowNumOnPage, _pageName, _userProfile.getUserId().trim());
                break;

            case ACTION_FIRST:
                    _empBean.setSearchVar(_tbSearchVar);
                    _empBean.setSelectedEmployee(_cbEmployee);
                    _empBean.setSelectedSort(_cbSort);
                    _empBean.clearAllDtlItem();
                    _empBean.getList(FIRST, _beginRowNumOnPage,
                    _endRowNumOnPage, _pageName, _userProfile.getUserId().trim());
                break;

            case ACTION_LAST:
                    _empBean.setSearchVar(_tbSearchVar);
                    _empBean.setSelectedEmployee(_cbEmployee);
                    _empBean.setSelectedSort(_cbSort);
                    _empBean.clearAllDtlItem();
                    _empBean.getList(LAST, _beginRowNumOnPage,
                    _endRowNumOnPage, _pageName, _userProfile.getUserId().trim());
                break;

            default:

                break;
        }

I have EmployeeBean where I get all the data

public java.util.Vector getList(int mode, int firstRow, int lastRow, String pageName, String userID) {

        java.util.Vector myEmployeeList = new java.util.Vector();
        StringBuffer sql = new StringBuffer();

        DbUtil db = null;
        ResultSet rset = null;

        int rowsToBeFeched = MAX_NUM_ROWS_IN_PAGE;
        int preserveFirstRow = 0;
        int preserveLastRow = 0;

        try
        {
            db = new DbUtil();

            sql.append("SELECT id_no, name, email ");
                if ((this.getSelectedColumn() != null)&& (this.getSelectedColumn().equals("1"))){
                    sql.append(" from employee ");
                }

                if ((this.getSelectedColumn() != null)&& (this.getSelectedColumn().equals("2"))){
                    sql.append(" from employee ");
                    sql.append(" where id_no = '");
                    sql.append(this.getSearchVar());
                    sql.append("'");
                }

            if (this.getSelectedSort() != null) {
                if(this.getSelectedSort().equals("1"))
                    sql.append(" ORDER BY id_no");
                else if(this.getSelectedSort().equals("2"))
                    sql.append(" ORDER BY name");
                else
                    sql.append(" ORDER BY id_no");
            } else
                sql.append(" ORDER BY id_no");

            rset =  db.executeQuery(sql.toString(),true);

            rset.last();
            int _totalRows = rset.getRow();

            this.setTotalRowCount(_totalRows);

            rowsToBeFeched = MAX_NUM_ROWS_IN_PAGE;


            if (firstRow < rowsToBeFeched) {
                preserveFirstRow = 0;
            } else {
                preserveFirstRow = firstRow - rowsToBeFeched;
            }

            if (lastRow < 2 * rowsToBeFeched) {
                preserveLastRow = rowsToBeFeched;
            } else {
                preserveLastRow = lastRow - rowsToBeFeched;
            }

                rowsToBeFeched = _totalRows;

            rset.beforeFirst();
            if (mode == FIRST) {
                this.setBeginRowNumOnPage(1);
            } else if (mode == LAST) {
                if (_totalRows <= rowsToBeFeched)
                    this.setBeginRowNumOnPage(1);
                else {
                    rset.last();
                    rset.relative(0 - rowsToBeFeched);
                    this.setBeginRowNumOnPage(rset.getRow() + 1);
                }
            } else if (mode == NEXT) {
                if (_totalRows <= rowsToBeFeched)
                    this.setBeginRowNumOnPage(1);
            else {
                rset.absolute(lastRow);
                    this.setBeginRowNumOnPage(rset.getRow() + 1);
                }
                } else if (mode == PREV) {
                if (firstRow <= (rowsToBeFeched + 1)) {
                    this.setBeginRowNumOnPage(1);
                    rset.beforeFirst();
                } else {
                    rset.absolute(firstRow - rowsToBeFeched - 1);
                    this.setBeginRowNumOnPage(rset.getRow() + 1);
          开发者_JAVA百科      }
            }

            clearAllDtlItem();

            if (rset != null) {
                int i = 0;
                while(rset.next() && i < rowsToBeFeched) {
                    EmployeeBean myEmployee = new EmployeeBean();
                    myEmployee.setIdNo(rset.getString(1).trim());
                    myEmployee.setName(rset.getString(2));
                    myEmployee.setEmail(rset.getString(3));

                    myEmployeeList.addElement(myEmployee);
                }

                Enumeration e = myEmployeeList.elements();
                    while (e.hasMoreElements()) {
                    EmployeeBean nextEmployee = (EmployeeBean) e.nextElement();
                    this.addDtlItem(nextEmployee.getIdNo(), nextEmployee.getName(), nextEmployee.getEmail());
                    }
                i++;
            }

            setEndRowNumOnPage(getBeginRowNumOnPage() + getNbrDtlItems() - 1);

                if (preserveFirstRow == 0) {
                    mode = FIRST;
                } else {
                    mode = NEXT;
                }
            getList( mode, preserveFirstRow, preserveLastRow, "Employee", userID);
        }


public void addDtlItem(String idNo, String name, String email )
    {
        EmployeeItemBean detailItem = new EmployeeItemBean(idNo, name, email );
        _dtlListArray.add(detailItem);

    }


Consider using Java Based Display Tag library.

It makes setting up tables, pagination and sorting really easy.

You can learn more about it here http://www.displaytag.org/1.2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜