Struts中如何实现查询结果分页显示
messagelist.do - > messagelist.jsp用 messagelist.do?start=0 来显示第一个页面
参数说明:http://www.
list:信息列表
start:开始位置
page:每页显示的信息数目
previous:上页开始位置
next:下页开始位置
MessageListAction 里的 perform 是这样写的:
------------------------------------------------------------------------------------
int start = Integer.parseInt(request.getParameter("start"));
System.out.println ("Start:" + start);
int page = 4;
// create a message list
Message[] list = new Message[16];
for (int i=0; i< list.length; i++){
list[i] = new Message(i ,"message" + i);
}
request.setAttribute("list", list);
request.setAttribute("start", new Integer(start));
request.setAttribute("page", new Integer(page));
// if there is a previous page, set the previous variable
int previous = start-page;
if ( previous >=0 ){
request.setAttribute("previous", new Integer(previous));
System.out.println ("previous:" + previous);
}
// if there is a next page, set the next variable
int next = start+page;
if ( next< list.length ){
request.setAttribute("next", new Integer(next));
System.out.println ("next:" + next);
}
return (mapping.findForward("success"));
------------------------------------------------------------------------------------
下面是 messagelist.jsp 里的内容:http://www.
------------------------------------------------------------------------------------
< logic:iterate id="message" name="list" offset="start" length="page" >
< br >< html:link
paramId="id" paramName="message" paramProperty="id"
page="/messagedetail.do" >
< bean:write name="message" property="name" / >
< /html:link >
< /logic:iterate >
< logic:present name="previous" >
< br >< html:link
paramId="start" paramName="previous"
page="/messagelist.do" >
上一页
< /html:link >
< /logic:present >
< logic:present name="next" >
< br >< html:link
paramId="start" paramName="next"
page="/messagelist.do" >
下一页
< /html:link >
< /logic:present >
http://www.
http://www.
------------------------------------------------------------------------------------
以此例子来说明 logic:iterate 在分页里的用法
由于每次 messagelist.do 都会处理所有的 message,所以性能上还是可以再优化
比如把 message 数组存放在 session 里。