JSTL 自定义标签

JSTL自定义标签:

1  jstl类创建

package com.kic.test;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * JSTL的学习
 */
public class PageTag extends TagSupport {
	private static final long serialVersionUID = 4576929406613968767L;
	private int totalMessage;//总信息数
	private int nowPage;//当前页
	private int pageSize;//每页显示信息数
	private String url;//url
	private String target;//目标窗口
	private String parameterName;//参数名称

	public int getTotalMessage() {
		return totalMessage;
	}

	public void setTotalMessage(int totalMessage) {
		this.totalMessage = totalMessage;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getParameterName() {
		return parameterName;
	}

	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;
	}

	@Override
	public int doStartTag() throws JspException {
		String html = null;
		if (totalMessage != 0) {
			html = getHTML();
		} else {
			html = "暂无分页信息";
		}
		try {
			this.pageContext.getOut().print(html);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	/**
	 * 获取HTML代码
	 * 
	 * @return
	 */
	private String getHTML() {
		StringBuffer result = new StringBuffer();
		nowPage = nowPage == 0 ? 1 : nowPage;//默认显示第1页
		int totalPage = totalMessage / pageSize;//总页数
		if (totalMessage % pageSize != 0) {
			totalPage++;
		}
		String param = (url.contains("?") ? "&" : "?") + parameterName + "=";//获取URL
		//首页
		result.append("<a href=\"");
		result.append(url);
		result.append(param);
		result.append("1");
		if (null != target) {
			result.append("target=\"");
		}
		result.append("\">首页</a>");
		//上一页
		if (nowPage == 1) {
			result.append("\\上一页");
		} else {
			result.append("\\<a href=\"");
			result.append(url);
			result.append(param);
			result.append(nowPage - 1);
			if (null != target) {
				result.append("target=\"");
			}
			result.append("\">上一页</a>");
		}
		//下一页
		if (nowPage == totalPage) {
			result.append("\\下一页");
		} else {
			result.append("\\<a href=\"");
			result.append(url);
			result.append(param);
			result.append(nowPage + 1);
			if (null != target) {
				result.append("target=\"");
			}
			result.append("\">下一页</a>");
		}
		//尾页
		result.append("\\<a href=\"");
		result.append(url);
		result.append(param);
		result.append(totalPage);
		if (null != target) {
			result.append("target=\"");
		}
		result.append("\">尾页</a>");
		return result.toString();
	}
}

 2 WEB-INF下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">
	<description>JSTL 1.1 core library</description>
	<display-name>Test tag</display-name>
	<tlib-version>2.0</tlib-version>
	<short-name>test</short-name>
	<uri>http://zhouxianglh.iteye.com</uri>
	<tag>
		<description>分页标签</description>
		<name>page</name>
		<tag-class>com.kic.test.PageTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<description>总信息数</description>
			<name>totalMessage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
		</attribute>
		<attribute>
			<description>当前页</description>
			<name>nowPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
		</attribute>
		<attribute>
			<description>每页显示信息数</description>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
		</attribute>
		<attribute>
			<description>链接</description>
			<name>url</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>String</type>
		</attribute>
		<attribute>
			<description>当前页参数名称</description>
			<name>parameterName</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>String</type>
		</attribute>
		<attribute>
			<description>目标窗口</description>
			<name>target</name>
			<rtexprvalue>true</rtexprvalue>
			<type>String</type>
		</attribute>
	</tag>
</taglib>

 3 web.xml中注册

<!-- 设置tld文件路径 -->
	<jsp-config>
		<taglib>
			<taglib-uri>/testPage</taglib-uri>
			<taglib-location>/WEB-INF/test.tld</taglib-location>
		</taglib>
	</jsp-config>

 4 jsp页面使用

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/testPage"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>JSTL学习</title>
	</head>
	<body>
		<!-- 分页标签使用 -->
		<test:page pageSize="${pageSize}" parameterName="nowPage" totalMessage="${totalMessage}" url="page.action" nowPage="${nowPage}" />
	</body>
</html>
 

 

你可能感兴趣的:(html,xml,Web,jsp,sun)