黑马程序员技术交流社区
标题:
有一个java程序有小小问题?
[打印本页]
作者:
小文文化
时间:
2017-10-11 15:20
标题:
有一个java程序有小小问题?
我有以下几个程序:
以下是getallcustomer.jsp程序:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'getallcustomer.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body style="text-align:center;">
<table frame="border">
<tr>
<th>客户姓名</th>
<th>客户性别</th>
<th>客户年龄</th>
<th>客户联系电话</th>
<th>客户爱好</th>
<th>客户类别</th>
</tr>
<c:forEach var="ll" items="${requestScope.cpb.list}">
<tr>
<td>${ll.name}</td>
<td>${ll.gender}</td>
<td>${ll.age}</td>
<td>${ll.cellphone}</td>
<td>${ll.live}</td>
<td>${ll.type}</td>
</tr>
</c:forEach>
</table>
<br><br>
共${cpb.totalrecord}条记录,每页${cpb.pagesize}条,共${cpb.totalpage}页,当前第${cpb.currentpage}页
<a href="">上一页</a>
<c:forEach var="pagenum" items="${cpb.pagebar}">
<a href="">${pagenum}</a>
</c:forEach>
<a href="">下一页</a>
</body>
</html>
以下是getallservlet.java程序:
package ccon;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.customer;
import bean.pagebean;
import bean.queryinfo;
import service.customerser;
import service.customerserimpl;
import utils.webutils;
public class getallservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
queryinfo qi = webutils.rtob(request, queryinfo.class);
customerser cus = new customerserimpl();
pagebean cpb = cus.pagequery(qi);
System.out.println("Hello");
System.out.println(cpb.getPagebar().length);
for(int i=0;i<cpb.getPagebar().length;++i)
{
System.out.println(cpb.getPagebar()[i]);
}
request.setAttribute("cpb", cpb);
request.getRequestDispatcher("/WEB-INF/page/getallcustomer.jsp").forward(request, response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
以下是pagebeen.java程序:
package bean;
import java.util.List;
public class pagebean {
private List list;
private int totalrecord;
private int pagesize;
private int totalpage;
private int currentpage;
private int prepage;
private int nextpage;
private int[] pagebar;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotalpage() {
if(this.totalrecord%this.pagesize==0)
{
this.totalpage=this.totalrecord/this.pagesize;
}
else
{
this.totalpage=this.totalrecord/this.pagesize+1;
}
return this.totalpage;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getPrepage() {
this.prepage = this.currentpage-1;
if(this.prepage<1)
{
this.prepage=1;
}
return this.prepage;
}
public int getNextpage() {
this.nextpage = this.currentpage+1;
if(this.nextpage>this.totalpage)
{
this.nextpage=this.totalpage;
}
return this.nextpage;
}
public int[] getPagebar() {
int[] pb=new int[this.totalpage];
for(int i=1;i<=this.totalpage;++i)
{
pb[i-1]=i;
}
this.pagebar=pb;
/*int[] pb=new int[this.getTotalpage()];
for(int i=1;i<=this.getTotalpage();++i)
{
pb[i-1]=i;
}
this.pagebar=pb;*/
return pagebar;
}
}
剩下的程序回答者可以自己加入,我不在增加程序了
我现在问题是:
我在getallcustomer.jsp的输出结果完全没有问题了,但在getallservlet.java中的System.out.println(cpb.getPagebar().length);
for(int i=0;i<cpb.getPagebar().length;++i)
{
System.out.println(cpb.getPagebar()[i]);
}
即在myeclipse控制台中输出是:
Hello
0
但我想在myeclipse控制台中输出的正确结果是:
Hello
3
1
2
3
但我把pagebean.java中的getPagebar方法修改为以下就可以在myeclipse控制台中输出的正确结果了,
public int[] getPagebar() {
/*int[] pb=new int[this.totalpage];
for(int i=1;i<=this.totalpage;++i)
{
pb[i-1]=i;
}
this.pagebar=pb;*/
int[] pb=new int[this.getTotalpage()];
for(int i=1;i<=this.getTotalpage();++i)
{
pb[i-1]=i;
}
this.pagebar=pb;
return pagebar;
}
请高手们解释一下各中的原因,谢谢!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2