showlist.do - > showlist.jsp
用 showlist.do?start=0 来显示第一个页面
参数说明:
list: 数据(从数据里查询出来的数据)
start:开始位置
page:每页显示的信息数目
previous:上页开始位置
next:下页开始位置
ShowListAction 里的 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"));
---------------------------------------------------------------------------
下面是 showlist.jsp 里的内容:
---------------------------------------------------------------------------
< 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="/showlist.do" >
上一页
< /html:link >
< /logic:present >
< logic:present name="next" >
< br >< html:link
paramId="start" paramName="next"
page="/messagelist.do" >
下一页
< /html:link >
< /logic:present >
---------------------------------------------------------------------------
以此例子是 logic:iterate 在分页里的用法
由于每次 showlist.do 都会处理所有的 message,所以性能上还是可以再优化
比如把 message 数组存放在 session 里。