自定义JSP标签需要继承TagSupport类 然后在里面重写doEndTag()方法,框架大概如下:
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class PageTag extends TagSupport
{
private static final long serialVersionUID = 8028854417096217249L;
private final Logger logger = Logger.getLogger(PageTag.class);
protected String id;
/**
* 当前页
*/
protected int pageNum = 1;
/**
* 每页大小
*/
protected int pageSize = 10;
/**
* 总条数
*/
protected int totalObject;
/**
* 总页数
*/
protected int totalPageNum;
private int multiLineShow = 1;
/**
* 是否双排展示
*/
private boolean doubleShow = false;
/**
* 是否是采用 LI布局
*/
private boolean liShow = false;
/**
* 是否为弹出层显示
*/
protected boolean dialogShow = false;
private Collection<?> items;
public int doEndTag() throws JspException
{
genPageInfo();
return TagSupport.SKIP_BODY;
}
protected void genPageInfo()
{
ServletContext servletContext = pageContext.getServletContext();
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("GBK");
configuration.setServletContextForTemplateLoading(servletContext, "/");
try
{
Template template = configuration.getTemplate(getTemplatePath());
Writer out = pageContext.getOut();
getPageInfo();
Map<String, Object> rootMap = getResultMap();
template.process(rootMap, out);
}
catch (Exception e)
{
logger.error("生成分页标签错误" + e, e);
}
}
protected Map<String, Object> genRootMap()
{
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("multiLineShow", multiLineShow);
rootMap.put("liShow", liShow);
return rootMap;
}
public void getPageInfo()
{
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
if (isAjaxPage())
{
Page page = (Page)request.getAttribute("page");
totalObject = page.getTotalObject();
pageNum = page.getPageNum();
pageSize = (Integer)request.getAttribute("pageSize");
}
else
{
totalObject = ((null == items) ? 0 : items.size());
}
if (isDoubleShow())
{
multiLineShow = 2;
}
totalPageNum = (totalObject - 1) / pageSize + 1;
}
private Map<String, Object> getResultMap()
{
Map<String, Object> rootMap = genRootMap();
rootMap.put("pageNum", pageNum);
rootMap.put("pageSize", pageSize);
rootMap.put("totalObject", totalObject);
rootMap.put("totalPageNum", totalPageNum);
rootMap.put("dialogShow", dialogShow);
rootMap.put("id", id);
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
rootMap.put("contextPath", request.getContextPath());
return rootMap;
}
protected String getTemplatePath()
{
return "/jsp/common/page.ftl";
}
set、get方法
}
然后创建Tag Library的描述文件,在xx.tld tld文件里定义标签,接着在web.xml文件里面加入<taglib>元素,最后在jsp里就可引用了
pageTag.tld文件
<?xml version="1.0" encoding="UTF-8"?>
<!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>p</short-name>
<uri>\/tags\/page.tld</uri>
<tag>
<name>ajaxPage</name>
<tag-class>com.hao.common.</tag-class>
<attribute>
<name>id</name>
<required>true</required>
<type>java.lang.String</type>
<description>异步加载DIV的id</description>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onChangePageFunc</name>
<required>true</required>
<type>java.lang.String</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>true</required>
<type>java.lang.String</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>dialogShow</name>
<required>false</required>
<type>java.lang.Boolean</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>page</name>
<tag-class>com.hao.common.g</tag-class>
<body-content>empty</body-content>
<description>
<![CDATA[
<pre>
使用说明:
items action里面查询的 结果集
需要将这1个参数传给page.jsp<br />
pageSize 可选项
isLiShow 是否是采用的 li布局<br />
isDoubleShow:是否为双排展示<br />
isMultiLineShow:多排展示<br />
</pre>
]]>
</description>
<attribute>
<name>id</name>
<required>false</required>
<type>java.lang.String</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pageSize</name>
<required>false</required>
<type>java.lang.Integer</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<type>java.util.Collection</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>liShow</name>
<type>java.lang.Boolean</type>
<required>false</required>
</attribute>
<attribute>
<name>doubleShow</name>
<type>java.lang.Boolean</type>
<required>false</required>
</attribute>
<attribute>
<name>dialogShow</name>
<required>false</required>
<type>java.lang.Boolean</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>multiLineShow</name>
<required>false</required>
<type>java.lang.Integer</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
public class extends PageTag
{
private static final long serialVersionUID = -2926149990721737658L;
/**
* ajax调用的url
*/
private String url;
/**
* 点击调页是调用的方法
*/
private String onChangePageFunc;
protected String getTemplatePath()
{
return "/jsp/common/.ftl";
}
protected Map<String, Object> genRootMap()
{
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("url", url);
rootMap.put("onChangePageFunc", onChangePageFunc);
return rootMap;
}
get/set()……
}
web.xml文件
<jsp-config>
<taglib>
<taglib-uri>\/tags\/page.tld</taglib-uri>
<taglib-location>\/WEB-INF\/pageTag.tld</taglib-location>
</taglib>
</jsp-config>
jsp页面引用
<%@ taglib uri="/tags/page.tld" prefix="p"%>
<p:page items="${menuList}"/>