1. web.xml区别
- JSTL 1.0使用
<?xml version="1.0" encoding="UTF-8"?>
<!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 id="WebApp_1432624743202">
- JSTL 1.1使用
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_1432624743202" version="2.4"
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" >
2. JSTL 标签库安装
3. 页面引用
- JSTL 1.0使用 注意:Oracle Java Technologies | Oracle
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="std" uri="/base/std/tld/std.tld" %>
- JSTL 1.1使用 注意:Oracle Java Technologies | Oracle
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="std" uri="/base/std/tld/std.tld" %>
4. fn函数使用
fn 函数的使用方法在JSTL 1.1才开始出现,JSTL 1.0如果需要使用fn方法需要自定义。如:${fn:length(java.lang.Object)} ,示例代码:
public static int length(Object obj) throws JspTagException {
if (obj == null) return 0;
if (obj instanceof String) return ((String)obj).length();
if (obj instanceof Collection) return ((Collection)obj).size();
if (obj instanceof Map) return ((Map)obj).size();
int count = 0;
if (obj instanceof Iterator) {
Iterator iter = (Iterator)obj;
count = 0;
while (iter.hasNext()) {
count++;
iter.next();
}
return count;
}
if (obj instanceof Enumeration) {
Enumeration enum_ = (Enumeration)obj;
count = 0;
while (enum_.hasMoreElements()) {
count++;
enum_.nextElement();
}
return count;
}
try {
count = Array.getLength(obj);
return count;
} catch (IllegalArgumentException ex) {}
throw new JspTagException(Resources.getMessage("FOREACH_BAD_ITEMS"));
}