Java开发之JSP指令

来源:互联网 发布:穿越火线数据异常 编辑:程序博客网 时间:2024/06/05 18:33
一、page指令

     page指令是最常用的指令,用来说明JSP页面的属性等。JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里。但需要注意的是,无论在哪个page指令里的属性,任何page允许的属性都只能出现一次,否则会出现编译错误。import属性除外,可以出现多次。属性名称区分大小写。

page指令的常见属性属性名称取值范围描述languagejava指明解释该JSP文件时采用的语言。一般为Java语言。默认为Java。extends任何类的全名指明编译该JSP文件时继承哪个类。jsp为Servlet,因此当指明继承普通类时需要实现Servlet的init、destroy等方法。import任何包名,类名引入该JSP中用到的类、包等。import是唯一可以声明多次的page指令属性。一个import属性可以引用多个类,中间用英文逗号隔开,如<%@ page import="java.util.List,java.util.ArrayList"%>。sessiontrue,false指明该JSP内是否内置Session对象。如果为true,则内置Session对象,可直接使用。否则不内置Session对象。默认为true。 autoFlushtrue,false是否运行缓存。如果为true,则使用out.println()等方法输出的字符串并不是立刻到达客户端服务器的,而是暂存在缓存里,缓存满或者程序执行完毕或者执行out.flush()操作时才到客户端。默认为true。 buffernone或者数字+kb指定缓存大小。当autoFlush设为true时有效,例如:<%@ page buffer="10kb"%>。 isThreadSafetrue,false指定是否线程安全。如果为true,则运行多个线程同时运行该JSP程序,否则只运行一个线程运行,其余线程等待。默认为false。 isErrorPagetrue,false指定该页面是否为错误处理页面,如果为true,则该JSP内置有一个Exception对象exception,可直接使用,否则没有,默认为fasle。errorPage某个JSP页面的相对路径指明一个错误显示页面,如果该JSP程序抛出了一个未捕捉的异常,则转到errorPage指定的页面。errorPage指定的页面通常isErrorPage属性为true,且内置的excception对象为未捕捉的异常。 contentType有效的文档类型

客户端浏览器根据该属性判断文档类型,例如:

HTML格式为text/html

纯文本格式为text/plain

JPG图像为image/jpeg

GIF图像为image/gif

WORD文档为application/msword 

info任意字符串指明JSP的信息。该信息可以通过Servlet.getServletInfo()方法获取到。 trimDirectiveWhitespacestrue,false是否去掉指令前后的空白字符。默认为false。 

trimDirectiveWhitespaces=“false”(默认为false)时HTML代码效果图:

trimDirectiveWhitespaces=“true”时HTML代码效果图:

 注意:在HTML文件中,空行是不影响显示效果的。但如果输出的是XML文件,则可能有问题,因为某些XML解析器不允许XML文件前面有空行。

二、include指令

1.重点说明     

      include指令只有一种格式:<%@ include file="relativeURL"%>。relativeURL为本应用程序内另一个JSP文件或者HTML文件的路径。include指令用来实现JSP页面的的区块化。

2.代码实践和效果图

Head.jsp(导航栏内容)

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2  3  4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6   <head> 7      8      9     <title>My JSP 'Head.jsp' starting page</title>10     11     <meta http-equiv="pragma" content="no-cache">12     <meta http-equiv="cache-control" content="no-cache">13     <meta http-equiv="expires" content="0">    14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">15     <meta http-equiv="description" content="This is my page">16     <!--17     <link rel="stylesheet" type="text/css" href="styles.css">18     -->19 20   </head>21   22   <body>23     <table width="100%" cellspacing=1 bgcolor=#999999>24        <tr>25           <td bgcolor=#666666 colspan="7"26               style="color:#FFFFFF; font-size:40px; height:60px;">27               Hello World28           </td>29        </tr>30        <tr>31           <td bgcolor=#DDDDDD align="center">首页</td>32           <td bgcolor=#DDDDDD align="center">百科</td>33           <td bgcolor=#DDDDDD align="center">文档</td>34           <td bgcolor=#DDDDDD align="center">下载</td>35           <td bgcolor=#DDDDDD align="center">关于</td>36           <td bgcolor=#DDDDDD align="center">邮件</td>37           <td bgcolor=#DDDDDD align="center">社区</td>38        </tr>39     </table>40   </body>41 </html>

