从jsp 1.1规范开始,jsp就支持在jsp中使用自定义标签了,自定义标签的广泛使用造成了程序员重复定义,这样就促成了jstl(javaserver pages standard tag library)的诞生。 因为工作中需要用到jstl,但网上却苦于找不到有关jstl的中文资料,所以就有了这篇文章。
jstl简介
jstl是一个不断完善的开放源代码的jsp标签库,是由apache的jakarta小组来维护的。jstl只能运行在支持jsp1.2和servlet2.3规范的容器上,如tomcat 4.x。但是在即将推出的jsp 2.0中是作为标准支持的。 jstl目前的最新版本为1.02,最终发布版为1.0。jstl包含两个部分:标签库和el(expression language表达式语言)语言。标签库目前支持四种标签: 标签 uri 前缀 示例 core http://java.sun.com/jstl/core c <c:tagname ...> xml processing http://java.sun.com/jstl/xml x <x:tagname ...> i18n capable formatting http://java.sun.com/jstl/fmt fmt <fmt:tagname ...> database access (sql) http://java.sun.com/jstl/sql sql <sql:tagname ...>
core支持jsp中的一些基本的操作; xml processing支持xml文档的处理; i18n capable formatting支持对jsp页面的国际化; database access (sql)支持jsp对数据库的操作。
例子: <c:if test="${user.wealthy}"> user.wealthy is true. </c:if>
如果user.wealthy值true,则显示user.wealthy is true.
<c:choose> <c:when test="${user.generous}"> user.generous is true. </c:when> <c:when test="${user.stingy}"> user.stingy is true. </c:when> <c:otherwise> user.generous and user.stingy are false. </c:otherwise> </c:choose>
只有当条件user.generous返回值是true时,才显示user.generous is true. 只有当条件user.stingy返回值是true时,才显示user.stingy is true. 其它所有的情况(即user.generous和user.stingy的值都不为true)全部显示user.generous and user.stingy are false.