用jsp:include 包含分页导航、页脚等一些公共模块

时间:2022-03-11 18:15:27

在应用中,我们经常碰到很多模块都要用到分页导航功能,特别是大数据量的应用,还有一些如个人消息、新闻头条、页脚等公共模块也一样,它们的实现代码几乎都一样,只是有个别参数不一样。如果我们在每个模块都COPY相同的代码,这样不但会造成生产效率的低下,而且还会提高维护成本,假设有一天,客户要求修改分页导航的风格,你就不得不去每个具有这种功能的页面去一一修改,那是很耗时间的。所以当我们碰到这种情况时,自然而然的就想到把这些几乎相同的代码抽取出来,放在一个公共的文件中,这样只要在需要这样功能的模块中把这公共文件包含进来就可以了,以后要修改它的风格,或者功能,就只要在这个公共文件中修改就可以了。这样维护起来就方便多了。

 

下面是一个例子:

 

 

 

    1. <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
    2. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
    3. <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
    4. <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
    5. <%@ page import="com.macronet.pub.db.Page" %>
    6. <%@ page contentType="text/html; charset=gbk"%>
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    8. <html>
    9.     <head>
    10.         <title>资源动因量采集</title>
    11.         <link rel="stylesheet"
    12.             href="<%=request.getContextPath()%>/css/style.css" type="text/css">
    13.             </head>
    14.     <body>
    15.         <div class="txt_title">
    16.             资源动因量采集
    17.         </div>
    18.         <logic:present name="alert">
    19.             <logic:equal value="true" name="alert">
    20.                 <script type="text/javascript">
    21.                     alert('保存成功');
    22.                     </script>
    23.             </logic:equal>
    24.         </logic:present>
    25.         <div class="frame_list">
    26.             <form action="ziydylb.do" method="post" name="frm">
    27.                 <input type="hidden" name="flag" id="flag" />
    28.                 <input type="hidden" id="rowNum" name="rowNum" />
    29.                 <input type="hidden" id="pageNo" name="pageNo"
    30.                     value="<bean:write name="pageNo"/>" />
    31.                 <input type="hidden" id="sForward" name="sForward" value="ziydylb" />
    32.                 <input type="hidden" id="sjlx" name="sjlx" value="ZYDY" />
    33.                 <table class="tab_list" cellspacing="0">
    34.                     <tr>
    35.                         <td class="tr_list">
    36.                             <div class="txt_tr">
    37.                                 采集年月  
    38.                                 <input type="text" name="ny" id="ny" size="6"
    39.                                     value="<bean:write name="ny" />" onclick="setmon(this)">
    40.                         <td class="tr_list">
    41.                             <div class="txt_tr">
    42.                                 请选择机构:
    43.                             </div>
    44.                         </td>
    45.                         <td class="tr_list">
    46.                             <div class="txt_tr">
    47.                                 <div id="combo"></div>
    48.                                 <input type="hidden" name="jgbh" id="jgbh"
    49.                                     value="<bean:write name="jgbh"/>" />
    50.                                 <input type="hidden" name="jgmc" id="jgmc"
    51.                                     value="<bean:write name="jgmc"/>" />
    52.                             </div>
    53.                         </td>
    54.                         <td class="tr_list">
    55.                             <div class="txt_tr">
    56.                                 <input type="button" value="查找"
    57.                                     onclick="setAction('1','showAll')" />
    58.                                 <c:if test="${MKQX=='3'||MKQX=='4'}">
    59.                                     <input type="button" value="采集"
    60.                                         onclick="setAction('<bean:write name="pageNo"/>','dataImportPage')" />
    61.                                     <input type="button" value="导入"
    62.                                         onclick="setAction('<bean:write name="pageNo"/>','insertPage')" />
    63.                                 </c:if>
    64.                                 <c:if test="${MKQX=='4'}">
    65.                                     <input type="button" value="清除"
    66.                                         onclick="setAction('1','clear')" />
    67.                                 </c:if>
    68.                             </div>
    69.                         </td>
    70.                     </tr>
    71.                 </table>
    72.                 <br>
    73.                 <table class="tab_list" cellspacing="0">
    74.                     <tr>
    75.                         <td class="tr_list" style="text-align: center;">
    76.                             资源动因编号
    77.                         </td>
    78.                         <td class="tr_list" style="text-align: center;">
    79.                             资源动因
    80.                         </td>
    81.                         <td class="tr_list" style="text-align: right;">
    82.                             动因量
    83.                         </td>
    84.                         <td class="tr_list" style="text-align: left;">
    85.                             单位
    86.                         </td>
    87.                     </tr>
    88.                     <logic:notEmpty name="results">
    89.                         <logic:iterate id="result" name="results" indexId="rowId">
    90.                             <tr>
    91.                                 <td class="td_list_odd" align="center" nowrap>
    92.                                     <bean:write name="result" property="C_DYYSBH" />
    93.                                 </td>
    94.                                 <td class="td_list_odd" align="center" nowrap>
    95.                                     <bean:write name="result" property="V_DYYSMC" />
    96.                                     <input type="hidden" name="dyysbh" style="border: none"
    97.                                         id="dyysbh"
    98.                                         value="<bean:write name="result" property="C_DYYSBH"/>" />
    99.                                 </td>
    100.                                 <td class="td_list_odd" align="right" nowrap>
    101.                                     <c:if test="${MKQX=='3'||MKQX=='4'}">
    102.                                         <input type="text" name="dyl<%=rowId%>" id="dyl" size="10"
    103.                                             style="border: none; text-align: right" align="right"
    104.                                             onfocus="changeStyle(this)" onblur="validator(this)"
    105.                                             value="<bean:write name="result" property="N_DYL"/>" />
    106.                                     </c:if>
    107.                                     <c:if test="${MKQX=='1'||MKQX=='2'}">
    108.                                         <input type="text" name="dyl<%=rowId%>" id="dyl" size="10"
    109.                                             readonly style="border: none; text-align: right"
    110.                                             align="right"
    111.                                             value="<bean:write name="result" property="N_DYL"/>" />
    112.                                     </c:if>
    113.                                 </td>
    114.                                 <td class="td_list_odd" align="left" nowrap="nowrap">
    115.                                     <bean:write name="result" property="V_DW" />
    116.                                      
    117.                                 </td>
    118.                             </tr>
    119.                         </logic:iterate>
    120.                     </logic:notEmpty>
    121.                     <logic:empty name="results">
    122.                         <tr>
    123.                             <td class="td_list_odd" colspan="9" align="left">
    124.                                 <div class="txt_td_pri">
    125.                                     没有数据!
    126.                                 </div>
    127.                             </td>
    128.                         </tr>
    129.                     </logic:empty>
    130.                     <!-- 
    131.                     <logic:notEmpty name="results">
    132.                         <tr>
    133.                             <td class="td_list_odd" colspan="9" align="right">
    134.                                 <div class="txt_td_pri">
    135.                                     <c:if test="${MKQX=='3'||MKQX=='4'}">
    136.                                         <input type="button" value="增加"
    137.                                             onclick="setAction('<bean:write name="pageNo"/>','beforeAdd')">
    138.                                     </c:if>
    139.                                 </div>
    140.                             </td>
    141.                         </tr>
    142.                     </logic:notEmpty>
    143.                      -->
    144.                 </table>
    145.                 <%
    146.                    String s="&ny="+request.getAttribute("ny")+"&jgbh="+request.getAttribute("jgbh")+"&jgmc="+request.getAttribute("jgmc");
    147.                    request.setAttribute("param",s);
    148.                  %>
    149.                 <br>
    150.                 <jsp:include page="/scripts/common/page.jsp">
    151.                     <jsp:param name="actionName" value="/ziydylb.do?flag=showAll"/>
    152.                 </jsp:include>
    153.             </form>
    154.         </div>
    155.     </body>
    156. </html>
    <%@ taglib uri=
    "/WEB-INF/struts-bean.tld" prefix="bean"%>
  1. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
  2. <%@ page import="com.macronet.pub.db.Page"%>
  3. <%
  4.     String path = request.getContextPath();
  5.     String formName = request.getParameter("formName");
  6.     String actionName = request.getParameter("actionName");
  7. %>
  8. <%@ page pageEncoding="gb2312"%>
  9. <script type="text/javascript">
  10.             function validation(obj){
  11.                 var s=obj.value;
  12.                 var patrn=/^[0-9]{1,20}$/;
  13.                 if (!patrn.exec(s)&&s!='') { 
  14.                  alert('请输入一个整数!');
  15.                  obj.value='';
  16.                  obj.focus(); 
  17.                  return;
  18.                 }
  19.                 var totalPage=document.getElementById('totalPage').innerText;
  20.                 if(parseInt(s)>parseInt(totalPage)){
  21.                        alert('你所输入的页码已超出总页数!');
  22.                        obj.value='';
  23.                        return;
  24.                 }
  25.                 
  26.             }
  27.             function setRowNumToPage(action){
  28.                var pageNo=document.getElementById('pageNoTo').value;
  29.                document.getElementById('pageNo').value=pageNo;
  30.                document.getElementById('action').value=action;
  31.                document.forms[0].submit();
  32.             }
  33. </script>
  34. <table cellpadding="0" cellspacing="0" class="tab_list">
  35.     <tr class="page_list">
  36.         <td>
  37.             <div>
  38.                 <%
  39.                     Page p = (Page) request.getAttribute("page");
  40.                     int currentPage = p.getCurrentPage();
  41.                 %>
  42.                 第<%=currentPage%>/
  43.                 <div id="totalPage" style="display: inline"><%=p.getTotalPages()%></div>
  44.                 页 共<%=p.getTotalRecords()%>条记录
  45.                 <a
  46.                     href="<%=request.getContextPath()%><%=actionName%>&pageNo=1<%=request.getAttribute("param")%>"
  47.                     class="pagenav">首页</a>
  48.                 <%
  49.                     if (p.isHasPrevious()) {
  50.                 %>
  51.                 <a
  52.                     href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=currentPage == 1 ? 1 : currentPage - 1%><%=request.getAttribute("param")%>"
  53.                     class="pagenav">上一页</a>
  54.                 <%
  55.                     } else {
  56.                 %>
  57.                 上一页
  58.                 <%
  59.                     }
  60.                     if (p.isHasNext()) {
  61.                 %>
  62.                 <a
  63.                     href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=currentPage == p.getTotalPages() ? p
  64.                         .getTotalPages() : currentPage + 1%><%=request.getAttribute("param")%>"
  65.                     class="pagenav">下一页</a>
  66.                 <%
  67.                     } else {
  68.                 %>
  69.                 下一页
  70.                 <%
  71.                     }
  72.                 %>
  73.                 <a
  74.                     href="<%=request.getContextPath()%><%=actionName%>&pageNo=<%=p.getTotalPages()%><%=request.getAttribute("param")%>"
  75.                     class="pagenav">末页</a> 
  76.             </div>
  77.         </td>
  78.         <td align="right">
  79.             <div class="txt_td_pri">
  80.                 转
  81.                 <input type="text" size="4" name="pageNoTo" id="pageNoTo"
  82.                     onblur="validation(this)" value="">
  83.                 页  
  84.                 <input type="button" value="跳转" class="btn"
  85.                     onclick="setRowNumToPage('search')">
  86.             </div>
  87.         </td>
  88.     </tr>
  89. </table>