java web 自定义标签_javaweb自定义标签

标签处理类:PageTagLib.java

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.TagSupport;

public class PageTagLib extends TagSupport {

private JspWriter writeout;

private int crruntPage;

private int pageSize;

private int totalCount;

private String doClick;

/**

* 序列化

*/

private static final long serialVersionUID = 3174234039143531070L;

/**

* @see 如若使用直接定位的 的input name名为crruntPage

*/

@Override

public int doEndTag() throws JspException {

initWriter();

showPageBody();

return EVAL_PAGE;

}

public void showPageBody() {

int PageSize = getPageSize() == 0 ? Constants.PAGESIZE : getPageSize();

setPageSize(PageSize);

StringBuffer out = new StringBuffer();

out = out.append("

");

out = out.append("

");

out = out.append("共 " + getTotalCount() + " 条纪录,当前第 [ "

+ getCrruntPage() + " / " + getMaxCount()[2] + " ] 页,每页"

+ PageSize + "条纪录");

out = out.append("

");

out = out.append("

");

out = out

.append("

out = out.append("

");

int lastPage = getCrruntPage() - 1 <= 0 ? 1 : getCrruntPage() - 1;

int nextPage = getCrruntPage() + 1 > getMaxCount()[2] ? getMaxCount()[2]

: getCrruntPage() + 1;

out = out

.append("

first.gif

+ getDoClick()

+ "\" href=\"#\" id=1 width='37' height='15' />

");

out = out

.append("

back.gif

+ getDoClick()

+ "\" href=\"#\" id="

+ lastPage

+ " width='43' height='15' />

");

out = out

.append("

next.gif

+ getDoClick()

+ "\" href=\"#\" id="

+ nextPage

+ " width='43' height='15' />

");

out = out

.append("

last.gif

+ getDoClick()

+ "\" href=\"#\" id="

+ getMaxCount()[2]

+ " width='37' height='15' />

");

out = out

.append("

转到第
");

out = out

.append("

");

out = out

.append(" "

+ getMaxCount()[2]

+ "){this.value=''}\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" name ='crruntPage' id ='crruntPage' value="

+ getCrruntPage() + " />");

out = out.append("

");

out = out.append("

页");

out = out

.append("

go.gif

+ getDoClick() + "\" width='37' height='15' />

");

out = out.append("

");

out = out.append("

");

out = out.append("

");

out = out.append("

");

out = out.append("");

out = out

.append("

tab_22.gif");

out = out.append("");

try {

this.writeout.println(out.toString());

this.writeout.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/** 获取当前页数的和最大页数的算法 * */

public int[] getMaxCount() {

int x = 1, y = 0;

// 总页面数

int totalPage = (getTotalCount() % getPageSize() == 0 ? getTotalCount()

/ getPageSize() : getTotalCount() / getPageSize() + 1);

if (getCrruntPage() < 5 && getCrruntPage() > 0) {

if (totalPage >= 5) {

x = 1;

y = 5;

} else {

x = 1;

y = totalPage;

}

}

if (getCrruntPage() > 2 && (getCrruntPage() + 2) <= totalPage) {

x = getCrruntPage() - 2;

y = getCrruntPage() + 2;

}

if (getCrruntPage() > 4 && (getCrruntPage() + 2) > totalPage) {

x = getCrruntPage() - 3;

y = totalPage;

}

if (getCrruntPage() == totalPage && getCrruntPage() > 4) {

x = getCrruntPage() - 4;

y = totalPage;

}

if (totalPage == 1) {

x = 1;

y = 1;

}

if (totalPage == 0) {

x = 1;

y = 1;

}

int[] out = new int[] { x, y, totalPage };

return out;

}

/** 获取页面中的out的对象 */

public void initWriter() {

try {

this.pageContext.getResponse().flushBuffer();

this.writeout = this.pageContext.getOut();

} catch (Exception e) {

// e.printStackTrace();

}

}

public int getCrruntPage() {

return crruntPage;

}

public void setCrruntPage(int crruntPage) {

this.crruntPage = crruntPage;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getTotalCount() {

return totalCount;

}

public void setTotalCount(int totalCount) {

this.totalCount = totalCount;

}

public String getDoClick() {

return doClick;

}

public void setDoClick(String doClick) {

this.doClick = doClick;

}

}

配置文件:page.tld

xsi:schemaLocation="外链网址已屏蔽 Web-jsptaglibrary_2_0.xsd"

version="2.0" >

page Tag

1.0

page_tag

/page_tag

showPageTag

com.immunity.util.PageTagLib

empty

crruntPage

true

true

pageSize

false

true

totalCount

true

true

doClick

true

true

web.xml

/page_tag

/WEB-INF/page.tld

/jsp/*

utf-8

页面使用 jsp文件中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值