小妹急求解决问题,哪位高手帮忙解决一下?比如能在一行显示3条记录,然后再换行,有分页的列表

kaqia2003 2007-09-03 04:28:47
我在代码如下:

int total=list.size();
int count=0;
Iterator it=list.iterator();
CoolWebInfo coolweb;


while(count<(page-1)*10&&it.hasNext()){//分页显示,每页显示10条记录
it.next();
count++;
}
count=0;
while(it.hasNext()){
coolweb=(CoolWebInfo)it.next();
coolweb_id=coolweb.getCoolweb_id();
coolweb_name=coolweb.getCoolweb_name();
coolweb_type=coolweb.getCoolweb_type();
coolweb_http=coolweb.getCoolweb_http();
coolweb_date=coolweb.getCoolweb_date();
coolweb_address=coolweb.getCoolweb_address();
out.println<"我要输出的内容">");
count++;

}

以上是我的代码,但是只能以列循环,不能以行循环。

各位大哥,帮帮小妹,急解决!

...全文
353 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZXEOC 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 kaqia2003 的帖子:]
我在代码如下:

int total=list.size();
int count=0;
Iterator it=list.iterator();
CoolWebInfo coolweb;


while(count <(page-1)*10&&it.hasNext()){//分页显示,每页显示10条记录
it.next();
count++;
}
count=0;
while(it.hasNext()){
coolweb=(CoolWebInfo)it.next();
coolweb_id=coolweb.getCoolweb_id();
coolweb_name=coolweb.getCoolweb_name();
coolweb_type=coolweb.getCoolweb_type();

[/Quote]
你的意思是你现在这个版本分页和显示都没问题,就差“每行显示3条”这个了?如果这样的话,把println改成print,然后每3个输出一个换行不就行了
huanga52 2008-09-18
  • 打赏
  • 举报
回复
你太厉害了!
dijunjun 2008-09-13
  • 打赏
  • 举报
回复
……这女的太强了
feeyon 2008-09-12
  • 打赏
  • 举报
回复
看晕了, 如果将从数据库里取出来的数据分页显示而不是一次性显示,毕竟一次取10条或20天,太麻烦数据库了.
需要写bean PageRows.java, bean里有两个属性:
Set<Integer,Object> pageRows;
String pageNav;

JSP:显示分页面导航时, ${PageRows.pageNav}, 留给JSP的只有这一句. 掌握JSP技术其实不如不会JSP.
因为看JSP会晕倒, 更不利于做成JAR.

action里 有最好只有四句,不含大括号: 包名,类名,方法名,语句

如果不习惯写JAVA BEAN, 写代码时,强迫自己不用页面测试,写一次就能改变习惯.

TaiNo 2007-09-05
  • 打赏
  • 举报
回复
应该是可以行循环,不能列循环吧
hanchuang 2007-09-05
  • 打赏
  • 举报
回复
请回答问题的大哥能不能把问题看清楚了在回答.谢谢
tomiehu 2007-09-05
  • 打赏
  • 举报
回复
<%@page contentType="text/html; charset=GBK"
import="java.sql.*,java.util.*,DBUtil.DBConnection"%>
<html>
<head>
<title>showInfo</title>
</head>
<body bgcolor="#ffffff">
<%
//获得session中的用户名和密码
String username = (String) request.getSession().getAttribute("username");
String password = (String) request.getSession().getAttribute("password");
//如果session中的用户名和密码为空就重定向向到login.html页面
if (username == null || "".equals(username) ||
password == null || "".equals(password)) {
response.sendRedirect("login.html");
}
//如果session中的用户名和密码不为空就进行以下操作
else {
//获得数据库的连接
Connection conn = DBUtil.DBConnection.getConn();
Statement sm = conn.createStatement();
//查找所有用户的信息
String sql = "select * from jobs";
ResultSet rs = sm.executeQuery(sql);
//声明了一个ArrayList
ArrayList users = new ArrayList();
//将从数据库中取出的数据经过包装成Student后放到ArrayList中
while (rs.next()) {
String username1 = rs.getString(2);
String password1 = rs.getString(3);
bean.Student stu = new bean.Student();
stu.setUsername(username1);
stu.setPassword(password1);
users.add(stu);
}
//将ArrayList的对象放到session中
request.getSession().setAttribute("users", users);
}
%>
<%
//取得session中的ArrayList对象
ArrayList us = (ArrayList) request.getSession().getAttribute("users");
//定义的记录总条数,初始化为0
int sum = 0;
//定义的每页显示的条数,初始化为5
int lenPerPage = 5;
//定义的最后一页剩下的记录数,初试化为0
int other = 0;
//定义的整除总页数,初试化为0
int pages = 0;
if (us != null) {
//重新对以上属性进行赋值
sum = us.size();
other = sum % lenPerPage;
pages = sum / lenPerPage;
//得到用户想显示的当前页码
String thePage = (String) request.getParameter("thePage");
//如果用户没有选择第几页默认显示第一页
if (thePage == null || "".equals(thePage)) {
//如果总记录数比每页显示的数目少,就将记录全部显示。
if (sum <= lenPerPage) {
for (int i = 0; i < sum; i++) {
bean.Student student = (bean.Student) us.get(i);
out.println("username=" + student.getUsername());
out.println("password=" + student.getPassword());
out.println("<br>");
}
}
//如果总记录数比每页显示的数目多,就将显示第一页的记录
else {
for (int i = 0; i < lenPerPage; i++) {
bean.Student student = (bean.Student) us.get(i);
out.println("username=" + student.getUsername());
out.println("password=" + student.getPassword());
out.println("<br>");
}
}
}
//如果用户选择了显示的页数就显示当前的页
else {
Integer crrentPage = new Integer(thePage);
//如果当前选择的是显示的最后一页,就将整除每页显示数量余下的记录显示出来
if (crrentPage.intValue() > pages) {
for (int i = sum - other - 1; i < sum; i++) {
bean.Student student = (bean.Student) us.get(i);
out.println("username=" + student.getUsername());
out.println("password=" + student.getPassword());
out.println("<br>");
}
}
//如果不是最后一页就将当前选定页的记录显示出来
else {
for (int i = (lenPerPage * crrentPage.intValue()) - lenPerPage; i < lenPerPage * crrentPage.intValue(); i++) {
bean.Student student = (bean.Student) us.get(i);
out.println("username=" + student.getUsername());
out.println("password=" + student.getPassword());
out.println("<br>");
}
}
}
//默认为第一页
if (thePage == null || "".equals(thePage)) {
thePage = "1";
}
//显示分页的页数。
for (int i = 1; i < pages + 1; i++) {
if (i == new Integer(thePage).intValue()) {
out.print(i);
out.print("  ");
continue;
}
out.print("<a href='showInfo.jsp?thePage=" + i + "'>" + i + "</a>");
out.print("  ");
}
}
//如果 other = sum % lenPerPage不为0 ,就在pages+1才为真正的总页数
if (other != 0) {
int thispage = pages + 1;
out.print("<a href='showInfo.jsp?thePage=" + thispage + "'>" + thispage + "</a>");
}
%>
</body>
</html>
kaqia2003 2007-09-04
  • 打赏
  • 举报
回复
请帅哥帮我解决啊,谢谢了!
kaqia2003 2007-09-04
  • 打赏
  • 举报
回复
请问楼上的帅哥什么意思displaytag……
一板砖夯死你 2007-09-04
  • 打赏
  • 举报
回复
displaytag……
kaqia2003 2007-09-04
  • 打赏
  • 举报
回复
这个大哥是不是写错了,coolweb[]=(CoolWebInfo[])it.next();这里面的coolweb[]还要去创建这个coolweb类啊!
TaiNo 2007-09-03
  • 打赏
  • 举报
回复
while(it.hasNext()count<10){
coolweb=(CoolWebInfo)it.next();
coolweb[]=(CoolWebInfo[])it.next();
coolweb_id=coolweb[count].getCoolweb_id();
coolweb_name=coolweb[count].getCoolweb_name();
coolweb_type=coolweb[count].getCoolweb_type();
coolweb_http=coolweb[count].getCoolweb_http();
coolweb_date=coolweb[count].getCoolweb_date();
coolweb_address=coolweb[count].getCoolweb_address();
out.println<"我要输出的内容">");
count++;
}
kaqia2003 2007-09-03
  • 打赏
  • 举报
回复
各位大哥,帮忙看一下,如果今天解决不了,小妹就得加班了!
kaqia2003 2007-09-03
  • 打赏
  • 举报
回复
public class CoolWebList {


public CoolWebDAO coolWebDao;

public CoolWebDAO getCoolWebDao(){


return DAOFactory.getInstance().createCoolWebDAO();
}

public void list(HttpServletRequest request,JspWriter out)throws Exception{

String tmp=(String)request.getParameter("page");
//如果第一次打开PAGE为空
if(tmp!=null){

listpage(request,out,Integer.parseInt(tmp));

}else{
//首次调用,page值为空,所以默认值1传入
listpage(request,out,1);
}


}


private void listpage(HttpServletRequest request, JspWriter out, int page) throws Exception {
// TODO 自动生成方法存根

int coolweb_id;
String coolweb_name="",coolweb_type="",coolweb_http="",coolweb_address="";
Date coolweb_date;

coolWebDao=this.getCoolWebDao();
List list=coolWebDao.getCoolWebList();

if(list==null){
out.print("尚无信息");
return;
}


int total=list.size();
int count=0;
Iterator it=list.iterator();
CoolWebInfo coolweb;

int total1;
if(total%10==0){
total1=total/10;
}else{
total1=total/10+1;
}
while(count<(page-1)*10&&it.hasNext()){//分页显示,每页显示10条记录
it.next();
count++;
}
count=0;
while(it.hasNext()count<10){
coolweb=(CoolWebInfo)it.next();
coolweb_id=coolweb.getCoolweb_id();
coolweb_name=coolweb.getCoolweb_name();
coolweb_type=coolweb.getCoolweb_type();
coolweb_http=coolweb.getCoolweb_http();
coolweb_date=coolweb.getCoolweb_date();
coolweb_address=coolweb.getCoolweb_address();
out.println<"我要输出的内容">");
count++;

}



out.print("<tr><td colspan=5 align=center>");

out.print("<table width=100% border=0 cellspacing=0><tr>");

out.print("<td height='26'>    共"+total+"条     ");
out.print("共"+total1+"页    </td>");
if(page>1)
out.print("<td align='right'><a href=newslist.jsp?page=1>首页</a>    ");
else
out.print("<td align='right'>首页    ");
if ((page - 1) > 0)
out.print("<a href=newslist.jsp?page="+ (page - 1) + ">上一页</a>    ");
else
out.print("上一页    ");
if ((page - 1) < (total / 10))
out.print("<a href=newslist.jsp?page="+ (page + 1) + ">下一页</a>    ");
else
out.print("下一页    ");
if(page>(total / 10))
out.print("尾页    </td>");
else
out.print("<a href=newslist.jsp?page="+total1+ ">尾页</a>    </td>");


out.print("</td></tr></table>");


}

}
fanchanghu 2007-09-03
  • 打赏
  • 举报
回复
ms代码没有发完全啊
就你发的这些代码
不能实现分页
而且会报一个count没有在使用之前定义的错误~~

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