SSH自定义标签

 一、标签处理类:
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";
}

六\引用分页标签

<div class="pagination pagination-centered">
<mytag:pager pageNo="${pageNo}" recordCount="${recordCount}"
pageSize="${pageSize}" url="ExtendAttributeAction_listmap.action" />
</div>

七\条件查询时,把条件值回显,支持条件分页.

原理:点击分页条,调用action方法后会执行doStartTag()方法,标签会自动添加表单中所有属性值.

action中:   
     request.setAttribute("recordCount", recordCount);
request.setAttribute("pageNo", pageNo);
request.setAttribute("pageSize", pageSize);
request.setAttribute("list", result);
// request.setAttribute("list", list);
request.setAttribute("name", name);
request.setAttribute("type", type);
jsp回显:
<form id="serach" class="form-search"
action="ExtendAttributeAction_listmap.action" method="post">
扩展属性名称: <input type="text" class="input-medium span2" name="name" value="${name }">&nbsp;&nbsp;
扩展属性类型: <select class="drop_down"
name="type">
<c:choose>
<c:when test="${type=='int'}">
<option value="">全部</option>
<option value="int" selected="selected">整型</option>
<option value="float">浮点型</option>
<option value="varchar(256)">字符串型</option>
</c:when>
<c:when test="${type=='float'}">
<option value="">全部</option>
<option value="int">整型</option>
<option value="float" selected="selected">浮点型</option>
<option value="varchar(256)">字符串型</option>
</c:when>
<c:when test="${type=='varchar(256)'}">
<option value="">全部</option>
<option value="int">整型</option>
<option value="float">浮点型</option>
<option value="varchar(256)" selected="selected">字符串型</option>
</c:when>
<c:otherwise>
<option value="">全部</option>
<option value="int">整型</option>
<option value="float">浮点型</option>
<option value="varchar(256)">字符串型</option>
</c:otherwise>
</c:choose> </select>&nbsp;&nbsp;
<button type="submit" class="btn">
<i class="icon-search"></i> 搜索
</button>
</form>
 
上一篇:Linux磁盘管理及LVM讲解(week2_day2)--技术流ken


下一篇:Angular4 组件间通讯