gpt4 book ai didi

JavaBean Servlet Jsp 分页

转载 作者:行者123 更新时间:2023-12-01 05:40:46 25 4
gpt4 key购买 nike

我正在尝试将数据显示到多个页面中。我不确定我在这里做错了什么。有人可以让我知道我在这里做错了什么吗?我可以在一页中获取所有数据,但无法将其放入多页中。谢谢

我的 Employee jsp 页面如下所示

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

我的 servlet 看起来像这样:

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

我的employeeWebBean看起来像这样,所有处理都发生在其中:

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

我有 EmployeeBean,我可以在其中获取所有数据

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);
}
}

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

}

最佳答案

考虑使用基于 Java 的显示标签库。

它使得设置表格、分页和排序变得非常简单。

您可以在这里了解更多信息http://www.displaytag.org/1.2/

关于JavaBean Servlet Jsp 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7234408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com