您的位置:首页 > 运维架构 > Tomcat

Tomcat中的JSTL配置与使用

2015-10-02 16:04 591 查看
一、在Tomcat中安装JSTL

前提 OS: WINDOWS;tomcat已经配置好。

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文件:【**我配置的时候好像不用加这个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>

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:param>   

<c:redirect>   

<c:remove>   删除某个变量或属性

<c:url>   

<c:when>   

<c:set>   1.   用于在某个作用范围(Request、Session、Application等)中设置某个值

2.   设置某个对象的属性

<c:catch>   捕获嵌在它里面的标签抛出异常

(1)<c:catch>标签

作用: 捕获嵌在它里面的标签抛出异常

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

<%@page contentType=”text/html;charset=gb2312” %>

<html>

<head><title>c:catch标签演示</title></head>

<body>

<c:catch var=”myException” >   //设置异常句柄,好比JAVA的 Exception e一样

<%

  String str=”abc”;

  int   i =Integer.parseInt(str);   //此处转换产生异常,因为abc字符串不能转换成数值

%>

</c:catch>

异常:<c:out value=”${myException }”/>

</body>

</html>

(2)<c:set>标签 

作用: 1.用于在某个作用范围(Request、Session、Application等)中设置某个值

2.设置某个对象的属性

<%@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”%>//引入标签库,设置前缀

<%@page contentType=”text/html;charset=gb2312”%>

<html>

<head><title>演示c:set标签</title></head>

<body>

<c:set var=”objInRequest” value=”abcStr” scope=”request” />   

//相当于request.setAttribute(“objInRequest”,”abcStr”)

<c:out value=”${objInRequest}”/>

//在request中获取objInRequest属性,将其值输出到JspWriter

</body>

</html>

User.java //这是一个javabean对象的定义

package dev; 

public class User{

public int id;

public String username;

public String password;

//public TYPE getter….

//public void setter…. 



//JSP页面

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

<%@page contentType=”text/html;charset=gb2312”%>

<jsp:useBean id=”userHandle” class=”dev.User”/>

<html>

<head><title>演示c:set标签</title></head>

<body>

<c:set target=”${userHandle}” property=”id” value=”123”/>

<c:set target=”${userHandle}” property=”username” value=”lindeqiang”/>

<c:set target=”${userHandle}” property=”password”>

This is my password //注意,这里的password的value的值也可以赋值在<c:set的标签体中

//写成<c:set target=”${userhandle}” property=”password” value=”123456”/>也可以

</c:set>

ID:<c:out value=”${ userHandle .id}”/><br>

User:<c:out value=”${userHandle.username}”/><br>

PWD:<c:out value=”${userHandle.password}”/><br>

</body>

</html>

3.<c:out>标签

作用: 把对象的数值输出到JspWriter

属性:

属性说明

属性   类型   描述

value   Object   计算的表达式

escapeXml   Boolean   是否将转换成字符实体代码,默认为true

default   Object   D当value的对象不存在时(null),就输出这个default的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jstl 配置