JSP设计-第11章(3)

<jsp:attribute>:
范例2:
<jsp:element name="firstname">
<jsp:attribute name="name">Mike</jsp:attribute>
<jsp:body>Hello</jsp:body>
</jsp:element>

执行的结果如下:
<firstname name="Mike">Hello</firstname>
<jsp:attribute>元素主要有两个用途:
(1) 当使用在<jsp:element>之中时,它可以定义XML 元素的属性,如上述的范例2。
(2) 它可以用来设定标准或自定义标签的属性值。如下范例1:
<jsp:attribute>的语法:
<jsp:attribute name="name" trim="true | false">
本体内容
</jsp:attribute >
<jsp:attribute>有两个属性:name 和trim。其中name 的值就是标签的属性名称。trim 可为
true 或false。假若为true 时,<jsp:attribute>本体内容的前后空白,将被忽略;反之,若为false,
前后空白将不被忽略。trim 的默认值为true。
范例1:
<jsp:useBean id="foo" class="jsp2.examples.FooBean">
Bean created! Setting foo.bar...<br>
<jsp:setProperty name="foo" property="bar">
<jsp:attribute name="value">
Hello World
</jsp:attribute>
</jsp:setProperty>
</jsp:useBean>
<br>
Result: <jsp:getProperty name="foo" property="bar">
其实上述的范例和下面的例子一样:
<jsp:useBean id="foo" class="jsp2.examples.FooBean">
Bean created! Setting foo.bar...<br>
<jsp:setProperty name="foo" property="bar" value="Hello World" >
</jsp:setProperty>
</jsp:useBean>
<br>
Result: <jsp:getProperty name="foo" property="bar">
<c:set var="message">中加入了set体,这种写法就把set体中的内容加入到var里
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>

<%-- Create test data --%>
<c:set var="message">
  This is just a lot of text that the browser will format to
  fit the browser window. Attempts to <blink> add HTML elements
  are dealt with by conversion to character entities.
  [code]
  This part I want the browser to leave alone, so that
  all my indentations are left intact:

    public class Foo {
      public String getBar(  ) {
        return bar;
      }
    }
  [/code]
  And then some regular text again.
</c:set>
<html>
  <head>
    <title>Online Forum</title>
  </head>
  <body bgcolor="white">
    <h1>Online Forum</h1>
    Here's a formatted message:
    <p>
      <my:htmlFormat>
        ${message}
      </my:htmlFormat>
    </p>
  </body>
</html>
 
Example 11-4. Processing the body (htmlFormat.tag)

doBody对message进行处理

注意要设置body-content为scriptless

<%@ tag body-content="scriptless" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%-- Capture the body evaluation result in a variable --%>
<jsp:doBody var="bodyRes" />

<%-- Convert special characters to character entities --%>
<c:set var="escapedBody" value="${fn:escapeXml(bodyRes)}" />

<%-- Replace "[code]/[/code]" with "<pre>/</pre>" --%>
<c:set var="convBody" 
  value="${fn:replace(escapedBody, '[code]', '<pre>')}" />
<c:set var="convBody" 
  value="${fn:replace(convBody, '[/code]', '</pre>')}" />

<%-- Output the result --%>
${convBody}
接下来我们看一个属性的扩展功能 - 属性片段(attribute fragment), 先解译什么是片段, 大家可以
把片段看作一段jsp代码, JSP规范中所谓的jsp fragment, 例如我们之前使用了jsp:doBody读取的主
体内容就是一个片段. 我们可以将一段巳命名的片段(named fragment)作为一个标记的属性使用, 并
而在标记中多次或一次调用这个传入的片段. 要调用这个片段, 我们使用另一个标淮动作:


<jsp:invoke/>

它是一个空标记, 有以下的属性:

1) fragment - 要调用的片段名.

2) var - 给出变量名, 把片段经过jsp容器计算过之後的结果作为字符串保存.

3) varReader - 同上, 不过将结果作为一个java.io.Reader保存.

4) scope - 作用域= =, 有var就有它, 不多说了, 默认page.
属性片段, 我们必须告诉jsp容器标记中那些标记属性的内容会是片段而不是静态文本. 我
们可以在属性元素上加上fragment="true"这个属性:

<%@ attribute name="name" fragment="true"
例:
例題中把<tr bgcolor="red"><td>${counter}: Even Row</td></tr>
作爲值傳給了even屬性。
<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
  <head>
    <title>Even and Odd Rows</title>
  </head>
  <body bgcolor="white">
    <h1>Even and Odd Rows</h1>
    <table>
      <my:forEvenAndOdd items="a,b,c,d,e">
        <jsp:attribute name="even">
          <c:set var="counter" value="${counter + 1}" />
          <tr bgcolor="red"><td>${counter}: Even Row</td></tr>
        </jsp:attribute>
        <jsp:attribute name="odd">
          <c:set var="counter" value="${counter + 1}" />
          <tr bgcolor="blue"><td>${counter}: Odd Row</td></tr>
        </jsp:attribute>
      </my:forEvenAndOdd>
    </table>
  </body>
</html>
 
Example 11-5. Using fragment attributes (forEvenAndOdd.tag)
rtexprvalue:A value of true means that the author can 
specify the value either as a static string or as a 
request-time attribute value, such as an EL expression;
false means the value must be a static string. 
The default value is true, so you only need to use this
attribute if you absolutely require a static value
我們將attribute的屬性設定為Fragment,然後想取得指定的Fragment的話,
就可以使用<jsp:invoke>動作元素,並指定Fragment的名稱
<%@ tag body-content="empty" %>
<%@ attribute name="items" rtexprvalue="true" required="true" %>
<%@ attribute name="even" fragment="true" required="true" %>
<%@ attribute name="odd" fragment="true" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${items}" varStatus="status">
  <c:choose>
    <c:when test="${status.count % 2 == 0}">
      <jsp:invoke fragment="even" />
    </c:when>
    <c:otherwise>
      <jsp:invoke fragment="odd" />
    </c:otherwise>
  </c:choose>
</c:forEach>
 <c:forEach> 剩余的属性 varStatus 所起的作用相同。和 
var 属性一样, varStatus 用于创建限定了作用域的变量。
不过,由 varStatus 属性命名的变量并不存储当前索引值或当前元素,
而是赋予 
javax.servlet.jsp.jstl.core.LoopTagStatus 
类的实例。
该类定义了一组特性,它们描述了迭代的当前状态,下面列出了这些特性:
 
 
特性 Getter 描述
currentgetCurrent() 当前这次迭代的(集合中的)项
indexgetIndex() 当前这次迭代从 0 开始的迭代索引
countgetCount() 当前这次迭代从 1 开始的迭代计数
firstisFirst() 用来表明当前这轮迭代是否为第一次迭代的标志
lastisLast() 用来表明当前这轮迭代是否为最后一次迭代的标志
begingetBegin() begin 属性值
endgetEnd() end 属性值
stepgetStep() step 属性值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值