首页 > 开发 > JSP > 正文

JSP基础知识之变量数据的HTML模板语法

2023-05-01 14:14:51
字体:
来源:转载
供稿:网友

当页面被返回给浏览器时,应用服务器HTML 模板语法使您能将变量字段放在 HTML 页面上,并使 Servlet 和 JavaBean 可利用数据库的值动态地替换变量。该功能是 JSP 的一个 IBM 扩展,它使引用变量数据变得十分容易。该语法只能用于 JSP 文件中。HTML 模板语法包括:

基本 HTML 模板语法;

替代 HTML 模板语法;

<REPEAT>标记。

这些标记通过 HTML 编写工具被设计成传递交互操作的标记。每一个标记有一个相应的结束标记。每一个标记是分大小写的,它们的一些属性也是分大小写的。IBM WebSphere Studio 使开发 JSP 文件以包含 HTML 模板语法变得更为容易。

(1) 基本 HTML 模板语法

<INSERT> 标记是用于指定变量字段的基本标记。一般的语法为:

<insert requestparm=pvalue requestattr=avalue bean=name
  property=property_name(optional_index).subproperty_name(optional_index)
  default=value_when_null>
  </insert>

其中的属性及其含义如下:

requestparm:要在请求对象内进行访问的参数。该属性是分大小写的,并且不能与Bean 和property属性一起使用。

Requestattr:要在请求对象内进行访问的属性。属性应使用 setAttribute 方法设置。该属性是分大小写的,并且不能与 Bean 和property属性一起使用。

Bean:由 <BEAN> 标记在 JSP 文件中说明的 JavaBean 名。请参阅访问 JavaBean 以获得 <BEAN> 标记的解释。该属性的值是分大小写的。当指定了 Bean 属性,但未指定property属性时,在替换中将使用完整的 Bean 。例如,如果 Bean 是类型 String 且未指定属性,则将替代 string 的值。

Property:访问替换的 Bean 的属性。该属性的值是分大小写的,并且是属性的独立场所名。该属性不能与 requestparm 属性一起使用。

Default:当 Bean 属性值为空时,显示可选的字符串。如果字符串包含多个字,则该字符串必须包含在一对双引号中(例如 "HelpDesk number")。该属性的值是分大小写的。如果未指定值,则当属性值为空时,用空字符串替代。

基本语法的示例如下:

<insert bean=userProfile property=username></insert>
  <insert requestparm=company default="IBM Corporation"></insert>
  <insert requestattr=ceo default="Company CEO"></insert>
  <insert bean=userProfile property=lastconnectiondate.month></insert>

在大多数情况下,property属性的值就是属性名。但是,可以通过指定property属性的全格式来访问属性的某一属性(子属性)。这个全格式也提供选择项给您来指定索引属性的一个索引。该可选的索引可以是一个常数(例如 2)或如重复设置 HTML 标记中描述的索引。使用属性标记的全格式示例如下:

<insert bean=staffQuery property=address(currentAddressIndex)></insert>
  <insert bean=shoppingCart property=items(4).price></insert>
  <insert bean=fooBean property=foo(2).bat(3).boo.far></insert>

(2) 替代 HTML 模板语法

HTML 标准不允许在 HTML 标记中嵌入 HTML 标记。因而,就无法在另一个 HTML 标记中嵌入<INSERT>标记。作为代替,请使用 HTML 模板替代语法。要使用替代语法:

请使用<INSERT>和</INSERT>来包含 HTML 标记,在该标记中指出替代内容。

指定 Bean 和property属性:

要指定 Bean 和属性特性,请使用下列格式:$(bean=b property=p default=d),其中 b、p 和 d
作为描述基本语法的值。

要指定 requestparm 属性,请使用下列格式:$(requestparm=r default=d),其中 r 和 d 作为
描述基本语法的值。

要指定 requestattr 属性,请使用下列格式:$(requestattr=r default=d),其中 r 和 d 作为描述
基本语法的值。

 替代 HTML 模板语法的示例如下:

<insert>
  <img src=$(bean=productAds property=sale default=default.gif)>
  </insert>
  <insert>
  <a href="http://www.myserver.com/map/showmap.cgi?country=$(requestparm=country default=
  usa )
  &city$(requestparm=city default="Research Triangle Park")&email=
  $(bean=userInfo property=email)>Show map of city</a>
  </insert>

(3) <REPEAT>标记

<REPEAT>标记的语法为:

<repeat index=name start=starting_index end=ending_index>
  </repeat>

其中:

index:是用于标识该重复数据块的一个可选的名称。该值是分大小写的。

Start:是用于该重复数据块的一个可选的开始索引值。缺省值为 0 。

End:是用于该重复数据块的一个可选的结束索引值。最大值是 2,147,483,647。如果结束属性的值小于开始属性的值,则忽略结束属性。

下面的示例 1、2 和 3 显示了如何使用<REPEAT>标记。如果所有的索引属性拥有 300 个或更少的元素,则这些示例将产生相同的输出。如果拥有的元素多于 300 个,示例 1 和示例 2 将显示所有的元素,而示例 3 将只显示前 300 个元素。示例 1 用缺省开始和结束索引显示了隐式索引:使用最小索引属性数的 bean 限制了循环重复的次数。

<table>
  <repeat>
  <tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
  <tr><td><insert bean=serviceLocationsQuery property=address></insert></tr></td>
  <tr><td><insert bean=serviceLocationsQuery property=telephone></insert></tr></td>
  </repeat>
  </table>

示例 2 显示了索引、开始索引和结束索引:

<table>
  <repeat index=myIndex start=0 end=2147483647>
  <tr><td><insert bean=serviceLocationsQuery property=city(myIndex)></insert></tr></td>
  <tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr>
  </td>
  <tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr>
  </td>
  </repeat>
  </table>

示例 3 用隐式开始索引显示了显式索引和结束索引。虽然指定了索引属性,仍可对索引过的属性城市进行隐式索引,因为不需要(i)。

<table>
  <repeat index=myIndex end=299>
  <tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
  <tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr>
  </td>
  <tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr>
  </td>
  </repeat>
  </table>

可以嵌套<REPEAT>数据块。独立索引每个数据块。该能力对交叉两个 bean 上的交错属性或含有子属性的属性非常有用。在示例中,将两个<REPEAT>数据块嵌套,用以显示用户购物手推车上每一张压缩光盘上的歌曲列表。

<repeat index=cdindex>
  <h1><insert bean=shoppingCart property=cds.title></insert></h1>
  <table>
  <repeat>
  <tr><td><insert bean=shoppingCart property=cds(cdindex).playlist></insert>
  </td></tr>
  </table>
  </repeat>
  </repeat>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表