在应用中,我们经常碰到很多模块都要用到分页导航功能,特别是大数据量的应用,还有一些如个人消息、新闻头条、页脚等公共模块也一样,它们的实现代码几乎都一样,只是有个别参数不一样。如果我们在每个模块都COPY相同的代码,这样不但会造成生产效率的低下,而且还会提高维护成本,假设有一天,客户要求修改分页导航的风格,你就不得不去每个具有这种功能的页面去一一修改,那是很耗时间的。所以当我们碰到这种情况时,自然而然的就想到把这些几乎相同的代码抽取出来,放在一个公共的文件中,这样只要在需要这样功能的模块中把这公共文件包含进来就可以了,以后要修改它的风格,或者功能,就只要在这个公共文件中修改就可以了。这样维护起来就方便多了。
下面是一个例子:
-
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
- <%@ page import="com.macronet.pub.db.Page" %>
- <%@ page contentType="text/html; charset=gbk"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>资源动因量采集</title>
- <link rel="stylesheet"
- href="<%=request.getContextPath()%>/css/style.css" type="text/css">
- </head>
- <body>
- <div class="txt_title">
- 资源动因量采集
- </div>
- <logic:present name="alert">
- <logic:equal value="true" name="alert">
- <script type="text/javascript">
- alert('保存成功');
- </script>
- </logic:equal>
- </logic:present>
- <div class="frame_list">
- <form action="ziydylb.do" method="post" name="frm">
- <input type="hidden" name="flag" id="flag" />
- <input type="hidden" id="rowNum" name="rowNum" />
- <input type="hidden" id="pageNo" name="pageNo"
- value="<bean:write name="pageNo"/>" />
- <input type="hidden" id="sForward" name="sForward" value="ziydylb" />
- <input type="hidden" id="sjlx" name="sjlx" value="ZYDY" />
- <table class="tab_list" cellspacing="0">
- <tr>
- <td class="tr_list">
- <div class="txt_tr">
- 采集年月
- <input type="text" name="ny" id="ny" size="6"
- value="<bean:write name="ny" />" onclick="setmon(this)">
- <td class="tr_list">
- <div class="txt_tr">
- 请选择机构:
- </div>
- </td>
- <td class="tr_list">
- <div class="txt_tr">
- <div id="combo"></div>
- <input type="hidden" name="jgbh" id="jgbh"
- value="<bean:write name="jgbh"/>" />
- <input type="hidden" name="jgmc" id="jgmc"
- value="<bean:write name="jgmc"/>" />
- </div>
- </td>
- <td class="tr_list">
- <div class="txt_tr">
- <input type="button" value="查找"
- onclick="setAction('1','showAll')" />
- <c:if test="${MKQX=='3'||MKQX=='4'}">
- <input type="button" value="采集"
- onclick="setAction('<bean:write name="pageNo"/>','dataImportPage')" />
- <input type="button" value="导入"
- onclick="setAction('<bean:write name="pageNo"/>','insertPage')" />
- </c:if>
- <c:if test="${MKQX=='4'}">
- <input type="button" value="清除"
- onclick="setAction('1','clear')" />
- </c:if>
- </div>
- </td>
- </tr>
- </table>
- <br>
- <table class="tab_list" cellspacing="0">
- <tr>
- <td class="tr_list" style="text-align: center;">
- 资源动因编号
- </td>
- <td class="tr_list" style="text-align: center;">
- 资源动因
- </td>
- <td class="tr_list" style="text-align: right;">
- 动因量
- </td>
- <td class="tr_list" style="text-align: left;">
- 单位
- </td>
- </tr>
- <logic:notEmpty name="results">
- <logic:iterate id="result" name="results" indexId="rowId">
- <tr>
- <td class="td_list_odd" align="center" nowrap>
- <bean:write name="result" property="C_DYYSBH" />
- </td>
- <td class="td_list_odd" align="center" nowrap>
- <bean:write name="result" property="V_DYYSMC" />
- <input type="hidden" name="dyysbh" style="border: none"
- id="dyysbh"
- value="<bean:write name="result" property="C_DYYSBH"/>" />
- </td>
- <td class="td_list_odd" align="right" nowrap>
- <c:if test="${MKQX=='3'||MKQX=='4'}">
- <input type="text" name="dyl<%=rowId%>" id="dyl" size="10"
- style="border: none; text-align: right" align="right"
- onfocus="changeStyle(this)" onblur="validator(this)"
- value="<bean:write name="result" property="N_DYL"/>" />
- </c:if>
- <c:if test="${MKQX=='1'||MKQX=='2'}">
- <input type="text" name="dyl<%=rowId%>" id="dyl" size="10"
- readonly style="border: none; text-align: right"
- align="right"
- value="<bean:write name="result" property="N_DYL"/>" />
- </c:if>
- </td>
- <td class="td_list_odd" align="left" nowrap="nowrap">
- <bean:write name="result" property="V_DW" />
- </td>
- </tr>
- </logic:iterate>
- </logic:notEmpty>
- <logic:empty name="results">
- <tr>
- <td class="td_list_odd" colspan="9" align="left">
- <div class="txt_td_pri">
- 没有数据!
- </div>
- </td>
- </tr>
- </logic:empty>
- <!--
- <logic:notEmpty name="results">
- <tr>
- <td class="td_list_odd" colspan="9" align="right">
- <div class="txt_td_pri">
- <c:if test="${MKQX=='3'||MKQX=='4'}">
- <input type="button" value="增加"
- onclick="setAction('<bean:write name="pageNo"/>','beforeAdd')">
- </c:if>
- </div>
- </td>
- </tr>
- </logic:notEmpty>
- -->
- </table>
- <%
- String s="&ny="+request.getAttribute("ny")+"&jgbh="+request.getAttribute("jgbh")+"&jgmc="+request.getAttribute("jgmc");
- request.setAttribute("param",s);
- %>
- <br>
- <jsp:include page="/scripts/common/page.jsp">
- <jsp:param name="actionName" value="/ziydylb.do?flag=showAll"/>
- </jsp:include>
- </form>
- </div>
- </body>
- </html>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
- <%@ page import="com.macronet.pub.db.Page"%>
- <%
- String path = request.getContextPath();
- String formName = request.getParameter("formName");
- String actionName = request.getParameter("actionName");
- %>
- <%@ page pageEncoding="gb2312"%>
- <script type="text/javascript">
- function validation(obj){
- var s=obj.value;
- var patrn=/^[0-9]{1,20}$/;
- if (!patrn.exec(s)&&s!='') {
- alert('请输入一个整数!');
- obj.value='';
- obj.focus();
- return;
- }
- var totalPage=document.getElementById('totalPage').innerText;
- if(parseInt(s)>parseInt(totalPage)){
- alert('你所输入的页码已超出总页数!');
- obj.value='';
- return;
- }
- }
- function setRowNumToPage(action){
- var pageNo=document.getElementById('pageNoTo').value;
- document.getElementById('pageNo').value=pageNo;
- document.getElementById('action').value=action;
- document.forms[0].submit();
- }
- </script>
- <table cellpadding="0" cellspacing="0" class="tab_list">
- <tr class="page_list">
- <td>
- <div>
- <%
- Page p = (Page) request.getAttribute("page");
- int currentPage = p.getCurrentPage();
- %>
- 第<%=currentPage%>/
- <div id="totalPage" style="display: inline"><%=p.getTotalPages()%></div>
- 页 共<%=p.getTotalRecords()%>条记录
- <a
- href="<%=request.getContextPath()%><%=actionName%>&pageNo=1<%=request.getAttribute("param")%>"
- class="pagenav">首页</a>
- <%
- if (p.isHasPrevious()) {
- %>
- <a
- href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=currentPage == 1 ? 1 : currentPage - 1%><%=request.getAttribute("param")%>"
- class="pagenav">上一页</a>
- <%
- } else {
- %>
- 上一页
- <%
- }
- if (p.isHasNext()) {
- %>
- <a
- href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=currentPage == p.getTotalPages() ? p
- .getTotalPages() : currentPage + 1%><%=request.getAttribute("param")%>"
- class="pagenav">下一页</a>
- <%
- } else {
- %>
- 下一页
- <%
- }
- %>
- <a
- href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=p.getTotalPages()%><%=request.getAttribute("param")%>"
- class="pagenav">末页</a>
- </div>
- </td>
- <td align="right">
- <div class="txt_td_pri">
- 转
- <input type="text" size="4" name="pageNoTo" id="pageNoTo"
- onblur="validation(this)" value="">
- 页
- <input type="button" value="跳转" class="btn"
- onclick="setRowNumToPage('search')">
- </div>
- </td>
- </tr>
- </table>