StringPagination类代码为:
package example;
public class StringPagination {
/** *//** 当前页码数 */
private int currentPage = 1;
/** *//** 需要分页的长字符串 */
private String text;
/** *//** 每页显示字符数, 默认为 200 */
private int sizePerPage = Constants.TEXT_PAGE_SIZE_DEFALUT;
/** *//** 总页数 */
private int totalPage;
public StringPagination() {}
/** *//**
* 返回当前页的字符串.
* @return
*/
public String getCurrentPagedText() {
try {
if(getCurrentPage() < getTotalPage()) {
return getText().substring((getCurrentPage() - 1) * getSizePerPage(),
getCurrentPage() * getSizePerPage());
} else if(getTotalPage() > 0) {
return getText().substring((getCurrentPage() - 1) * getSizePerPage());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** *//**
* @return Returns the 当前页码数.
*/
public int getCurrentPage() {
if(currentPage <= 0) currentPage = 1;
return currentPage;
}
/** *//**
* 设置当前页码, 从 1 开始.
*
* @param currentPage
* The 当前页码数 to set.
*/
public void setCurrentPage(int currentPage) {
if (currentPage <= 0) {
currentPage = 1;
}
this.currentPage = currentPage;
}
/** *//**
* @return Returns the 总页码数, 如果没有数据, 就返回 1.
*/
public int getTotalPage() {
if(getText() == null) totalPage = 0;
totalPage = (int) Math.ceil(1.0 * getText().length() / getSizePerPage()); // 总页面数
if (totalPage == 0)
totalPage = 1;
return totalPage;
}
/** *//**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/** *//**
* @return Returns the 每页显示字符数.
*/
public int getSizePerPage() {
return sizePerPage;
}
/** *//**
* @param sizePerPage
* The 每页显示字符数 to set.
*/
public void setSizePerPage(int sizePerPage) {
this.sizePerPage = sizePerPage;
}
/** *//**
* @return Returns the 需要分页的长字符串.
*/
public String getText() {
return text;
}
/** *//**
* @param text
* The 需要分页的长字符串 to set.
*/
public void setText(String text) {
this.text = text;
}
public static void main(String[] args) {
StringPagination pager = new StringPagination();
pager.setSizePerPage(5);
pager.setText("12345678901");
pager.setCurrentPage(2);
System.out.println(pager.getTotalPage());
System.out.println(pager.getCurrentPagedText());
}
}
Action代码为:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
StringPagination stringPagination = new StringPagination();
WapResource wapResource = new WapResource();
int curPage; //当前页
int maxPage; // 总页数
String strResPic = "";
String content = "";
String resName = "";
String outputContent = "";
String strResId = request.getParameter("resId");
String strPageNo = request.getParameter("curPage");
try {
content = new String(request.getParameter("content").getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(strPageNo != null){
if(!"1".equals(strPageNo)){
stringPagination.setCurrentPage(Integer.parseInt(strPageNo));
}
}
List wapResourceList = this.getWapResourceDao().getWapResourceByResID(Long.valueOf(strResId));
if(wapResourceList != null){
// String pagination process
wapResource = (WapResource)wapResourceList.get(0);
curPage = stringPagination.getCurrentPage();
strResPic = wapResource.getResPic();
resName = wapResource.getResName();
stringPagination.setText(wapResource.getResContent());
maxPage = stringPagination.getTotalPage();
outputContent = stringPagination.getCurrentPagedText();
request.setAttribute("wapResourceList", wapResourceList);
request.setAttribute("content", content);
request.setAttribute("curPage", String.valueOf(curPage));
request.setAttribute("forward", String.valueOf(stringPagination.getCurrentPage() - 1));
request.setAttribute("back", String.valueOf(stringPagination.getCurrentPage() + 1));
request.setAttribute("maxPage", String.valueOf(maxPage));
request.setAttribute("strResPic", strResPic);
request.setAttribute("resName", resName);
request.setAttribute("strResId", strResId);
request.setAttribute("outputContent", outputContent);
}
return mapping.findForward("searchContent");
} JSP 页面:
<%@ page contentType="text/vnd.wap.wml; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Cache-Control" content="max-age=0" />
</head>
<card title='<bean:message key="prompt.index.title"/>'>
<p align='left'>
<%
int curPage,forward, back; //当前页
int maxPage; // 总页数
String content = (String)request.getAttribute("content");
String strResPic = (String)request.getAttribute("strResPic");
String outputContent = (String)request.getAttribute("outputContent");
String resName = (String)request.getAttribute("resName");
String resId = (String)request.getAttribute("strResId");
curPage = Integer.parseInt(request.getAttribute("curPage").toString());
maxPage = Integer.parseInt(request.getAttribute("maxPage").toString());
forward = Integer.parseInt(request.getAttribute("forward").toString());
back = Integer.parseInt(request.getAttribute("back").toString());
if(null != strResPic){
%>
<img src='<%=strResPic%>' alt='img' />
<br/>
<%
}
out.print(resName);
out.print("<br/><br/>");
out.print(outputContent);
%>
<br/>
<%
if (curPage != 1 && maxPage > 0 ) {
%>
<anchor>上一页
<go href="/cars/searchContent.do" method="post" accept-charset="UTF-8">
<postfield name="curPage" value="<%=forward%>"/>
<postfield name="resId" value="<%=resId%>"/>
<postfield name="content" value="<%=content%>"/>
</go>
</anchor>
<br/>
<%
}; if (curPage != maxPage && maxPage > 0 ) {
%>
<anchor>下一页
<go href="/cars/searchContent.do" method="post" accept-charset="UTF-8">
<postfield name="curPage" value="<%=back%>"/>
<postfield name="resId" value="<%=resId%>"/>
<postfield name="content" value="<%=content%>"/>
</go>
</anchor>
<br/>
<%
}
%>
共<%=maxPage%>页
<br/>
<anchor> 返回上级
<go href="/cars/search.do" method="post" accept-charset="UTF-8">
<postfield name="content" value="<%=content%>"/>
</go>
</anchor>
<br/>
<%@include file="../bottom1_1.jsp"%>
</p>
</card>
</wml>
很简单的,大家看看吧,我也参考了别人的代码……
posted on 2006-02-28 18:37
一凡 阅读(797)
评论(1) 编辑 收藏 所属分类:
JAVA 基础