编写处理类
在web-INF下创建my.tld文件
在web.xml中配置
在jsp中导入标签
[color=red]自定义分页标签的编写[/color]
第一步:
第二步:
编写WEB_INF下tld目录下的paginator.tld
第三步:
引入标签体
第四步:
在jsp页面上
结束!
希望对忘记了的朋友有帮助,也是为了在项目中能迅速copy 谢谢~
package com.oa;
import com.oa.manager.AclManager;
//JSTL函数,完成权限的即时认证
public class SecurityFunction {
private static AclManager aclManager;
public static boolean method(int userId,String sn,int permission) {
return aclManager.hasPermissionByResourceSn(userId, sn, permission);
}
//该方法不能定义为public static ,否则spring无法注入
public void setAclManager(AclManager aclManager) {
SecurityFunction.aclManager = aclManager;
}
}
在web-INF下创建my.tld文件
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<display-name>JSTL functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>my</short-name>
<uri>http://www.oa.com/jsp/jstl/functions</uri>
<function>
<name>method</name>
<function-class>com.oa.SecurityFunction</function-class>
<function-signature>
boolean method(int, java.lang.String, int)
</function-signature>
</function>
</taglib>
在web.xml中配置
<jsp-config>
<taglib>
<taglib-uri>http://www.oa.com/jsp/jstl/functions</taglib-uri>
<taglib-location>/WEB-INF/my.tld</taglib-location>
</taglib>
</jsp-config>
在jsp中导入标签
<%@ taglib prefix="my" uri="/WEB-INF/my.tld" %>
<c:if test="${my:method(id,searchString,p)}">
.......
</c:if>
[color=red]自定义分页标签的编写[/color]
第一步:
/**
*
*/
package com.cmsz.rist.common.page;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
import com.cmsz.rist.init.RistConfig;
/**
* 分页tag,生成分页的跳转标识
*
*
*/
public class PaginatorTag extends TagSupport
{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(PaginatorTag.class);
/**
*
*/
private static final long serialVersionUID = 1L;
private String form;// 页面的form名
private String action;// 跳转的action URL
private String name; // 分页器的名称
private String scope = "request"; // 分页器所在位置page/request/session/application
private static final Map<String, Integer> scopeMap = new HashMap<String, Integer>(); // 属性范围Map
static
{
// 初始化属性范围Map
scopeMap.put("page", PageContext.PAGE_SCOPE);
scopeMap.put("request", PageContext.REQUEST_SCOPE);
scopeMap.put("session", PageContext.SESSION_SCOPE);
scopeMap.put("application", PageContext.APPLICATION_SCOPE);
}
public int doEndTag() throws JspTagException
{
//工程路径
String rootPath = com.cmsz.rist.common.util.SystemValue.ROOT;
String rootPathSp = rootPath.substring(0,rootPath.length()-1);
String path = rootPathSp.substring(rootPathSp.lastIndexOf("\\")+1);
//跳转
//String GO = "\""+((HttpServletRequest) pageContext.getRequest()).getContextPath()+ "/images/GO.gif" + "\"";
//String GO2 = "\""+((HttpServletRequest) pageContext.getRequest()).getContextPath()+ "/images/GO2.gif" + "\"";
Paginator paginator = (Paginator) pageContext.getAttribute(name,
((Integer) scopeMap.get(scope)).intValue());
JspWriter out = pageContext.getOut();
StringBuffer buffer = new StringBuffer(1000);
int totalPage = paginator.getTotalPage();
try
{
if (paginator.getTotalCount() > 0)
{// 有记录
buffer.append("<script language=\"JavaScript\">\n");
buffer.append("<!--\n");
buffer.append("function GotoPage(id) {\n");
buffer.append("var pageTotal =document." + form + ".pageSize.value; \n");
if(action.indexOf('?')>0){
buffer.append("document." + form + ".action='" + action+"&pageTotal='+pageTotal+'&pageNow='+"
+"id"
+ ";\n");
}else{
buffer.append("document." + form + ".action='" + action+"?pageTotal='+pageTotal+'&pageNow='+"
+"id"
+ ";\n");
}
buffer.append("if(id<=0){id=1;}\n");
buffer.append("document." + form + ".pageIndex.value=id;\n");
buffer.append("document." + form + ".submit();\n");
buffer.append("}\n");
buffer.append("function changePage(obj) {\n");
buffer.append("var id = obj.value;\n");
/*buffer.append("var ar = obj.options;\n");
buffer.append("for(var i=0;i<ar.length;i++){\n");
buffer.append("if(id==ar[i].value){\n");
buffer.append("ar[i].selected=true;\n");
buffer.append("}}\n");*/
if(action.indexOf('?')>0){
buffer.append("document." + form + ".action='" + action+"&pageTotal='+id+'&pageNow='+"
+"1"
+ ";\n");
}else{
buffer.append("document." + form + ".action='" + action+"?pageTotal='+id+'&pageNow='+"
+"1"
+ ";\n");
}
buffer.append("document." + form + ".pageIndex.value=1;\n");
buffer.append("document." + form + ".submit();\n");
buffer.append("}\n");
buffer.append("function checkPage(size,page,total) {\n");
buffer.append("if(total)");
buffer.append("document." + form + ".action='" + action
+ "';\n");
buffer.append("document." + form + ".pageIndex.value=id;\n");
buffer.append("document." + form + ".submit();\n");
buffer.append("}\n");
buffer.append("function checkPageSize(page,size,total){\n");
buffer.append("var reg = /[^0-9]/;\n");
buffer.append("if(size.match(reg) != null){\n");
buffer.append("alert('每页记录数请输入整数');\n");
buffer.append("document." + form + ".pageSize.focus();}\n");
buffer.append("if(size<1){\n");
buffer.append("document." + form + ".pageSize.value=10;}\n");
buffer.append("if(size>300){\n");
buffer.append("document." + form + ".pageSize.value=300;}\n");
buffer.append("var newsize = document." + form
+ ".pageSize.value;\n");
buffer.append("checkPage(page,newsize,total);\n");
buffer.append("}\n");
buffer.append("function checkPage3(page,size,total){\n");
buffer.append("if(!page){\n");
buffer.append("alert('请输入跳转的页码!');\n");
buffer.append("return false;\n");
buffer.append("}\n");
buffer.append("var reg = /^(([0-9]{1,})([0-9]))|([1-9]{1})$/;\n");
buffer.append("if(!reg.exec(page)){\n");
buffer.append("alert('跳转页数请输入大于0的整数');\n");
//buffer.append("document." + form + ".pageIndex.focus();}\n");
buffer.append("return false;\n");
buffer.append("}\n");
buffer.append("var num = total/size; \n");
buffer.append("var nums = Math.round(num+0.4999999);\n");
buffer.append("if(page<1){\n");
buffer.append("document." + form + ".pageIndex.value=1;}\n");
buffer.append("if(page > nums){\n");
buffer.append("document." + form + ".pageIndex.value=nums;}\n");
buffer.append("GotoPage(page)");
buffer.append("}\n");
buffer.append("//-->\n");
buffer.append("</script>\n");
buffer.append("<script language=\"JavaScript\" event=\"onkeydown\" for=\"document\">\n"); buffer.append("if(event.srcElement.name=='pageSize'){\n");
buffer.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
buffer.append("return true;\n"); buffer.append("}else{return false;}}"); buffer.append("if(event.srcElement.name=='pageIndex'){\n");
buffer.append("if(((event.keyCode>=48)&&(event.keyCode<=57))||((event.keyCode>=96)&&(event.keyCode<=105))||(event.keyCode==8)||(event.keyCode==46)||(event.keyCode==13)||(event.keyCode==9)){\n");
buffer.append("return true;\n"); buffer.append("}else{return false;}}"); buffer.append("</script>\n");
// 以上为页面跳转的JavaScript方法
buffer.append("<table class=\"page\">\n");
buffer.append("<tr>\n");
buffer.append("<td width='20%'>\n");
buffer.append("每页显示的数量 <select name='ps' id='ps' onchange='javascript:changePage(this)' class='w5'>");
int size = RistConfig.getConfig().getInt("core.setPageSize.value",10);
if(size < 10){
size = 10;
}
for(int i=10;i<=100;i=i+5){
if(i == paginator.getPageSize()){
buffer.append("<option value="+i+" selected>"+i+"</option>");
}else{
buffer.append("<option value="+i+" >"+i+"</option>");
}
}
/*buffer.append("<option value=\"20\">20</option>");
buffer.append("<option value=\"30\">30</option>");
buffer.append("<option value=\"40\">40</option>");
buffer.append("<option value=\"50\">50</option>");
buffer.append("<option value=\"60\">60</option>");
buffer.append("<option value=\"70\">70</option>");
buffer.append("<option value=\"80\">80</option>");
buffer.append("<option value=\"90\">90</option>");
buffer.append("<option value=\"100\">100</option>");*/
buffer.append("</select>");
buffer.append("</td>\n");
buffer.append("<td width='30%' class='info'>\n");
buffer.append("共有<span class='orange fb'>" + totalPage + "</span>页");
buffer.append("<span class='orange fb'>" + paginator.getTotalCount()
+ "</span>条记录,");
buffer.append("当前为第<span class='orange fb'>" + paginator.getPageIndex()
+ "</span>页\n");
buffer.append("</td>\n");
buffer.append("<td width='35%' class='pagebuttons'>");
if (paginator.getPageIndex() > 1)
{// 不是第一页,显示“第一页”,“上一页”链接
buffer
.append("<a href='javascript:GotoPage(1)' class='bt_pghome'><span>首页</span></a>");
buffer
.append("<a class='bt_pgpre' href='javascript:GotoPage("
+ (paginator.getPageIndex() - 1)
+ ")'><span>上一页</span></a>");
}
else
{// 是第一页,不显示“第一页”,“上一页”链接
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pghome_gray'><span>首页</span></a>");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgpre_gray'><span>上一页</span></a>");
}
if (paginator.getPageIndex() == totalPage || totalPage <= 1)
{// 是最后页或共只有一页,不显示“下一页”,“最后页”链接
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgnext_gray'><span>下一页</span></a>");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgend_gray'><span>尾页</span></a>");
}
else
{// 不是最后页且总页数大于1页,显示“下一页”,“最后页”链接
/* buffer
.append("<a class='bt_pgnext' href='javascript:GotoPage("
+ (paginator.getPageIndex() + 1)
+ ")'>下一页</a>");
buffer
.append("<a class='bt_pgend' href='javascript:GotoPage("
+ totalPage
+ ")'>最后页</a>");*/
buffer.append("<a href='javascript:GotoPage("
+ (paginator.getPageIndex() + 1)
+ ")' class='bt_pgnext'><span>下一页</span></a>");
buffer.append("<a href='javascript:GotoPage("
+ totalPage
+ ")' class='bt_pgend'><span>尾页</span></a>");
}
buffer.append("</td>");
buffer
.append("<td width='2%' align='left'><input type='hidden' size='3' name='pageSize' value='"
+ paginator.getPageSize()
+ "' onkeyup=\"value=value.replace(/^[^1-9]+|[^\\d]/g,'');\" onblur='checkPageSize(document.all.pageIndex.value,document.all.pageSize.value,"
+ paginator.getTotalCount()
+ ")'></td>\n");
buffer
.append("<td width='2%' align='left'><input type='hidden' size='4' name='pageIndex' value='"
+ paginator.getPageIndex()
+ "'onkeyup=\"value=value.replace(/^[^1-9]+|[^\\d]/g,'');\" onblur='checkPage(document.all.pageIndex.value,document.all.pageSize.value,"
+ paginator.getTotalCount()
+ ")'></td>\n");
buffer.append("<td width='15%' class='skip'>转至 <input name='iPagePara' id='iPagePara' class='w2' "
+ "type='text' size='5' maxlength='5' > 页");
//buffer.append("<img onmouseover='this.src="
//+ GO2 +"' style='CURSOR:pointer' onmouseout='this.src="
//+ GO +"' height='18' alt='跳转' src="+ GO +" width='35'"
//+ " onclick=\"javascript:checkPage3(document.all.iPagePara.value,"+paginator.getPageSize()+","+paginator.getTotalCount()+");\" align='absmiddle'/></td>\n");
buffer.append("<input type='button' class='bt_pggo' onmouseover='this.className=\"bt_pggo_o\"' onmouseout='this.className=\"bt_pggo\"' ");
buffer.append(" onclick=\"javascript:checkPage3(document.all.iPagePara.value,"+paginator.getPageSize()+","+paginator.getTotalCount()+");\" /></td>");
buffer.append("</tr>\n");
buffer.append("</table>\n");
}
else
{// 无记录
buffer.append("<table class=page>\n");
buffer.append("<tr>\n");
buffer.append("<td width='50%'class='info'>\n");
buffer
.append("共有<span class='orange fb'>0</span>页<span class='orange fb'>0</span>条记录,");
buffer.append("当前为第<span class='orange fb'>0</span>页</a></td>");
buffer.append("<td width='35%' class='pagebuttons'>");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pghome_gray'><span>首页</span></a>\n");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgpre_gray'><span>上一页</span></a>\n");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgnext_gray'><span>下一页</span></a>\n");
buffer.append("<a disabled=\"disabled\" style=\"cursor: default\" href='javascript:;' class='bt_pgend_gray'><span>尾页</span></a>\n");
buffer.append("</td>");
buffer
.append("<td width='2%' align='left'>"
+ "</td>\n");
buffer
.append("<td width='2%' align='left'><input type='hidden' size='4' name='pageIndex' value='"
+ paginator.getPageIndex()
+ "' onblur='checkPage(document.all.pageIndex.value,document.all.pageSize.value,"
+ paginator.getTotalCount()
+ ")'></td>\n");
// buffer.append("<td width='15%'>转至 <input name='iPagePara' id='iPagePara' class=\"text\" "
// + "type='text' size='3'> 页");
// buffer.append("<img onmouseover='this.src="
// + GO2 +"' style='CURSOR:pointer' onmouseout='this.src="
// + GO +"' height='18' alt='跳转' src="+ GO +" width='35'"
// + " align='absmiddle'/></td>\n");
//buffer.append("<input type='button' class='bt_pggo' onmouseover='this.className=\"bt_pggo\"' onmouseout='this.className=\"bt_pggo\"' disabled='disabled'/></td>");
buffer.append("</tr>\n");
buffer.append("</table><br>\n");
}
out.write(buffer.toString());
out.flush();
}
catch (Exception e)
{
logger.error("生成分页html发生错误", e);
}
return EVAL_PAGE;
}
public void setForm(String form)
{
this.form = form;
}
public void setAction(String action)
{
HttpServletRequest request = (HttpServletRequest) pageContext
.getRequest();
this.action = request.getContextPath() + action;
}
public void setName(String name)
{
this.name = name;
}
public void setScope(String scope)
{
this.scope = scope;
}
}
第二步:
编写WEB_INF下tld目录下的paginator.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<short-name>paginator</short-name>
<uri>http://www.huadu-tech.com/tags-paginator</uri>
<description>
paginator taglib.
</description>
<tag>
<name>page</name>
<tag-class>com.cmsz.rist.common.page.PaginatorTag</tag-class>
<body-content>JSP</body-content>
<description>
Iterates over a specified array of objects, showing pages of information at a time.
</description>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>action</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>form</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
第三步:
引入标签体
<%@ taglib uri="/WEB-INF/tld/paginator.tld" prefix="paginator"%>
第四步:
在jsp页面上
<div align="center">
<paginator:page name="paginator" form="smsClientForm"
action="/querySmsClient.do?method=querySmsClient" />
</div>
结束!
希望对忘记了的朋友有帮助,也是为了在项目中能迅速copy 谢谢~