Foot.jsp(版权内容)

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2  3  4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6   <head> 7     8      9    10     11     <meta http-equiv="pragma" content="no-cache">12     <meta http-equiv="cache-control" content="no-cache">13     <meta http-equiv="expires" content="0">    14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">15     <meta http-equiv="description" content="This is my page">16     <!--17     <link rel="stylesheet" type="text/css" href="styles.css">18     -->19 20   </head>21   22   <body>23      <table width="100%" cellspacing=1 bgcolor=#CCCCCC>24         <tr>25             <td align="center" bgcolor=#666666 style="color: #FFFFFF;font-size:14px;height:20px;">26                Copyright 2015-2016 &copyKing27             </td>28         </tr>29      </table>30   </body>31 </html>

Include.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ page trimDirectiveWhitespaces="true" %><!-- 是否去掉指令前后的空白字符。默认为false --> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html>10   <head>11     <base href="<%=basePath%>">12     13     14     <meta http-equiv="pragma" content="no-cache">15     <meta http-equiv="cache-control" content="no-cache">16     <meta http-equiv="expires" content="0">    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18     <meta http-equiv="description" content="This is my page">19     <!--20     <link rel="stylesheet" type="text/css" href="styles.css">21     -->22 23   </head>24   25   <body>26     <%@ include file="Head.jsp" %>     <!--include指令 -->27     <p style="line-height:22px; text-indent:2em;">拉布拉多猎犬因原产地在加拿大的纽芬兰与拉布拉多省而得名。28     拉布拉多犬是一种中大型犬类,个性忠诚、大气、憨厚、温和、阳光、开朗、活泼,智商极高,也对人很友善,29     是非常适合被选作经常出入公共场合的导盲犬或地铁警犬及搜救犬和其他工作犬的狗品种,30     跟哈士奇(西伯利亚雪撬犬)和金毛猎犬并列三大无攻击性犬类,拉布拉多智商位列世界犬类第七。</p>31     <%@ include file="Foot.jsp" %>     <!--include指令 -->32   </body>33 </html>

3.include行为和include指令

    JSP还提供了另一种包含文件的行为(include行为):<jsp:include page="relativeURL">命令。该命令与include指令使用方法基本一致。不同的是include指令是把Head.jsp和Foot.jsp的源代码添加到Include.jsp中然后再编译成一个class文件,属于先包含后编译。而include行为则是运行时单独执行Head.jsp和Foot.jsp,然后把执行结果包含到Include.jsp中,属于先运行后包含行为

    除了上面两种方法包含文件外,还可以在web.xml中通过JSP配置来包含文件。

1 <jsp-config>2      <jsp-property-group>3         <include-prelude>/Head.jspf</include-prelude><!--在执行JSP之前执行的文件  -->4         <include-coda>/Foot.jspf</include-coda><!--在执行JSP之后执行的文件  -->5      </jsp-property-group>6   </jsp-config>

 

三、taglib指令

      JSP支持标签技术。使用标签功能能够实现视图代码重用。要使用标签功能必须先声明标签库以及标签前缀。taglib指令用来指明JSP页面内使用的JSP标签库。taglib指令有两个属性,uri为类库的地址,prifix为标签的前缀。

 1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 2 <html> 3 <head> 4 </head> 5 <body> 6      <c:forEach var="item" items="${arrays}"> 7              <c:out value="item"></c:out> 8      </c:forEach> 9 </body>10 </body>11 </html>

 

原创粉丝点击