改ページラベルのカスタマイズ
5700 ワード
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class FromPageTag extends TagSupport {
private static final long serialVersionUID = -3662095604913922414L;
private int pageNo;//
private int pageSize;//
private int totalRecord;//
@Override
public int doStartTag() throws JspException {
int tag = totalRecord % pageSize;
if (tag > 0) {
tag = 1;
}
int endPageNo = totalRecord / pageSize + tag;
StringBuilder sb = new StringBuilder(
"<div class=\"dataTables_wrapper no-footer\"><div class=\"dataTables_paginate paging_simple_numbers\">");
//
sb.append("<a class=\"paginate_button previous disabled\" ");
if (pageNo > 1) {
sb.append("onclick=\"_turnPage(").append(pageNo - 1).append(")\"");
}
sb.append("> </a>");
//
sb.append(addPageCell(1));
if (endPageNo > 1) {
sb.append(addPageCell(2));
}
if (endPageNo > 2) {
sb.append(addPageCell(3));
}
if (pageNo > 5) {
sb.append("<span>…</span>");
}
//
for (int i = pageNo - 1; i <= pageNo + 1; i++) {
if (i <= 3) {
continue;
}
if (i >= endPageNo - 2) {
continue;
}
sb.append(addPageCell(i));
}
if (pageNo < endPageNo - 3) {
sb.append("<span>…</span>");
}
//
if (endPageNo - 2 > 3) {
sb.append(addPageCell(endPageNo - 2));
}
if (endPageNo - 1 > 3) {
sb.append(addPageCell(endPageNo - 1));
}
if(endPageNo > 3) {
sb.append(addPageCell(endPageNo));
}
//
sb.append("<a class=\"paginate_button next disabled\"");
if(pageNo < endPageNo) {
sb.append("onclick=\"_turnPage(").append(pageNo+1).append(")\"");;
}
sb.append("> </a></div>");
try {
if (sb != null) {
JspWriter out = pageContext.getOut();
out.print(sb);
}
} catch (Exception e) {
throw new JspException(" ");
}
return super.doStartTag();
}
private String addPageCell(int number) {
StringBuilder sb = new StringBuilder();
sb.append("<span><a class=\"paginate_button ");
if (pageNo != number) {
sb.append("\" onclick=\"_turnPage(").append(number).append(")\"");
} else {
sb.append(" current\"");
}
sb.append(">").append(number).append("</a></span>");
return sb.toString();
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
2.WEB-INF/tlds内にformPageを追加する.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version><!-- -->
<jsp-version>1.2</jsp-version><!-- JSP -->
<short-name>mytag</short-name><!-- JSP -->
<tag>
<name>formPage</name><!-- -->
<tag-class>com.common.FromPageTag</tag-class><!-- -->
<body-content>empty</body-content><!-- -->
<!-- -->
<attribute>
<name>pageNo</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pageSize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>totalRecord</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3. web.xmlで定義
<jsp-config>
<taglib>
<taglib-uri>/tld/page</taglib-uri>
<taglib-location>/WEB-INF/tlds/formPage.tld</taglib-location>
</taglib>
</jsp-config>
4.使用
<%@ taglib uri="/tld/page" prefix="myTag" %>
<myTag:formPage pageSize="${pageSize }" pageNo="${pageNo }" totalRecord="${totalRecord }"/>
<script type="text/javascript">
function _turnPage(pageNo) {
document.getElementById("pageNo").value=pageNo;
document.getElementById("pageSize").value=20;
document.getElementById("myForm").submit();
}
</script>