📄 pagertag.java
字号:
package com.pegasus.framework.component.taglib.html.pager;
import javax.servlet.jsp.JspException;
import com.pegasus.framework.component.taglib.xml.Xhtml;
public class PagerTag extends BasePagerTag {
// private Logger logger = LogManager.getLogger(BasePagerTag.class);
/**
* 组装HTML.
*/
protected void prepareHtml() throws JspException{
startTable();
prepareComponent(getFirstImg(), getFirstSource(), getJSTaglibPagerGoToPageDefine("FIRST"));
writer.text(getSeparator());
prepareComponent(getPreImg(), getPreSource(), getJSTaglibPagerGoToPageDefine("PRE"));
writer.textBlank(Integer.parseInt("5"));
preparePages(showPageNumber, maxPageNumber);
writer.textBlank(Integer.parseInt("5"));
prepareComponent(getNextImg(), getNextSource(), getJSTaglibPagerGoToPageDefine("NEXT"));
writer.text(getSeparator());
prepareComponent(getLastImg(), getLastSource(), getJSTaglibPagerGoToPageDefine("LAST"));
writer.textBlank(2);
prepareCurAndTotalPage(getPageNo(), getTotalPageCount());
writer.textBlank(2);
prepareTotalObject(getTotalObjectCount());
writer.textBlank(2);
prepareFetchSize(getFetchSize());
endTable();
}
/**
* 组装javascipt.
*/
protected void prepareJs() throws JspException{
prepareJSTaglibPagerGoToPage();
}
/**
* Gets the JS taglib pager go to page define.
*
* @param actionType the action type
*
* @return the JS taglib pager go to page define
*/
private String getJSTaglibPagerGoToPageDefine(String actionType) {
return getJSTaglibPagerGoToPageDefine(actionType, getPageNo());
}
/**
* Gets the JS taglib pager go to page define.
*
* @param actionType the action type
* @param pageNo the page no
*
* @return the JS taglib pager go to page define
*/
private String getJSTaglibPagerGoToPageDefine(String actionType, int pageNo) {
StringBuffer function = new StringBuffer();
function.append("taglib_pager_goToPage(");
function.append(getName()).append(",'");
function.append(actionType).append("',");
function.append(pageNo).append(",");
function.append(getTotalPageCount()).append(")");
return function.toString();
}
/**
* Prepare JS taglib pager go to page.
*/
private void prepareJSTaglibPagerGoToPage() {
writer.startElement(Xhtml.Html.SCRIPT);
writer.attribute(Xhtml.Attr.TYPE, "text/javascript");
writer.attribute(Xhtml.Attr.LANGUAGE, "JavaScript");
writer.textLineBreak();
StringBuffer function = new StringBuffer();
function.append(" function taglib_pager_goToPage(form,action_type,pageNo, totalPageCount){").append(
"\n");
function.append(" if(pageNo == -1) pageNo = document.getElementById('").append(getPageNoProperty())
.append("').value *1;").append("\n");
function.append(" switch(action_type) {").append("\n");
function.append(" case 'FIRST' : ").append("\n");
function.append(" pageNo = 1;if(totalPageCount == 0) pageNo =0;break;").append("\n");
function.append(" case 'PRE': ").append("\n");
function.append(" pageNo = pageNo - 1;if(pageNo <= 1) pageNo =1;").append("\n");
function.append(" if(totalPageCount == 0) pageNo =0;break;").append("\n");
function.append(" case 'NEXT': ").append("\n");
function
.append(" pageNo = pageNo + 1;if(pageNo > totalPageCount ) pageNo = totalPageCount;break;")
.append("\n");
function.append(" case 'LAST': ").append("\n");
function.append(" pageNo = totalPageCount;if(totalPageCount == 0) pageNo =0;break;").append("\n");
function.append(" case 'GO': ").append("\n");
function.append(" if(pageNo > totalPageCount ) pageNo = totalPageCount;").append("\n");
function.append(" if(pageNo < 1 ) pageNo = 1;").append("\n");
function.append(" break;").append("\n");
function.append(" }").append("\n");
function.append(" if(pageNo < 0) return;").append("\n");
function.append(" if(pageNo > totalPageCount) return;").append("\n");
function.append(" document.getElementById('").append(getPageNoProperty()).append(
"').value = pageNo;").append("\n");
if (getUrl() != null && getUrl().length() > 0) {
function.append(" form.action = '" + getUrl() + "';").append("\n");
}
function.append(" form.submit();").append("\n");
function.append("}").append("\n");
writer.text(function);
writer.endElement(Xhtml.Html.SCRIPT);
writer.textLineBreak();
}
/**
* Start table.
*/
private void startTable() {
writer.startElement(Xhtml.Html.DIV);
writer.startElement(Xhtml.Html.TABLE);
writer.attribute(Xhtml.Attr.CLASS, tableStyleClass);
writer.attribute(Xhtml.Attr.STYLE, tableStyle);
writer.startElement(Xhtml.Html.TR);
writer.startElement(Xhtml.Html.TD);
writer.attribute(Xhtml.Attr.CLASS, tdStyleClass);
writer.attribute(Xhtml.Attr.STYLE, tdStyle);
}
/**
* End table.
*/
private void endTable() {
writer.endElement(Xhtml.Html.TD);
writer.endElement(Xhtml.Html.TR);
writer.endElement(Xhtml.Html.TABLE);
writer.endElement(Xhtml.Html.DIV);
}
/**
* Prepare component.组装前进,后退,首页,尾页
*
* @param img the img
* @param source the source
* @param clickFunction the click function
*/
private void prepareComponent(String img, String source, String clickFunction) throws JspException{
if (PAGE_TYPE_IMG.equals(getPageType())) {
writer.startElement(Xhtml.Html.IMG);
if (btnStyle == null) {
writer.attribute(Xhtml.Attr.STYLE, "cursor:hand;");
}
else {
writer.attribute(Xhtml.Attr.STYLE, btnStyle + ";cursor:hand;");
}
writer.attribute(Xhtml.Attr.SRC, img);
writer.attribute(Xhtml.Attr.ALT, getMessage(source, isUseResource(), getBundle()));
writer.attribute(Xhtml.Attr.ONCLICK, clickFunction);
}
else if (PAGE_TYPE_BTN.equals(getPageType())) {
writer.startElement(Xhtml.Html.INPUT);
writer.attribute(Xhtml.Attr.CLASS, btnStyleClass);
if (btnStyle == null) {
writer.attribute(Xhtml.Attr.STYLE, "cursor:hand;");
}
else {
writer.attribute(Xhtml.Attr.STYLE, btnStyle + ";cursor:hand;");
}
writer.attribute(Xhtml.Attr.TYPE, PagerConstant.INPUT_TYPE_BUTTON);
writer.attribute(Xhtml.Attr.VALUE, getMessage(source, isUseResource(), getBundle()));
writer.attribute(Xhtml.Attr.ONCLICK, clickFunction);
}
else if (PAGE_TYPE_LINK.equals(getPageType())) {
writer.startElement(Xhtml.Html.A);
writer.attribute(Xhtml.Attr.ONCLICK, clickFunction);
writer.attribute(Xhtml.Attr.CLASS, btnStyleClass);
if (btnStyle == null) {
writer.attribute(Xhtml.Attr.STYLE, "cursor:hand;");
}
else {
writer.attribute(Xhtml.Attr.STYLE, btnStyle + ";cursor:hand;");
}
writer.text(getMessage(source, isUseResource(), getBundle()));
writer.endElement(Xhtml.Html.A);
}
else {
throw new JspException("don't support pageType:" + getPageType());
}
}
/**
* Prepare pages.组装google样式
*
* @param showPageNumber the show page number
* @param maxPageNumber the max page number
*/
private void preparePages(boolean showPageNumber, int maxPageNumber) {
if (showPageNumber) {
for (int i = pageNo; i < pageNo + maxPageNumber; i++) {
writer.startElement(Xhtml.Html.A);
writer.attribute(Xhtml.Attr.ONCLICK, getJSTaglibPagerGoToPageDefine("GO", i));
writer.attribute(Xhtml.Attr.STYLE, "cursor:hand;");
writer.text(new Integer(i));
writer.endElement(Xhtml.Html.A);
if (i >= totalPageCount) {
break;
}
}
}
}
/**
* Prepare cur and total page.组装当前页和总页数
*
* @param pageNo the page no
* @param totalPageCount the total page count
*/
private void prepareCurAndTotalPage(int pageNo, int totalPageCount) {
writer.startElement(Xhtml.Html.INPUT);
writer.attribute(Xhtml.Attr.NAME, getPageNoProperty());
writer.attribute(Xhtml.Attr.CLASS, inputStyleClass);
writer.attribute(Xhtml.Attr.STYLE, inputStyle);
writer.attribute(Xhtml.Attr.TYPE, PagerConstant.INPUT_TYPE_TEXT);
writer.attribute(Xhtml.Attr.VALUE, new Integer(pageNo).toString());
writer.attribute(Xhtml.Attr.SIZE, new Integer(DEFAULT_INPUT_SIZE).toString());
writer.attribute(Xhtml.Attr.MAXLENGTH, new Integer(DEFAULT_INPUT_MAXLENGTH).toString());
writer.attribute(Xhtml.Attr.ONKEYDOWN, "if(event.keyCode==13) "
+ getJSTaglibPagerGoToPageDefine("GO", -1));
writer.textForwardSlash();
writer.text(new Integer(totalPageCount).toString());
}
/**
* Prepare total object.组装总记录数
*
* @param totalObjectCount the total object count
*/
private void prepareTotalObject(int totalObjectCount) {
writer.text(getMessage(getTotalObjectTitle(), isUseResource(), getBundle()));
writer.textBlank();
writer.text(new Integer(totalObjectCount).toString());
}
/**
* Prepare fetch size.组装当前页数量
*
* @param fetchSize the fetch size
*/
private void prepareFetchSize(int fetchSize) {
String fetchSizeStr = new Integer(fetchSize).toString();
writer.text(getMessage(getFetchSizeTitle(), isUseResource(), getBundle()));
writer.textBlank();
writer.startElement(Xhtml.Html.SELECT);
writer.attribute(Xhtml.Attr.NAME, getFetchSizeProperty());
writer.attribute(Xhtml.Attr.ONCHANGE, getJSTaglibPagerGoToPageDefine("", 1));
for (int i = 0; i < fetchSizeVector.size(); i++) {
String value = (String) fetchSizeVector.get(i);
writer.startElement(Xhtml.Html.OPTION);
if (value.equals(fetchSizeStr)) {
writer.attribute(Xhtml.Attr.SELECTED, PagerConstant.TRUE);
}
writer.text(value);
writer.endElement(Xhtml.Html.OPTION);
}
writer.endElement(Xhtml.Html.SELECT);
}
/**
*
*/
protected void initPager() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -