jstl标签


1.准备jstl
   到http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/下载jakarta-taglibs-standard-1.1.2.zip
解压后成为jakarta-taglibs-standard-1.1.2 
2.准备web开发目录
   比如我的web目录系统默认目录%tomcat_home%\webapps\ROOT下,工作目录为ROOT,也可以自定义工作目录,但必须要保证工作目录下建立WEB-INF\lib,WEB-INF\classes 
3.拷贝.jar文件
   将jakarta-taglibs-standard-1.1.2\lib\下的两个jar文件:standard.jar和jstl.jar文件拷贝到工作目录的\WEB-INF\lib\下 
4.拷贝.tld文件(可选)
 将jakarta-taglibs-standard-1.1.2\tld\下的15个tld类型文件拷到"Working folder\WEB-INF\tld"下 
5.在工作目录下的\WEB-INF\下建立web.xml文件:(可选)
  <?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt-1_0</taglib-uri>
    <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt-1_0-rt</taglib-uri>
    <taglib-location>/WEB-INF/tld/fmt-1_0-rt.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/core-1_0-rt</taglib-uri>
    <taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/core-1_0</taglib-uri>
    <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/sql-1_0</taglib-uri>
    <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/sql-1_0-rt</taglib-uri>
    <taglib-location>/WEB-INF/tld/sql-1_0-rt.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/tld/x.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/x-1_0</taglib-uri>
    <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/x-1_0-rt</taglib-uri>
    <taglib-location>/WEB-INF/tld/x-1_0-rt.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/fn</taglib-uri>
    <taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/permittedTaglibs</taglib-uri>
    <taglib-location>/WEB-INF/tld/permittedTaglibs.tld</taglib-location>
</taglib> 
<taglib>
    <taglib-uri>http://java.sun.com/jstl/scriptfree</taglib-uri>
    <taglib-location>/WEB-INF/tld/scriptfree.tld</taglib-location>
</taglib>
</web-app> 
  
提示: 
In addition to declaring the tag libraries, tutorial examples access the JSTL API and implementation. In the Application Server, the JSTL TLDs and libraries are distributed in the archive <J2EE_HOME>/lib/appserv-jstl.jar. This library is automatically loaded into the classpath of all web applications running on the Application Server, so you don't need to add it to your web application. 

  
以上摘自j2ee的tutorial,说明步骤四、五中的TLD文件注册部分是可以省略的,WEB工程能自动的从jar包中加载相应的标签库。 

6.建立一个名为test.jsp文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>测试你的第一个使用到JSTL 的网页</title>
</head>
<body>
<c:out value="欢迎测试你的第一个使用到JSTL 的网页"/>
</br>你使用的浏览器是:</br>
<c:out value="${header['User-Agent']}"/>
<c:set var="a" value="David O'Davies" />
<c:out value="David O'Davies" escapeXml="true"/>
</body>
</html> 
7.显示结果:
欢迎测试你的第一个使用到JSTL 的网页
你使用的浏览器是:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SailBrowser; .NET CLR 1.1.4322) David O'Davies
8.注意的问题
主要的一个问题还是路径的问题,笔者配置了好几次都不能成功,就是把工作目录给弄错了,把项目的目录误认为是工作目录,一直在项目的目录下配置文件,其实应该在工作目录下配置,当然可以把工作目录改成项目的目录,问题也就可以解决了。
9.常出现的问题和提示:
org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
出现这个问题主要一种可能是工作目录的\WEB-INF\lib\下缺少standard.jar和jstl.jar文件,另外一种可能是web.xml没有进行TAG的正确配置。
According to TLD or attribute directive in tag file, attribute value does not accept any expressions
应用部署运行的时候出现JSP异常, 发生在使用JSTL库的时候: According to TLD or attribute directive in tag file, attribute value does not accept any expressions, 可能是因为使用了JSP2.0版本, 同时又没有使用JSTL core库的备用版本(RT库), 以下有两种处理方法: 
a. 修改web.xml. 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
改为2.3版本的 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
b. 使用JSTL core RT库 
JSTL core库的有两种taglib伪指令, 其中RT库即是依赖于JSP传统的请求时属性值, 而不是依赖于EL来实现(称为EL库.JSP2.0将支持EL) 
JSP中使用<%@ taglib uri=http://java.sun.com/jstl/core prefix="c"%>在2.3版本都可以,在2.4就不行了, 难道是版本不兼容吗? 
只要将 
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
改为 
<%@ taglib uri=http://java.sun.com/jstl/core_rt prefix="c"%>
就没有问题了 
三、标签的使用 
C标准标签库
Taglib-http://java.sun.com/jstl/core
基础:
1.jsp页面引入C标签库:
<@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”>   //引入标签库 前缀为c
2.c标签库的标签列表
    
C标签库例举
标签名   用处
<c:choose>   
<c:forEach>   
<c:forTokens>   
<c:if>   
<c:import>   
<c:otherwise>   
<c:out>   把对象的数值输出到JspWriter
<c:p

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值