posts - 23,comments - 12,trackbacks - 0

          The include Directive


          The following is the syntax for the include directive:

          <%@ include file="relativeURL" %>

          As you can see the directive accepts a single file attribute that is used to indicate the resource whose content is to be included in the declaring JSP. The file attribute is interpreted as a relative URL; if it starts with a slash it's interpreted as relative to the context of the web application (namely a context-relative path), otherwise it's interpreted as relative to the path of the JSP that contains the include directive (namely a page relative path). The included file may contain either static content, such as HTML or XML, or another JSP page.

          For example:
          <%@ include file="/copyright.html"%>


          Let's consider a real-world example of such a templating mechanism that utilizes the include directive to provide a consistent page layout for a web application.

          Consider the following two JSP pages:

          Header.jsp
              <html>
                <head><title>A Very Simple Example</title></head>
                <body style="font-family:verdana,arial;font-size:10pt;">
                  <table width="100%" height="100%">
                    <tr bgcolor="#99CCCC">
                      <td align="right" height="15%">Welcome to this example...</td>
                    </tr>
                    <tr>
                      <td height="75%">

          Footer.jsp
                     </td>
                   </tr>
                   <tr bgcolor=" #99CC99">
                     <td align="center" height="10%">Copyright ACompany.com 2003</td>
                   </tr>
                 </table>
               </body>
             </html>

          As you can see, Header.jsp declares the starting elements of an HTML table that is to be 100 percent of the size of the page and has two rows, whereas Footer.jsp simply declares the closing elements for the table. Used separately, either JSP will result in partial HTML code that will look very strange to a user but when they're combined using the include directive it's easy to create consistent pages as part of a web application.

          Let's see just how simple this basic template mechanism is to use:

          Content.jsp
              <%@ include file='./Header.jsp'%>
              <p align="center">The Content Goes Here...!!!</p>
              <%@ include file='./Footer.jsp'%>

          2、
          date.jsp
          <html>
            <body>
              <h2>Greetings!</h2>
           <P>The current time is <%=new java.util.Date()%> precisely
            </body>
          </html>

          3、
          dateBean.jsp
          <html>
              <head><title>Professional JSP, 3rd Edition</title></head>
              <body style="font-family:verdana;font-size:10pt;">
                  <jsp:useBean id="date" class="com.apress.projsp20.ch01.DateFormatBean"/>
                  <h2>Today's date is <%= date.getDate() %></h2>
              </body>
          </html>

          或:
          dateBean_getProperty.jsp
          <html>
              <head><title>Professional JSP, 3rd Edition</title></head>
              <body style="font-family:verdana;font-size:10pt;">
                  <jsp:useBean id="date" class="com.apress.projsp20.ch01.DateFormatBean"/>
                  <h2>Today's date is <jsp:getProperty name="date" property="date"/></h2>
              </body>
          </html>

          dateBean_setProperty.jsp
          <html>
              <head><title>Professional JSP, 3rd Edition</title></head>
              <body style="font-family:verdana;font-size:10pt;">
                  <jsp:useBean id="date" class="com.apress.projsp20.ch01.DateFormatBean"/>
                  <jsp:setProperty name="date" property="format"
                                   value="EEE, d MMM yyyy HH:mm:ss z"/>
                  <h2>Today's date is <jsp:getProperty name="date" property="date"/></h2>
              </body>
          </html>

          其中DateFormatBean.java:
             package com.apress.projsp20.ch01;

              import java.util.Date;
              import java.text.*;

              public class DateFormatBean {
                private DateFormat dateFormat;
                private Date date;

                public DateFormatBean() {
                  dateFormat = DateFormat.getInstance();
                  date = new Date();
                }

                public String getDate() {
                  return dateFormat.format(date);
                }

                public void setDate(Date date) {
                  this.date = date;
                }

                public void setFormat(String format) {
                  this.dateFormat = new SimpleDateFormat(format);
                }
              }
          例:SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

          posted on 2005-08-24 17:20 my java 閱讀(303) 評論(0)  編輯  收藏 所屬分類: jsp

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 庆云县| 金湖县| 汶上县| 五河县| 慈溪市| 远安县| 湄潭县| 慈利县| 文山县| 洛浦县| 和田市| 深圳市| 荆门市| 乾安县| 鹤庆县| 镇平县| 绥化市| 紫阳县| 禹城市| 浑源县| 儋州市| 锡林郭勒盟| 全南县| 纳雍县| 卢龙县| 菏泽市| 甘谷县| 台湾省| 安溪县| 龙山县| 青铜峡市| 肥东县| 株洲县| 区。| 沙河市| 航空| 永清县| 延庆县| 寻甸| 邵阳县| 云霄县|