JSP 语法详解

在 JSP 文件中,主要由模板元素(静态的 HTML 和 XML 内容)、注释、指令元素、动作元素、脚本元素(声明、表达式、Scriptlets 和 JSP 内建对象)组成。以下按照官方资料的顺序,分「核心语法」,「指令」,和「标准动作」三部分记录,作为四号标题的链接直接指向官方资料,需要查看详细内容时,猛击它就行了。

Core Syntax 核心语法

Comment 注释

Documents the JSP page but is not inserted into the response. 属于 JSP 文档的一部分,但不会放在页面回应(Response)中去。

注释分几种情况:

  • <!-- comment [ <%=expression%>] --> 仿 HTML/XML 注释,在客户端网页源码中会显示这个注释。
  • <%-- comment --%> JSP 标准注释, 隐藏注释,写在 JSP 程序中,注释内容在发布 JSP 页面时会被完全忽略(不会发给用户).
  • Scriptlets 中的注释,和 Java 代码的注释一样,用//和或者/* */或者/** */,这类注释,客户端是看不到的。<% /**这也是一个 Scriptlet 中的注释,理论上可以用 javadoc 从生成的 Java 文件中提取出注释来*/ %>

下面的代码中,最後在页面代码中,只存在第一条注释,别的都看不到:

<body>
hello, world!
<!-- 1. 这个注释会显示在前端页面吗?会 -->
<%-- 2. 第二个注释呢?不会 --%>
<%
// 3. 第三个注释,不会显示在前端页面
/*
4. 第四个注释,不会显示在前端页面
*/
/**
* 5. 第五个注释,不会显示在前端页面
*/
%>

Declaration 声明

格式: <%! this is declaration%>

e.g.

<%!
public String hello;
public int calc() {
    //......
}
%>

Declares a variable or method valid in the scripting language used in the page. 声明一个页面中的脚本里用到的变量和方法, 在这个页面中, 声明的内容是共享的, 如果不想共享这些内容, 则不要用声明这种方式, 而是在 Scriptlet 里使用的时候再定义.

在由 JSP 编译成的 Servlet 中可以发现, 页面上的声明, 已转化成为 Servlet 的成员变量或成员方法. 方法不能是抽象的, 否则 JSP 对应的 Servlet 无法实例化.

Expression 表达式

Contains an expression valid in the scripting language used in the page. 页面中包含的合法(合表达式语言即 Java 之法)的表达式.

Expressions 标签是以 <%= 为起始, 以 %> 为结尾, 其中间内容包含一段合法的表达式.

语法:<%= expression %>

<%= (new java.util.Date()).toLocaleString(); %> 这一句是不对的, 不能使用分号来结束. 但是同样的表达式用在 Scriptlet 中就需要以分号来结尾了。

<%= ...%> 本质上是 out.write(...) 的语法糖。

Scriptlet 脚本

Contains a code fragment valid in the scripting language used in the page. 简单地说, 就是一段包含在<% %>之间的 Java 代码片段.

EL Expression 表达式语言

Contains an expression in the JSP Expression Language (EL). 本站有更详细的记录, 参看: Expression Language>>

Directives 指令

指令(Directives)主要用来提供整个JSP 网页相关的信息,并且用来设定 JSP 网页的相关属性,例如:网页的编码方式、语法、信息等。

指令一般来说有如下的形式:

<%@ directive {attributte="value"} * %>

指令的这种语法形式尽管简单明了, 但并不是符合 XML 标签闭合要求。

Attribute Directive 属性指令

Declares an attribute of the custom tag defined in the tag file.

Include Directive 包含指令

Includes a resource of text or code when the JSP page is translated. 在JSP 编译时插入一个包含文本或代码的文件,这个包含的过程是静态的,而包含的文件可以是JSP 网页、HTML 网页、文本文件,或是一段Java 程序。注意,包含文件中要避免使用<html>、</html>、<body>、</body>等标签,因为这将会影响在原来JSP网页中同样的标签,这样做有时会导致错误。

语法: <%@ include file = "relativeURLspec" %><jsp:directive.include file = "relativeURLspec" />

注意:

本指令是静态包含, 要求 file 不能是一个变量, 而且 file 路径後中不能传递参数, 且 file 所指的路径必须是相对本 JSP 页面的相对路径. 如果 Java EE 工程没有字符转化的过滤器, 被包含文件的中文可能出现乱码(参本小节的参考页面).

如果一个页面要包含多个页面,那么被包含的页面中要注意不要定义同名的变量,否则会出现冲突。

本站参考: What's the difference between @include and jsp:include?

Page Directive 页面指令

Defines attributes that apply to an entire JSP page. 页面指令是用来定义JSP页面中的全局属性的。一个 JSP 页面可以包含一个或者多个 JSP 页面指令,在编译过程中,所有页面指令都被抽出来用于一个页面,除了 import 外,其他页面指令只能出现一次。import 的时候, 既可以一条一条 import, 也可以一个 import, 不同的类用逗号隔开.

页面指令的语法详细内容参考SUN的官方在线说明:Page Drective

<%@ page 

[ language="java" ] 指定 JSP 容器用什么语言来编译 JSP, 目前只有 Java 可用.

[ extends="package.class" ] 本 JSP 页面编译後的 Servlet 继承自哪个类.

