码迷,mamicode.com
首页 > 其他好文 > 详细

SSH自定义标签

时间:2014-08-20 14:06:42      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

 一、标签处理类:
package
cn.conris.sys.form; import java.io.IOException; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; import javax.servlet.jsp.tagext.TagSupport; import cn.conris.sys.utils.PropertyUtil; public class PageTag extends TagSupport { private int pageSize = PropertyUtil.getPageSize(); // 每页要显示的记录数 private int pageNo = PropertyUtil.getPageNumber(); // 页号 private int recordCount; // 总记录数 private String url; // 目的地URL public void setPageSize(int pageSize) { this.pageSize = pageSize; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } public void setUrl(String url) { this.url = url; } // 主要的逻辑 @Override public int doStartTag() throws JspException { if(recordCount == 0){ return super.doStartTag(); } // 总页数 int pageCount = (recordCount + pageSize - 1) / pageSize; // 页号越界处理 if (pageNo > pageCount) { pageNo = pageCount; } if (pageNo < 1) { pageNo = 1; } StringBuilder sb = new StringBuilder(); sb.append("<form name=‘pageController‘ id=‘pageController‘ action=‘‘ method=‘post‘>&nbsp") .append("<input type=‘hidden‘ id=‘pageNo‘ name=‘pageNo‘ value=‘" + pageNo + "‘ />&nbsp"); //------------------------------------ 获取所有請求中的参数 HttpServletRequest request = (HttpServletRequest) pageContext .getRequest(); Enumeration<String> enumeration = request.getParameterNames(); String name = null; String value = null; //把请求中的所有参数当作隐藏表单域在页面中写出) while (enumeration.hasMoreElements()) { name = enumeration.nextElement(); value = request.getParameter(name); // 去除页号 if (name.equals("pageNo")) { if (null != value && !"".equals(value)) { pageNo = Integer.parseInt(value); } continue; } sb.append("<input type=‘hidden‘ name=‘") .append(name) .append("‘ value=‘") .append(value) .append("‘/>&nbsp"); } //---------------------------------------------------- sb.append(" 共" + pageCount + "页,&nbsp") .append("当前第" + pageNo + "页 &nbsp"); if (pageNo == 1) { sb.append("首页"); sb.append(" "); sb.append("上一页&nbsp"); } else { sb.append("<a href=‘#‘ onclick=‘turnOverPage(1)‘>首页</a>&nbsp"); sb.append(" "); sb.append("<a href=‘#‘ onclick=‘turnOverPage(") .append((pageNo - 1)).append(")‘>上一页</a>&nbsp"); } sb.append(" "); if (pageNo == pageCount) { sb.append("下一页"); sb.append(" "); sb.append("尾页&nbsp"); } else { sb.append("<a href=‘#‘ onclick=‘turnOverPage(") .append((pageNo + 1)).append(")‘>下一页</a>&nbsp"); sb.append(" "); sb.append("<a href=‘#‘ onclick=‘turnOverPage(").append(pageCount) .append(")‘>尾页</a>&nbsp"); } sb.append(" 跳到<select style=‘width:80px;‘ onChange=‘turnOverPage(this.value)‘>&nbsp"); for (int i = 1; i <= pageCount; i++) { if (i == pageNo) { sb.append(" <option value=‘") .append(i) .append("‘ selected=‘selected‘>第") .append(i) .append("页</option>&nbsp"); } else { sb.append(" <option value=‘") .append(i) .append("‘>第") .append(i) .append("页</option>&nbsp"); } } sb.append("</select>&nbsp"); sb.append(" &nbsp"); sb.append("</form>&nbsp"); // 生成提交表单的JS sb.append("<script language=‘javascript‘>"); // sb.append(" //翻页函数/t/n"); sb.append(" function turnOverPage(no){"); sb.append(" var form = document.pageController;"); // sb.append(" //页号越界处理"); sb.append(" if(no").append(">").append(pageCount).append(") {"); sb.append(" no=").append(pageCount).append(";"); sb.append(" }"); sb.append(" if(no").append("< 1){"); sb.append(" no=1;"); sb.append(" }"); sb.append(" form.").append("pageNo").append(".value=no;"); sb.append(" form.action=‘").append(url).append("‘;"); sb.append(" form.submit();"); sb.append(" }"); sb.append("</script>"); try { pageContext.getOut().println(sb.toString()); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } }
二、标签库描述文件:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
 <tlib-version>2.0</tlib-version>
 <short-name>my</short-name>
 <uri>http://conris/ModelSystem</uri>
 <tag>
    <description>通用分页标签</description>
    <name>pager</name>
    <tag-class>cn.conris.sys.form.PageTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>pageNo</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue> 
    </attribute>
    <attribute>
      <name>recordCount</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>pageSize</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>url</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

三\配置web.xml


 <!-- 配置自定义标签 -->
  <jsp-config>
    <taglib>
        <taglib-uri>/mytaglib</taglib-uri>
        <taglib-location>/WEB-INF/classes/pageTag.tld</taglib-location>
    </taglib>
  </jsp-config>

四\把pageSize写在了配置文件里面,这样可以灵活的修改配置。

pageSize=5
pageNumber=1

读取配置文件的工具类

package cn.conris.sys.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {
    private static Properties p = new Properties();
    private static InputStream inputStream = null;
    static {
        inputStream = PropertyUtil.class
                .getResourceAsStream("/page.properties");
        try {
            p.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int getPageSize() {
        return Integer.parseInt(p.getProperty("pageSize"));
    }

    public static int getPageNumber() {
        return Integer.parseInt(p.getProperty("pageNumber"));
    }

    

    public static void main(String[] args) {
        System.out.println(getPageSize());
    }

}

五,action处理类

public String listmap() {
        String name = extendAttribute.getName();
        String type = extendAttribute.getType();
        List<ExtendAttribute> list = null;
        Map<String, String> maps = new HashMap<String, String>();
        if (name != null && !name.equals("")) {
            maps.put("name", name);
        }
        if (type != null && !type.equals("")) {
            maps.put("type", type);
        }

        list = extendAttributeService.find(maps);
        
        String pageNoStr = request.getParameter("pageNo");
        int pageNo = PropertyUtil.getPageNumber();
        if (pageNoStr != null && !pageNoStr.equals("")) {
            pageNo = Integer.parseInt(pageNoStr);
        }

        int recordCount = list.size();
        int pageSize = PropertyUtil.getPageSize();
        int startNo = (pageNo - 1) * pageSize;
        List<ExtendAttribute> result = null;
        if (recordCount <= pageSize) {
            result = list;
        } else {
            if(startNo + pageSize>recordCount){
                result = list.subList(startNo, recordCount);    
            }else{
                result = list.subList(startNo, startNo + pageSize);
            }
        }

        request.setAttribute("recordCount", recordCount);
        request.setAttribute("pageNo", pageNo);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("list", result);
        // request.setAttribute("list", list);
        return "success";
    }

 

 

 

SSH自定义标签,布布扣,bubuko.com

SSH自定义标签

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/mgzy/p/3924351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!