花了一上午簡單研究了下自定義標簽。就弄了個自定義的分頁標簽。代碼沒有怎麼仔細優化。先貼上來,:)
1,標簽的實現類NumenTag.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp(SUN企業級應用的首選).jsp(SUN企業級應用的首選)Exception;
import javax.servlet.jsp(SUN企業級應用的首選).jsp(SUN企業級應用的首選)Writer;
import javax.servlet.jsp(SUN企業級應用的首選).tagext.TagSupport;
/** *//**
* 自定義的分頁標簽
*
* @author 馬萬林
*
*/
public class NumenTag extends TagSupport {
private String url = null;
private Result result;
private String page = null;
private String[] p;
private Map kv = new HashMap();
private List key = null;
/** *//**
* 分離參數
* @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");
}
public void setUrl(String url) {
this.url = url;
}
public int doStartTag() throws jsp(SUN企業級應用的首選)Exception {
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().getTo