分頁標籤庫
阿新 • • 發佈:2018-12-12
tld 自定義標籤一般放在WEB-INF(可以限制訪問,提高安全性等)根目錄下
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <!-- 標籤庫描述符 --> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <uri>/zking</uri> <tag> <name>page</name> <tag-class>com.zking.test.tag.PageTag</tag-class> <body-content>empty</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>rownum</name> <tag-class>com.zking.test.tag.RownumTag</tag-class> <body-content>empty</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>count</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>select</name> <tag-class>com.zking.test.tag.SelectTag</tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>id</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>cssStyle</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>cssClass</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>onchange</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>valueKey</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>textKey</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>headValue</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>headText</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>selectedValue</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
分頁標籤
package com.zking.test.tag; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import com.zking.test.util.PageBean; public class PageTag extends BodyTagSupport { private static final long serialVersionUID = 8003507841172246203L; private PageBean pageBean; public PageTag() { super(); } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } @Override public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.println(this.toHtml()); return SKIP_BODY; } catch (Exception e) { throw new RuntimeException(e); } } private String toHtml() { StringBuffer bf = new StringBuffer(); if (null == pageBean || !pageBean.isPagination()) { return bf.toString(); } // 1. 生成表單 bf.append("<form id='pageBeanForm' action='" + pageBean.getUrl() + "' method='post'>"); bf.append("<input type='hidden' name='page' value=''>"); for (Map.Entry<String, String[]> entry : pageBean.getParameterMap().entrySet()) { String name = entry.getKey(); String[] values = entry.getValue(); if ("page".equals(name)) { continue; } for (String value : values) { bf.append("<input type='hidden' name='" + name + "' value='" + value + "'>"); } } bf.append("</form>"); // 2. 生成按鈕 bf.append("<div style='text-align: right;font-size: 12px;'>每頁" + pageBean.getRows() + "條,共" + pageBean.getTotal() + "條,當前第" + pageBean.getPage() + "頁,共" + pageBean.getMaxPage() + "頁 <a href='javascript:doGotoPage(1)'>首 頁</a> <a href='javascript:doGotoPage(" + pageBean.getPreviousPage() + ")'>上一頁</a> <a href='javascript:doGotoPage(" + pageBean.getNextPage() + ")'>下一頁</a> <a href='javascript:doGotoPage(" + pageBean.getMaxPage() + ")'>尾 頁</a> <input id='pageNumber' type='text' style='text-align: center;width: 30px;'> <a href='javascript:doSkipPage();'>GO</a></div>"); // 3. 動態生成js bf.append("<script type='text/javascript'>"); bf.append("function doGotoPage(page){"); bf.append(" var form = document.getElementById('pageBeanForm');"); bf.append(" form.page.value = page;"); bf.append(" form.submit();"); bf.append("}"); bf.append("function doSkipPage(page){"); bf.append(" var page = document.getElementById('pageNumber').value;"); bf.append(" if(!page||isNaN(page)||parseInt(page)<1||parseInt(page)>"+pageBean.getMaxPage()+"){"); bf.append(" alert('頁碼必須為1~"+pageBean.getMaxPage()+"之間數字');"); bf.append(" document.getElementById('pageNumber').value = '';"); bf.append(" return;"); bf.append(" }"); bf.append(" doGotoPage(parseInt(page));"); bf.append("}"); bf.append("</script>"); return bf.toString(); } }
標識記錄
package com.zking.test.tag; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import com.zking.test.util.PageBean; public class RownumTag extends BodyTagSupport { private static final long serialVersionUID = 8003507841172246203L; private PageBean pageBean; private Integer count; public RownumTag() { super(); } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } @Override public int doStartTag() throws JspException { try { int rownum = (pageBean.getPage() - 1) * pageBean.getRows() + count; JspWriter out = pageContext.getOut(); out.println(rownum + ""); return SKIP_BODY; } catch (Exception e) { throw new RuntimeException(e); } } }
下拉標籤
package com.zking.test.tag;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 8988102673322920172L;
private String name;// 非空 <select name=''>
private String id;// 允許空 <select id=''>
private String cssStyle;// 允許空 <select style=''>
private String cssClass;// 允許空 <select class=''>
private String onchange;// 允許空 <select onchange=''>
private List<Object> items;// 非空,資料來源
private String valueKey;// 非空屬性名,用此物件此屬性生成option的value
private String textKey;// 非空屬性名,用此物件此屬性生成option的text
private String headText;// 允許空
private String headValue = "";// 允許空
private Object selectedValue;// 允許空
public SelectTag() {
super();
}
public Object getSelectedValue() {
return selectedValue;
}
public void setSelectedValue(Object selectedValue) {
this.selectedValue = selectedValue;
}
public String getHeadText() {
return headText;
}
public void setHeadText(String headText) {
this.headText = headText;
}
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCssStyle() {
return cssStyle;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getOnchange() {
return onchange;
}
public void setOnchange(String onchange) {
this.onchange = onchange;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getValueKey() {
return valueKey;
}
public void setValueKey(String valueKey) {
this.valueKey = valueKey;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.println(this.toHTML());
return SKIP_BODY;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String toHTML() throws Exception {
StringBuffer bf = new StringBuffer();
bf.append("<select");
bf.append(" name='" + name + "'");
if (null != id) {
bf.append(" id='" + id + "'");
}
if (null != cssStyle) {
bf.append(" style='" + cssStyle + "'");
}
if (null != cssClass) {
bf.append(" class='" + cssClass + "'");
}
if (null != onchange) {
bf.append(" onchange='" + onchange + "'");
}
bf.append(">");
if (null != headText) {
bf.append("<option value='" + headValue + "'>" + headText + "</option>");
}
for (int i = 0; null != items && i < items.size(); i++) {
Object obj = items.get(i);// deptId,deptName
Object value = PropertyUtils.getProperty(obj, valueKey);// deptId
Object text = PropertyUtils.getProperty(obj, textKey);// deptName
if (null != selectedValue && selectedValue.equals(value)) {
bf.append("<option selected value='" + value + "'>" + text + "</option>");
} else {
bf.append("<option value='" + value + "'>" + text + "</option>");
}
}
bf.append("</select>");
return bf.toString();
}
}