[ import="{package.class | package.*}, ..." ]

[ session="true|false" ] 本 JSP 是否可以使用 session 对象, 即指定一个 HTTP 会话中这个页面是否参与.

[ buffer="none|8kb|sizekb" ] 决定输出流(output stream)是否有缓冲区

[ autoFlush="true|false" ] 缓冲区是否要自动清除, 注意, 缓冲区满了不够用的话, 会抛出异常.

[ isThreadSafe="true|false" ] Servlet 2.4不推荐使用

[ info="text" ] 主要用于表明此JSP页面的相关信息, 可用 servlet.getServletInfo() 获取值, 默认忽略.

[ errorPage="relativeURL" ] 如果本页面发生异常, 将跳转到 relativeURL 的页面上.

[ contentType="mimeType [ ; charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] MIME类型, JSP页面的编码方式.

[ isErrorPage="true|false" ]

[ pageEncoding="characterSet | ISO-8859-1" ]

[ isELIgnored="true|false"] 是否忽略 EL, 默认值是 web.xml 设定的.

%>

需要注意的是,ISO-8859-1是西欧字符集,涉及中文到时候,可改成 UTF-8. 另,Java 中 ISO-8859-1 字符转化为 UTF-8 字符的做法是:new String("an ISO-8859-1 String".getBytes("ISO-8859-1"), "UTF-8").

Tag Directive 标签指令

Similar to the page directive in a JSP page, but applies to tag files instead of JSP pages.

Taglib Directive 标签库指令

Defines a tag library and prefix for the custom tags used in the JSP page. 它的功能是让用户自定义新的标签.

语法: <%@ taglib uri = "tagLibraryURI" prefix="tagPrefix" %><jsp:directive.taglib uri = "tagLibraryURI" prefix="tagPrefix" /> (uri, tag library 存放路径; prefix, 用来区分多个自定义标签的)

示例:

<%@ taglib uri ="/supertags/" prefix="super" %>
……….
<super:doMagic>
……..
……..
</super:doMagic>

本站参考: SimpleTag Handlers and JSP Fragments

Variable Directive 变量指令

Defines an expression language variable exposed by the tag to the calling page.

Standard Actions 标准动作

Action 元素的语法以 XML 为基础,所以,在使用时大小写是有差别的。

第一类. 存取JavaBean的

  1. <jsp:useBean> Instantiates or references a bean with a specific name and scope.
  2. <jsp:setProperty> Sets a bean property value or values.
  3. <jsp:getProperty> Inserts the value of a bean property into the response.

第二类. JSP 1.2 原有的

  1. <jsp:include> Includes a static resource or the result from another web component. <jsp:include>元素允许你包含动态和静态文件,这两种产生的结果是不尽相同的。如果包含进来的只是静态文件,那么只是把静态文件的内容加到JSP 网页中;如果包含进来的为动态文件,那么这个被包含的文件也会被JSP Container 编译执行。
  2. <jsp:forward> Forwards a request to an HTML file, JSP page, or servlet. 将客户端所发出来的请求,从一个JSP 网页转交给另一个JSP 网页。虽然转向了, 但地址栏的内容没有变化. 如果你加上<jsp:param>标签,你就能够向目标文件传送参数和值,不过这些目标文件必须也是一个能够取得这些请求参数的动态文件,例如:.cgi、.php、.asp 等等。<jsp:forward>只有一个属性page。page 的值,可以是一个相对路径,即你所要重新导向的网页位置,亦可以是经过表达式运算出的相对路径。
  3. <jsp:param> <jsp:param>用来提供key/value 的信息,它也可以与<jsp:include>、<jsp:forward>和<jsp:plugin> 一起搭配使用。
  4. <jsp:plugin> Causes the execution of an applet or bean. The applet or bean executes in the specified plugin. If the plugin is not available, the client displays a dialog to initiate the download of the plugin software.
  5. <jsp:params>
  6. <jsp:fallback>

第三类. 主要用在 JSP Document 之中的

其中<jsp:output>是 JSP 2.0 新增的元素。

  1. <jsp:root> Defines standard elements and namespace attributes of tag libraries.
  2. <jsp:declaration>
  3. <jsp:scriptlet>
  4. <jsp:expression>
  5. <jsp:text> Encloses template data.
  6. <jsp:output> Specifies the XML declaration or the document type declaration in the request output of a JSP document or a tag file that is in XML syntax.

第四类. 动态产生 XML 元素标签的值

这3个都是在 JSP 2.0 中加入进来的元素。

  1. <jsp:attribute> Used as a substitute for attributes of standard or custom actions.
  2. <jsp:body> Explicitly defines an element body. This tag is required if the enclosing element contains any jsp:attribute tags.
  3. <jsp:element> Dynamically generates an XML element. 用来动态定义 XML 元素标签的值。语法:
      1. <jsp:element name="name">
      2. 本体内容
      3. </jsp:element>(TODO 补充例子)

第五类. 用在 Tag File 中的

  1. <jsp:invoke> Evaluates a fragment attribute.
  2. <jsp:doBody> Evaluates the body of the tag used by the calling page to invoke this tag file.

事先打算按照《JSP 应用开发详解》一书第三章的内容来记录,但看附一的官方资料後,改成用 SUN 的资料来重写,SUN 介绍了就不写,记录一点自以为需要的。