自定义标签分三步
1.实现TagSupport
package com.it.huanyu;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;public class MyTestTag extends TagSupport { private static final long serialVersionUID = 1L; @Override public int doStartTag() throws JspException { JspWriter out=super.pageContext.getOut(); try { out.print("这个是自定义标签输出的大标题
"); out.print("这个是自定义标签输出的小标题
"); out.print("这个是自定义标签输出的内容这个是自定义标签输出的内容这个是自定义标签输出的内容这个是自定义标签输出的内容"); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); }}
2.写tld文件
2.0 1.2 myTestTag http://ithuanyu.cn/myTestTag/ out com.it.huanyu.MyTestTag JSP create navigation for paging
3.在页面引入和输出
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page trimDirectiveWhitespaces="true"%><%@ taglib prefix="myTestTag" uri="http://ithuanyu.cn/myTestTag/"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>自定义标签的使用
4.分页标签
package com.itheima.crm.utils;import java.io.IOException;import java.io.UnsupportedEncodingException;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;/** * 显示格式 上一页 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; @Override public 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; try { if (value.getClass().isArray()) { // 解决GET乱码问题 // value = new String(((String[]) // value)[0].getBytes("ISO-8859-1"), "UTF-8"); value = ((String[]) value)[0]; url = append(url, key.toString(), value.toString()); } else if (value instanceof String) { // 解决GET乱码问题 // value = new String(((String) // value).getBytes("ISO-8859-1"), "UTF-8"); value = (String) value; url = append(url, key.toString(), value.toString()); } } catch (Exception e) { e.printStackTrace(); } } 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; }}
2.0 1.2 common http://itcast.cn/common/ Common Tag Common Tag library page com.itheima.crm.utils.NavigationTag JSP create navigation for paging bean true number true url true true
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page trimDirectiveWhitespaces="true"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="itcast" uri="http://itcast.cn/common/"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>客户列表-BootCRM