Java for Web学习笔记(二六):JSTL(2)Core Tag(上)

可以在JSP中替代Java代码的几乎所有功能,包括条件编程,循环,迭代和内容输出。taglib的directive如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:out>

Attributes

Name

Required

Request-time

Type

Description

value

true

true

java.lang.String

Expression to be evaluated.

default

false

true

java.lang.String

Default value if the resulting value is null.

escapeXml

false

true

java.lang.String

Determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.

上表是从文档http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/中摘出,对接口有详细的描述。c:out中的value通常是EL表达式,注意到escapeXML的缺省值为true,也就是<c:out value="${someVariable}"/>相当于${fn:escapeXml(someVariable)}。

value和default值经常采用EL方式表达。对于default值,另一种书写方式如下。

<c:out value="${someVariable}">default value</c:out>

我们也可以采用较为复杂的HTML tag,JavaScript,以及jsp的语句进行描述。如果我们不给出value值(null),那么显示的就是这里面的default value。

一般来讲value给的是String,如果给的是java.io.Reader,将从Reader中读取并给出。

<c:url>

Attributes

Name

Required

Request-time

Type

Description

var

false

false

java.lang.String

Name of the exported scoped variable for the processed url. The type of the scoped variable is String.

scope

false

false

java.lang.String

Scope for var.

value

false

true

java.lang.String

URL to be processed.

context

false

true

java.lang.String

Name of the context when specifying a relative URL resource that belongs to a foreign context.

下面是几个例子:

Sample 1:静态链接

<c:url value="http://www.example.net/content/news/today.html" />

Sample 2: Sample1是个静态的链接,没有实际意义,但是对于动态链接,确是非常好用。

<c:url value="http://www.example.net/content/news/today.jsp">
    <c:param name="story" value="${storyId}" />
    <c:param name="seo" value="${seoString}" />
</c:url>

Sample 3:对于参数story,在生成链接的时候自动进行转换,转换为story=STORY+BOOK,这就相当方便了。

<a href="<c:url value="/test.jsp">
    <c:param name="story"value="STORY BOOK"/>
    <c:param name="content"value="${content}"/>
</c:url>">Hello,world</a>
       Sample 4:一般而言,web app在本app中流转,但是有时链接要指向执行其他的web app(即其他的上下文),可以通过context参数。
<a href="<c:url value="/item.jsp?itemId=15" context="/store"/>">Hello,world</a>

链接为:http://localhost:8080/store/item.jsp?itemId=15

Sample 5:参数var

<c:url value="/test.jsp?action=home" var="homepageUrl" />
<a href="${homepageUrl}">Home</a>

如果一个link多次使用,不需要每次进行构造,可以存放在var中,缺省是pagescope,即作为Attribute存放在pageContext中,如果要存放在request,session或者appliction,通过scope参数,例如

<c:url value="/index.jsp" var="homepageUrl" scope="request"/>

有一点需要提醒,在测试中,如果使用var的c:url要封装在<a href=“”>中,否则会失效,避免这样做。

<c:if>

Attributes

Name

Required

Request-time

Type

Description

test

true

true

boolean

The test condition that determines whether or not the body content should be processed.

var

false

false

java.lang.String

Name of the exported scoped variable for the resulting value of the test condition. The type of the scoped variable is Boolean.

scope

false

false

java.lang.String

Scope for var.

这是条件控制,例子如下:

<c:if test="${something == somethingElse}">
    execute only if test is true
</c:if>

var和scope参数和<c:url>中一样,可以存放test的值,用于以后的处理。最常用的情况是很多地方都要进行相同的判断,可以先将判断的值存在var中,例子如下。

<c:if test="${someComplexExpressionIsTrue}" var="itWasTrue" />
...

<c:if test="${itWasTrue}">
    do something
</c:if>
...

<c:if test="${itWasTrue}">
    do something else
</c:if>


相关链接: 我的Professional Java for Web Applications相关文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值