2019独角兽企业重金招聘Python工程师标准>>>
1 先要导入一个jar包
2 自定一个标签库 ,该库放在WEB-INF/tld下面
3 需要实现 该类 extends TagSupport 实现里面的方法
4 在jsp页面引入 该标签库 并使用
5 在web.xml文件中配置该标签 (不过这个不是必须的)
6 现在给出一个显示页码的 自定义 处理类
package com.shi.utils;import java.io.IOException;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;import org.apache.taglibs.standard.tag.common.core.UrlSupport;/*** 显示格式 上一页 1 2 3 4 5 下一页*/
public class NavigationTag extends TagSupport {static final long serialVersionUID = 2372405317744358833L;/*** request 中用于保存Page 对象的变量名,默认为“page”*/private String bean = "page";/*** 分页跳转的url地址,此属性必须*/private String url = null;/*** 显示页码数量*/private int number = 5;@Overridepublic int doStartTag() throws JspException {JspWriter writer = pageContext.getOut();HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();Page page = (Page)request.getAttribute(bean); if (page == null) return SKIP_BODY;url = resolveUrl(url, pageContext);try {//计算总页数int pageCount = page.getTotal() / page.getSize();if (page.getTotal() % page.getSize() > 0) {pageCount++;}writer.print("");} catch (IOException e) {e.printStackTrace();}return SKIP_BODY;}private String append(String url, String key, int value) {return append(url, key, String.valueOf(value));}/*** 为url 参加参数对儿* * @param url* @param key* @param value* @return*/private String append(String url, String key, String value) {if (url == null || url.trim().length() == 0) {return "";}if (url.indexOf("?") == -1) {url = url + "?" + key + "=" + value;} else {if(url.endsWith("?")) {url = url + key + "=" + value;} else {url = url + "&" + key + "=" + value;}}return url;}/*** 为url 添加翻页请求参数* * @param url* @param pageContext* @return* @throws javax.servlet.jsp.JspException*/private String resolveUrl(String url, javax.servlet.jsp.PageContext pageContext) throws JspException{//UrlSupport.resolveUrl(url, context, pageContext)Map params = pageContext.getRequest().getParameterMap();for (Object key:params.keySet()) {if ("page".equals(key) || "rows".equals(key)) continue;Object value = params.get(key);if (value == null) continue;if (value.getClass().isArray()) {url = append(url, key.toString(), ((String[])value)[0]);} else if (value instanceof String) {url = append(url, key.toString(), value.toString());}}return url;}/*** @return the bean*/public String getBean() {return bean;}/*** @param bean the bean to set*/public void setBean(String bean) {this.bean = bean;}/*** @return the url*/public String getUrl() {return url;}/*** @param url the url to set*/public void setUrl(String url) {this.url = url;}public void setNumber(int number) {this.number = number;}}