jstl:JSP标准标签库。主要提供一些标签简化JSP代码。这些标签都有特定的功能。
jstl-1.2.jar
standard-1.1.2.jar
核心标签库:http://java.sun.com/jsp/jstl/core 推荐前缀名:c
格式化标签库:http://java.sun.com/jsp/jstl/fmt 推荐前缀名:fmt
函数标签库:http://java.sun.com/jsp/jstl/functions 推荐前缀名:fn
<%@taglib uri="标签库的url" prefix="前缀名" %>
标签库的url自己指定的,可以是任意的url路径。
如果要访问jstl标签库的标签,必须要通过“前缀名”进行访问。而前缀名可以是任意名字。
通过查看jstl.jar/META-INF/xxx.tld,tld文件就是jstl标签定义文件。每一个标签库都有一个标签定义文件。比如核心标签库有一个c.tld文件。
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core librarydescription>
<display-name>JSTL coredisplay-name>
<tlib-version>1.1tlib-version>
<short-name>cshort-name>
<uri>http://java.sun.com/jsp/jstl/coreuri>
<tag>
<description>
Catches any Throwable that occurs in its body and optionally
exposes it.
description>
<name>catchname>
<tag-class>org.apache.taglibs.standard.tag.common.core.CatchTagtag-class>
<body-content>JSPbody-content>
<attribute>
<description>
Name of the exported scoped variable for the
exception thrown from a nested action. The type of the
scoped variable is the type of the exception thrown.
description>
<name>varname>
<required>falserequired>
<rtexprvalue>falsertexprvalue>
attribute>
tag>
taglib>
body...
${pageCope.属性名}
forEach标签遍历数组或集合的时候,它会把遍历出来的每一个元素都保存在pageContext域对象的属性中。属性名就是使用var来指定的。
注意:遍历Map集合的时候,遍历出来的每一个元素是一个Entry对象。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
//String[] books = {"三国演义", "西游记", "红楼梦"};
//pageContext.setAttribute("books", books);
//List books = new ArrayList();
//books.add("西游记");
//books.add("海底总动员");
//books.add("海底三万里");
//pageContext.setAttribute("books", books);
Map books = new HashMap();
books.put("西游记", 99);
books.put("海底总动员", 199);
books.put("海底三万里", 79);
pageContext.setAttribute("books", books);
%>
Insert title here
<%-- ${pageScope.book.getKey()} --> ${pageScope.book.getValue()}
--%>
${pageScope.book.key} --> ${pageScope.book.value}
日期格式化标签:
日期模式与SimpleDateFormat对象所使用模式一样。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
Date d = new Date();
pageContext.setAttribute("currentDate", d);
%>
Insert title here
当前时间:
定义格式:
${fn:函数名(参数列表)}
函数可以在jstl文件下的/META-INF/fn.tld文件下查看。下面是该文件内容:
JSTL 1.1 functions library
JSTL functions
1.1
fn
http://java.sun.com/jsp/jstl/functions
Tests if an input string contains the specified substring.
contains
org.apache.taglibs.standard.functions.Functions
boolean contains(java.lang.String, java.lang.String)
<c:if test="${fn:contains(name, searchString)}">
Tests if an input string contains the specified substring in a case insensitive way.
containsIgnoreCase
org.apache.taglibs.standard.functions.Functions
boolean containsIgnoreCase(java.lang.String, java.lang.String)
<c:if test="${fn:containsIgnoreCase(name, searchString)}">
Tests if an input string ends with the specified suffix.
endsWith
org.apache.taglibs.standard.functions.Functions
boolean endsWith(java.lang.String, java.lang.String)
<c:if test="${fn:endsWith(filename, ".txt")}">
Escapes characters that could be interpreted as XML markup.
escapeXml
org.apache.taglibs.standard.functions.Functions
java.lang.String escapeXml(java.lang.String)
${fn:escapeXml(param:info)}
Returns the index withing a string of the first occurrence of a specified substring.
indexOf
org.apache.taglibs.standard.functions.Functions
int indexOf(java.lang.String, java.lang.String)
${fn:indexOf(name, "-")}
Joins all elements of an array into a string.
join
org.apache.taglibs.standard.functions.Functions
java.lang.String join(java.lang.String[], java.lang.String)
${fn:join(array, ";")}
Returns the number of items in a collection, or the number of characters in a string.
length
org.apache.taglibs.standard.functions.Functions
int length(java.lang.Object)
You have ${fn:length(shoppingCart.products)} in your shopping cart.
Returns a string resulting from replacing in an input string all occurrences
of a "before" string into an "after" substring.
replace
org.apache.taglibs.standard.functions.Functions
java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)
${fn:replace(text, "-", "")}
Splits a string into an array of substrings.
split
org.apache.taglibs.standard.functions.Functions
java.lang.String[] split(java.lang.String, java.lang.String)
${fn:split(customerNames, ";")}
Tests if an input string starts with the specified prefix.
startsWith
org.apache.taglibs.standard.functions.Functions
boolean startsWith(java.lang.String, java.lang.String)
<c:if test="${fn:startsWith(product.id, "100-")}">
Returns a subset of a string.
substring
org.apache.taglibs.standard.functions.Functions
java.lang.String substring(java.lang.String, int, int)
P.O. Box: ${fn:substring(zip, 6, -1)}
Returns a subset of a string following a specific substring.
substringAfter
org.apache.taglibs.standard.functions.Functions
java.lang.String substringAfter(java.lang.String, java.lang.String)
P.O. Box: ${fn:substringAfter(zip, "-")}
Returns a subset of a string before a specific substring.
substringBefore
org.apache.taglibs.standard.functions.Functions
java.lang.String substringBefore(java.lang.String, java.lang.String)
Zip (without P.O. Box): ${fn:substringBefore(zip, "-")}
Converts all of the characters of a string to lower case.
toLowerCase
org.apache.taglibs.standard.functions.Functions
java.lang.String toLowerCase(java.lang.String)
Product name: ${fn.toLowerCase(product.name)}
Converts all of the characters of a string to upper case.
toUpperCase
org.apache.taglibs.standard.functions.Functions
java.lang.String toUpperCase(java.lang.String)
Product name: ${fn.UpperCase(product.name)}
Removes white spaces from both ends of a string.
trim
org.apache.taglibs.standard.functions.Functions
java.lang.String trim(java.lang.String)
Name: ${fn.trim(name)}
下面列举了一些常用函数:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%
String name = "ChinaSoft";
pageContext.setAttribute("name", name);
%>
Insert title here
${fn:toLowerCase(name)}
${fn:toUpperCase(name)}
${fn:length(name)}
${fn:substring(name, 1, 5)}
第一步:创建一个类,继承SimpleTagSupport
package com.eight.utils;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class FormatDateTag extends SimpleTagSupport{
}
第二步:重写doTag方法。举例:创建修改时间格式标签
public class FormatDateTag extends SimpleTagSupport{
Date date;
@Override
public void doTag() throws JspException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(date != null){
String dateStr = sdf.format(date);
// 把系统时间输出到JSP页面上
this.getJspContext().getOut().write(dateStr);
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
第三步:在WEB-INF目录下创建一个 .tld文件,配置short-name和uri
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core librarydescription>
<display-name>JSTL coredisplay-name>
<tlib-version>1.1tlib-version>
<short-name>lmcshort-name>
<uri>/mytagsuri>
<tag>
<name>formatDateTagname>
<tag-class>com.eight.utils.FormatDateTagtag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>datename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
<tag>
<name>statusname>
<tag-class>com.eight.utils.StatusTagtag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>typename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>statusname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
taglib>
第四步:在jsp页面引入自定义标签库:
<%@ page language="java" import="java.util.*,com.eight.beans.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/mytags" prefix="lmc" %>
第五步:使用自定义标签
第一步:创建类
package com.eight.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PageTag2 extends SimpleTagSupport{
String url;
String arg1Name;
String arg1Value;
String arg2Name;
String arg2Value;
Integer curPage;
Integer totalPage;
Integer total;
@Override
public void doTag() throws JspException, IOException {
StringBuilder html = new StringBuilder("");
//首页
html.append("首页");
//上一页
html.append("上一页");
//下一页
html.append("下一页");
//尾页
html.append("尾页共");
//其他
html.append(total);
html.append("条纪录,当前第");
html.append(curPage);
html.append("/");
html.append(totalPage);
html.append("页,每页5条纪录 ");
this.getJspContext().getOut().write(html.toString());
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getArg1Name() {
return arg1Name;
}
public void setArg1Name(String arg1Name) {
this.arg1Name = arg1Name;
}
public String getArg1Value() {
return arg1Value;
}
public void setArg1Value(String arg1Value) {
this.arg1Value = arg1Value;
}
public String getArg2Name() {
return arg2Name;
}
public void setArg2Name(String arg2Name) {
this.arg2Name = arg2Name;
}
public String getArg2Value() {
return arg2Value;
}
public void setArg2Value(String arg2Value) {
this.arg2Value = arg2Value;
}
public Integer getCurPage() {
return curPage;
}
public void setCurPage(Integer curPage) {
this.curPage = curPage;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
第二步:在 .tld上配置
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core librarydescription>
<display-name>JSTL coredisplay-name>
<tlib-version>1.1tlib-version>
<short-name>lmcshort-name>
<uri>/mytagsuri>
<tag>
<name>statusname>
<tag-class>com.eight.utils.StatusTagtag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>typename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>statusname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
<tag>
<name>pagename>
<tag-class>com.eight.utils.PageTag2tag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>urlname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>arg1Namename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>arg1Valuename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>arg2Namename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>arg2Valuename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>curPagename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>totalPagename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
<attribute>
<name>totalname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
taglib>
第三步:在jsp页面上引用: