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 閱讀(307) 評論(0)  編輯  收藏 所屬分類: jsp

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


          網站導航:
           
          主站蜘蛛池模板: 怀集县| 奎屯市| 富裕县| 常熟市| 玉山县| 新源县| 玛曲县| 连城县| 革吉县| 新河县| 华容县| 旬邑县| 麦盖提县| 定州市| 延吉市| 南平市| 桂阳县| 洪雅县| 甘泉县| 谢通门县| 尉犁县| 乌拉特中旗| 郯城县| 泾川县| 万宁市| 南昌市| 乌兰浩特市| 大连市| 喜德县| 蛟河市| 科技| 菏泽市| 沙田区| 衡阳县| 定兴县| 通河县| 禹州市| 雅安市| 红河县| 景宁| 屏东市|