花了一上午简单研究了下自定义标签。就弄了个自定义的分页标签。代码没有怎么仔细优化。先贴上来,:) 1,标签的实现类NumenTag.java import java.util.ArrayList; import javax.servlet.jsp.JspException; /** *//** private String url = null; private Result result; private String page = null; private String[] p; private Map kv = new HashMap(); private List key = null; /** *//** /** *//** public void setUrl(String url) { public int doStartTag() throws JspException { } 2,标签定义文件numentag.tld <?xml version=”1.0″ encoding=”utf-8″ ?> 3.js <%@ page contentType=”text/html; charset=utf-8″%>
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
* 自定义的分页标签
*
* @author 马万林
*
*/
public class NumenTag extends TagSupport {
* 分离参数
* @param params
*/
public void setParams(String params) {
key = new ArrayList();
p = params.split(“;”);
for (int i = 0; p.length>1&&i < p.length; i++) {
kv.put(p[i].split(“:”)[0], p[i].split(“:”)[1]);
key.add(p[i].split(“:”)[0]);
}
}
* 分页的结果
* @param result
*/
public void setResult(String result) {
this.result = (Result) pageContext.getRequest().getAttribute(“result”);
this.page = (String) pageContext.getRequest().getAttribute(“page”);
}
this.url = url;
}
StringBuffer sb = null;
if (result != null && result.getContent() != null
&& result.getContent().size() > 0) {
sb = new StringBuffer(“分页: “);
if (result.getPage().getHasPrePage()){
sb.append(“<a href=”).append(url);
if (url.indexOf(“?”) == -1)
sb.append(“?page=1”);
else
sb.append(“&page=1”);
linkParams(sb);
sb.append(“>”).append(“首页”).append(“</a>”).append(” | “);
sb.append(“<a href=”).append(url);
if (url.indexOf(“?”) == -1)
sb.append(“?page=”).append(
result.getPage().getPreviousPage());
else
sb.append(“&page=”).append(
result.getPage().getPreviousPage());
linkParams(sb);
sb.append(“>”).append(“上页”).append(“</a>”).append(” | “);
}
if (result.getPage().getHasNextPage()) {
sb.append(“<a href=”).append(url);
if (url.indexOf(“?”) == -1)
sb.append(“?page=”).append(result.getPage().getNextPage());
else
sb.append(“&page=”).append(result.getPage().getNextPage());
linkParams(sb);
sb.append(“>”).append(“下页”).append(“</a>”).append(” | “);
sb.append(“<a href=”).append(url);
if (url.indexOf(“?”) == -1)
sb.append(“?page=”).append(result.getPage().getTotalPage());
else
sb.append(“&page=”).append(result.getPage().getTotalPage());
linkParams(sb);
sb.append(“>”).append(“尾页”).append(“</a>”);
}
sb
.append(” 跳转到:<select name=goToPage onchange=javascript:goPage(this.value)>”);
int s = result.getPage().getTotalCount();
for (int i = 1; i <= s; i++) {
sb.append(“<option value=”).append(i);
if (Integer.parseInt(page) == i)
sb.append(” selected”);
sb.append(“>第”).append(i).append(“页</option>”);
}
sb.append(“</select>”);
sb.append(“<script language=javascript>”);
sb.append(“function goPage(page){“).append(“window.location=”)
.append(url);
if (url.indexOf(“?”) == -1)
sb.append(“?page=+”).append(“page”);
else
sb.append(“&page=+”).append(“page”);
sb.append(“+”);
linkParams(sb);
sb.append(“;”);
sb.append(“}”);
sb.append(“</script>”);
}
try {
if (sb != null) {
JspWriter out = pageContext.getOut();
out.print(sb);
}
} catch (Exception e) {
throw new JspException(e);
}
return EVAL_PAGE;
}
/** *//**
* 链接参数处理
* @param sb
*/
private void linkParams(StringBuffer sb) {
for (int i = 0; i < key.size(); i++) {
sb.append(“&”).append(key.get(i)).append(“=”).append(
kv.get(key.get(i)));
}
}
<!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>numen</short-name>
<!–OutputTag–>
<tag>
<name>out</name>
<tag-class>cn.cityyouth.util.NumenTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>result</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>params</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<%@ include file=”/common/taglibs.jsp”%>
<%@ taglib uri=”/WEB-INF/numentag.tld” prefix=”n”%>
<html>
<head>
</head>
<body>
<table>
<tr><td>
<n:out url=”test.do” result=”result” params=”${type}”/>
</td></tr>
</table>
</body>
</html>
http://blog.csdn.net/numen_wlm/archive/2007/01/30/1497778.aspx
jsp 自定义分页标签_jsp技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp 自定义分页标签_jsp技巧
相关推荐
-      通过jdbc连接oracle的十大灵活技术_jsp技巧
-      jdbc之代码重复使用_jsp技巧
-      提升jsp页面响应速度的七大秘籍绝招_jsp技巧
-      jdbc连sql server数据库步骤及有一项操作已被挂起,需重新启动计算机解决办法_jsp技巧
-      解决jsp中使用request乱码问题_jsp技巧
-      详细的jsp分页(oracle+jsp+apache)_jsp技巧
-      jsp中与标签要用不同的方式获得数据库中的数据_jsp技巧
-      jsp2.0新特性_jsp文摘