pagehelper.java~2~
来自「hibernate+spring的相片上传项目」· JAVA~2~ 代码 · 共 387 行 · 第 1/2 页
JAVA~2~
387 行
pageBarBuffer.append(" ");
// 首页
StringBuffer firstPageBuffer = new StringBuffer();
if (totalPage > 1 && page != 1) {
firstPageBuffer.append(url + "?page=1");
if (isHaveParam) {
firstPageBuffer.append("&" + param);
}
display1 = true;
}
// 上一页
StringBuffer perPageBuffer = new StringBuffer();
if (page > 1) {
perPageBuffer.append(url + "?page=" + (page - 1));
if (isHaveParam) {
perPageBuffer.append("&" + param);
}
display2 = true;
}
// 下一页
StringBuffer nextPageBuffer = new StringBuffer();
if (page < totalPage) {
nextPageBuffer.append(url + "?page=" + (page + 1));
if (isHaveParam) {
nextPageBuffer.append("&" + param);
}
display3 = true;
}
// 最后一页
StringBuffer lastPageBuffer = new StringBuffer();
if (totalPage > 1 && page < totalPage) {
lastPageBuffer.append(url + "?page=" + totalPage);
if (isHaveParam) {
lastPageBuffer.append("&" + param);
}
display4 = true;
}
// 每页显示记录数
StringBuffer numPageBuffer = new StringBuffer();
numPageBuffer.append("<select name=\"perPage\" ");
numPageBuffer.append(" onchange=\"");
numPageBuffer.append(" if(this.options[this.selectedIndex]");
numPageBuffer.append(".value!=''){");
numPageBuffer.append(" location='" + url + "?");
if (isHaveParam) {
numPageBuffer.append(param + "&");
}
numPageBuffer.append("page=1");
numPageBuffer.append("&perPage='+this.options[this.selectedIndex]");
numPageBuffer.append(".value;}\">");
for (int i = 1; i <= cNum; i++) {
numPageBuffer.append("<option value=\"" + (i * 10) + "\"");
if ((i * 10) == perPage) {
numPageBuffer.append(" selected>" + (i * 10) + "</option>");
} else {
numPageBuffer.append(">" + (i * 10) + "</option>");
}
}
numPageBuffer.append("</select>");
// 跳转到第几页
StringBuffer jumpPageBuffer = new StringBuffer();
jumpPageBuffer.append("<select name=\"pages\"");
jumpPageBuffer.append(" onchange=\"");
jumpPageBuffer.append(" if(this.options[this.selectedIndex]");
jumpPageBuffer.append(".value!=''){location='" + url + "?");
if (isHaveParam) {
jumpPageBuffer.append(param + "&");
}
jumpPageBuffer.append("page='+this.options[this.selectedIndex]");
jumpPageBuffer.append(".value;}\">");
for (int i = 1; i <= totalPage; i++) {
jumpPageBuffer.append("<option value=\"" + i + "\"");
if (i == page) {
jumpPageBuffer.append("selected");
}
jumpPageBuffer.append(">" + i + "</option>");
}
jumpPageBuffer.append("</select>");
Configuration cfg = new Configuration();
Template newsTemplate = null;
StringWriter out = new StringWriter();
try {
cfg.setDirectoryForTemplateLoading(new File(realPath
+ pageObject.getPageTemplatePath()));
cfg.setObjectWrapper(new DefaultObjectWrapper());
/**
* 因为本项目所有页面均为UTF-8编码,故此采用UTF-8, 如果页面编码为GBK,此处也需要改成GBK
*/
newsTemplate = cfg.getTemplate(pageTemplate, "UTF-8");
/**
* 以下主要是进行数据填充,将数据填充到模型中
*/
Map root = new HashMap();
root.put("total", String.valueOf(total));
root.put("page", String.valueOf(page));
root.put("allPage", String.valueOf(totalPage));
if (display1) {
root.put("firstPageStart", "<a href=\""
+ firstPageBuffer.toString() + "\">");
root.put("firstPageEnd", "</a>");
root.put("firstStyle", "");
} else {
root.put("firstStyle", imgStyle);
root.put("firstPageStart", "");
root.put("firstPageEnd", "");
}
if (display2) {
root.put("prevPageStart", "<a href=\""
+ perPageBuffer.toString() + "\">");
root.put("prevPageEnd", "</a>");
root.put("prevStyle", "");
} else {
root.put("prevStyle", imgStyle);
root.put("prevPageStart", "");
root.put("prevPageEnd", "");
}
if (display3) {
root.put("nextPageStart", "<a href=\""
+ nextPageBuffer.toString() + "\">");
root.put("nextPageEnd", "</a>");
root.put("nextStyle", "");
} else {
root.put("nextStyle", imgStyle);
root.put("nextPageStart", "");
root.put("nextPageEnd", "");
}
if (display4) {
root.put("lastPageStart", "<a href=\""
+ lastPageBuffer.toString() + "\">");
root.put("lastPageEnd", "</a>");
root.put("lastStyle", "");
} else {
root.put("lastStyle", imgStyle);
root.put("lastPageStart", "");
root.put("lastPageEnd", "");
}
if (isPrevPageNumStyle) {
root.put("prevPageNumStyle", imgStyle);
} else {
root.put("prevPageNumStyle", "");
}
if (isNextPageNumStyle) {
root.put("nextPageNumStyle", imgStyle);
} else {
root.put("nextPageNumStyle", "");
}
root.put("prevPageNumStart", prevPageNumStart.toString());
root.put("prevPageNumEnd", prevPageNumEnd.toString());
root.put("nextPageNumStart", nextPageNumStart.toString());
root.put("nextPageNumEnd", nextPageNumEnd.toString());
root.put("pagePrevPage", pageBarBuffer.toString());
root.put("pageBar", pageBarBuffer.toString());
root.put("numPage", numPageBuffer.toString());
root.put("jumpPage", jumpPageBuffer.toString());
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return out.toString();
}
public int sumPage(int total, int perPage) {
int totalPage = (total + perPage - 1) / perPage;
return totalPage;
}
public int getCpage(int total, int perPage, int cPage) {
if (cPage - sumPage(total, perPage) == 1) {
cPage--;
}
if (cPage - sumPage(total, perPage) > 1 || cPage < 1) {
cPage = 1;
}
return cPage;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?