JSTL标签,是一个规范,由sun来定义的,一个不断完善的开放源代码的JSP标签库.JSTL支持通用的,结构化的任务,比如迭代,条件判断,XML文档操作,SQL标签,除了这些还提供了一个框架来使用集成JSTL的自定义标签
JSTL 标签根据其功能可以分为:
核心标签库(Core Tags)
格式化标签(Formatting Tags)
SQL标签(SQL Tags)
XML标签(XML Tags)
JSTL函数(JSTL Function)
在项目中如何使用JSTL标签:
1、引用相关的jar 包:jstl和standard
将jstl..jar和standard.jar拷贝到WEB_INF/Lib下
2、在JSP中添加伪指令,采用taglib指令引入标签库
<%@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" %>
核心标签库:
用于在JSP中显示数据,就像<%= ... > | |
用于保存数据 | |
用于删除数据 | |
用来处理产生错误的异常状况,并且将错误信息储存起来 | |
与我们在一般程序中用的if一样 | |
本身只当做<c:when>和<c:otherwise>的父标签 | |
<c:choose>的子标签,用来判断条件是否成立 | |
<c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行 | |
检索一个绝对或相对 URL,然后将其内容暴露给页面 | |
基础迭代标签,接受多种集合类型 | |
根据指定的分隔符来分隔内容并迭代输出 | |
用来给包含或重定向的页面传递参数 | |
重定向至一个新的URL. | |
使用可选的查询参数来创造一个URL |
核心库实例:
新建servlet:
package com.lf.jstl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JstlCoreServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//普通字符串
request.setAttribute("hello", "Hello World");
//HTML字符串
request.setAttribute("welcome","<font color='red'>欢迎你来到这个世界</font>" );
//条件控制
request.setAttribute("v1", 10);
request.setAttribute("v2", 20);
request.setAttribute("userList", new ArrayList());
//结构
List users = new ArrayList();
Group group = new Group();
group.setName("动力节点603班");
for(int i=0;i<10;i++){
User user = new User();
user.setUsername("张三_"+i);
user.setAge(23+i);
user.setGroup(group);
users.add(user);
}
request.setAttribute("users", users);
//map
Map map = new HashMap();
map.put("k1", "v1");
map.put("k2", "v2");
map.put("k3", "v3");
map.put("k4", "v4");
request.setAttribute("map",map);
//forTokens
request.setAttribute("strTokens", "1#2#3#4#5");
request.getRequestDispatcher("/jstl_core.jsp").forward(request, response);
}
}
JSP:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="com.lf.jstl.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>测试EL表达式</h1>
<li>采用c:out标签</li><br>
hello(使用标签):<c:out value="222"/><br>
hello(使用标签):<c:out value="hello"/><br>
hello(使用标签):<c:out value="${hello }"/><br>
hello(使用EL表达式标签):${hello }<br>
hello(default):${hello123 }<br>
hello(使用缺省值):<c:out value="${hello123 }" default="没有值"/><br>
hello(使用缺省值):<c:out value="${hello123 }"/>没有值<br>
welcome:${welcome }<br>
welcome(使用标签,escapexm):<c:out value="${welcome }" escapeXml="true" /><br>
welcome(使用标签,escapexm):<c:out value="${welcome }" escapeXml="false" /><br>
<p>
<li>测试c:set,c:remove</li>
<c:set value="root" var="userid" /><br>
set userid:${userid }<br>
<c:remove var="userid" />
remove userid:${userid }<br>
<p>
<li>条件控制标签c:if</li>
<c:if test="${v1 lt v2 }"><br>
</c:if>
<p>
<li>条件控制标签 c:choose,c:when,c:otherwise</li><br>
<c:choose>
<c:when test="${v1 gt v2 }">
v1大于v2<br>
</c:when>
<c:otherwise>
v1小于v2
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${empty userList }">
没有符合条件的数据
</c:when>
<c:otherwise>
存在用户数据<br>
</c:otherwise>
</c:choose>
<p>
<li>演示循环控制标签:forEach</li>
比较<br>
<h3>采用jsp脚本显示</h3>
<table border="1">
<tr>
<td>用户名称</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<%
List userList = (List)request.getAttribute("users");
if(userList == null || userList.size()==0){
%>
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
<%
}else{
for(Iterator iter=userList.iterator();iter.hasNext();){
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getAge() %></td>
<td><%=user.getGroup().getName() %></td>
</tr>
<%
}
}
%>
</table>
<h3>采用forEach标签</h3>
<table border="1">
<tr>
<td>用户名称</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users }">
<tr>
<td colspan-"3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users }" var="user">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<h3>采用forEach标签,begin.end</h3>
<table border="1">
<tr>
<td>用户名称</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users }">
<tr>
<td colspan-"3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users }" var="user" begin="2" end="8" step="2">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<p>
<li>循环控制标签:forEach,输出map</li><br>
<c:forEach items="${map }" var="entry">
${entry.key},${entry.value }<br>
</c:forEach>
<p>
<li>循环控制标签</li><br>
<c:forTokens items="${strTokens }" delims="#" var="v">
${v } <br>
</c:forTokens>
</body>
</html>
格式化库:
package com.lf.jstl;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 测试JSTL格式化库
* @author zym
*
*/
public class JstlFmtServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("today", new Date());
request.setAttribute("n", 12345678.123);
request.setAttribute("p", 0.1234567);
request.getRequestDispatcher("/jstl_fmt.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h3>测试格式化日期标签</h3><br>
today:(default):<fmt:formatDate value="${today }"/><br>
today:(type="date"):<fmt:formatDate value="${today }" type="date"/><br>
today:(type="time"):<fmt:formatDate value="${today }" type="time"/><br>
today:(type="both"):<fmt:formatDate value="${today }" type="both"/><br>
today:(type="short"):<fmt:formatDate value="${today }" dateStyle="short"/><br>
today:(type="medium"):<fmt:formatDate value="${today }" dateStyle="medium"/><br>
today:(type="long"):<fmt:formatDate value="${today }" dateStyle="long"/><br>
today:(type="full"):<fmt:formatDate value="${today }" dateStyle="full"/><br>
today:(type="yyyy-MM-dd HH:mm:ss"):<fmt:formatDate value="${today }" pattern="yyyy-MM-dd HH:mm:ss"/><br>
当前时间:${currentDate }<br>
<h3>测试格式化日期标签</h3><br>
n{defalut}:<fmt:formatNumber value="${n }"/><br>
n{pattern="###,###,###.####"}:<fmt:formatNumber value="${n }" pattern="###,###,###.####"/><br>
n{pattern="###,###,###.0000"}:<fmt:formatNumber value="${n }" pattern="###,###,###.0000"/><br>
n{groupingUsed="false"}:<fmt:formatNumber value="${n }" groupingUsed="false"/><br>
n{maxIntegerDigits="12" minIntegerDigits="10"}:<fmt:formatNumber value="${n }" maxIntegerDigits="12" minIntegerDigits="10"/><br>
n{maxFractionDigits="4" minFractionDigits="6"}:<fmt:formatNumber value="${n }" maxFractionDigits="4" minFractionDigits="6"/><br>
<h3>测试货币标签</h3>
n{defalut}:<fmt:formatNumber value="${n }" type="currency"/><br>
n{defalut}:<fmt:formatNumber value="${n }" type="currency" currencySymbol="$"/><br>
<h3>测试百分比</h3>
p{defalut}:<fmt:formatNumber value="${p }" type="percent" minFractionDigits="2" maxFractionDigits="2"/>
</body>
</html>
function函数库:
package com.lf.jstl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 测试JSTL函数库
* @author zym
*
*/
public class JstlFunctionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("hello", "HelloWorld");
request.getRequestDispatcher("/jstl_fn.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>测试JSTL函数库</h1>
hello.length(jsp脚本):<%=((String)request.getAttribute("hello")).length() %><br>
hello.length(JSTL的length函数,必须放到EL表达式中):<br>
用法:前缀+冒号+函数名:${fn:length(hello) }<br>
</body>
</html>
初步学习JSTL,用法其实很简单,常用的也就那么几个,了解了,其实很好实现。总结一下,以后不会也会查,使用JSTL标签库简化我们的JSP代码。