如何实现JSP网页模板 JSP网页母版

来源:互联网 发布:2017淘宝不好做 编辑:程序博客网 时间:2024/04/25 14:43

一般情况下,我们做网站的时候,其实,页脚,横幅,左菜单都是一样的

如果在每个页面都写一次的话,就造成了资源浪费。通常情况下,我们的解决办法就是使用网络模板。其实很简单就是用来一个<jsp:include page="url"/>标签,将重复使用的代码写到一个页面上,然后将其引用过来。
实例:
main.jsp:
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
   <head>
      <title>Template Page Sample</title>
   </head>
   <body >
      <%-- One table lays out all of the content for this page --%>
      <table width="100%" height="100%">
         <tr>
            <%-- Header section section --%>
            <td width="150" valign="top" align="left" bgcolor="#CCFFCC">
               <jsp:include page="header.jsp"/>
            </td>
         </tr>
         <tr>
            <%-- Main content section --%>
            <td height="100%" width="*">
               <table width="100%" height="100%">
                  <tr>
                     <%-- Sidebar section --%>
                     <td valign="top" height="15%">
                        <jsp:include page="sidebar.jsp"/>
                     </td>
                  
                     <%-- Content section --%>
                     <td valign="top" height="*">
                        <jsp:include page="indexContent.jsp"/>
                     </td>
                  </tr>
                </table>
             </td>
         </tr>
         <tr>
             <%-- Footer section --%>
             <td valign="bottom" height="15%">
                 <jsp:include page="footer.jsp"/>
             </td>
         </tr>
      </table>
   </body>
</html>
然后相应的页面写上我们设置的代码就可以了。
0 0