JSP页面进行数据显示时,可以使用JSTL快速方便的进行显示。
如果web-app的version="2.5",则无需在web.xml中通过taglib标签进行配置了。
<jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
</jsp-config>
以上是不必要的。我们只需要引入
jstl-1.2.jar到lib目录下,即可
注意:旧版本中使用JSTL需要引入jstl.jar和standard.jar,而jstl-1.2.jar版本已经把standard.jar的内容合并到一块了,其中就包括若干tld文件。
web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>jstl</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
下面是一些使用例子:
<%@page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>首页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
List<String> list=new ArrayList<String>();
list.add("aa");
list.add("bbb");
list.add("dddd");
request.setAttribute("list", list);
request.setAttribute("now", new Date());
%>
<c:forEach items="${list}" var="entry">
<td>${entry}</td>-------<td>${fn:length(entry)}</td><br/>
</c:forEach>
<fmt:formatDate value="${now}" type="both" pattern="yyyy-MM-dd HH:mm:ss" /><br/>
<fmt:formatDate value="<%=new Date() %>" type="both" pattern="yyyy-MM-dd HH:mm:ss" />
</body>
</html>
效果如下: