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 阅读(288) 评论(0)  编辑  收藏 所属分类: jsp

只有注册用户登录后才能发表评论。


网站导航: