JSTL标签简介
=====================================================================
JSTL的全称是JavaServer Pages Standard Tag Library,它是由Sun公司制定的一套标准规范并且由Apache来实现的,它主要提供给java
web开发人员一个标准的标签函数库。JSTL的提出旨在取代传统JSP页面中嵌入的繁杂的java代码以提高程序的可阅读性,可维护性从而使得jsp页面更加简洁。JSTL标签以及EL的使用作为日常开发过程中的必备技能已经成为衡量一个java web程序员是否合格的重要标准,所以练好JSTL基本功对于初入IT界的人来讲是非常重要的。
JSTL标签的配置和引入
========================================================================================
1.配置JSTL:将jstl.jar和standard.jar拷贝到WEB-INF/lib下
2.在页面中引入(采用tablib指令引入标签库):
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
上面引入的这三个标签库分别是Core标签库,函数标签库和格式化标签库,它们是在做web开发中最常用的,其他的标签库引入方式与上面方法相似。在下面的内容中将分别介绍着三个标签库中标签的用法。
Core核心库标签的用法
======================================================================
Core标签库中的标签共有13个,根据其功能的不同可以分为4大类,它们分别是:
1 表达式控制标签:out 、set、remove、catch
- <%@page import="java.util.Iterator"%>
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@ page import="java.util.*" %>
- <%@ page import="com.bjpowernode.jstl.*" %>
- <%@ 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>
- <li>采用c:out标签</li><br>
- hello(使用标签):<c:out value="${hello }"></c:out><br>
- hello(使用EL表达式):${hello }<br>
- hello(default):${hello123 }<br>
- hello(使用缺省值):<c:out value="${hello123 }" default="没有值" /><br>
- welcom(使用EL表达式):${welcome }<br>
- welcome(使用标签,escapeXml=true):<c:out value="${welcome }" escapeXml="true" /><br>
- welcome(使用标签,escapeXml=false):<c:out value="${welcome }" escapeXml="false" />
- <p>
- <li>测试:c:set,c:remove</li>
- <c:set value="root" var="userid" /><br>
- userid:${userid }
- <c:remove var="userid" /><br>
- userid:${userid }
- <p>
- <li>c:catch标签</li><br>
- <c:catch var="msg">
- <%
- Integer.parseInt("sdf");
- %>
- </c:catch>
- ${msg }<br>
- <p>
- </body>
- </html>
<%@page import="java.util.Iterator"%> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="java.util.*" %> <%@ page import="com.bjpowernode.jstl.*" %> <%@ 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> <li>采用c:out标签</li><br> hello(使用标签):<c:out value="${hello }"></c:out><br> hello(使用EL表达式):${hello }<br> hello(default):${hello123 }<br> hello(使用缺省值):<c:out value="${hello123 }" default="没有值" /><br> welcom(使用EL表达式):${welcome }<br> welcome(使用标签,escapeXml=true):<c:out value="${welcome }" escapeXml="true" /><br> welcome(使用标签,escapeXml=false):<c:out value="${welcome }" escapeXml="false" /> <p> <li>测试:c:set,c:remove</li> <c:set value="root" var="userid" /><br> userid:${userid } <c:remove var="userid" /><br> userid:${userid } <p> <li>c:catch标签</li><br> <c:catch var="msg"> <% Integer.parseInt("sdf"); %> </c:catch> ${msg }<br> <p> </body> </html>
2 流程式控制标签:if、choose、when、otherwise
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <li>条件控制标签:c:if</li><br>
- <c:if test="${v1 lt v2}">
- v1小于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<br>
- </c:otherwise>
- </c:choose>
- <c:choose>
- <c:when test="${empty userList }">
- 没有符合条件的数据<br>
- </c:when>
- <c:otherwise>
- 存在符合条件的数据<br>
- </c:otherwise>
- </c:choose>
- </body>
- </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <li>条件控制标签:c:if</li><br> <c:if test="${v1 lt v2}"> v1小于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<br> </c:otherwise> </c:choose> <c:choose> <c:when test="${empty userList }"> 没有符合条件的数据<br> </c:when> <c:otherwise> 存在符合条件的数据<br> </c:otherwise> </c:choose> </body> </html>
3 循环标签:forEach、forTokens
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>采用forEach标签,varstatus</h3>
- <table border="1">
- <tr>
- <td>用户名称</td>
- <td>年龄</td>
- <td>所属组</td>
- </tr>
- <c:choose>
- <c:when test="${empty users }">
- <tr>
- <td clospan="3">没有符合条件的数据</td>
- </tr>
- </c:when>
- <c:otherwise>
- <c:forEach items="${users }" var="user" varStatus="vs">
- <c:choose>
- <c:when test="${vs.count mod 2 == 0 }">
- <tr bgcolor="red">
- </c:when>
- <c:otherwise>
- <tr>
- </c:otherwise>
- </c:choose>
- <td>${user.username }</td>
- <td>${user.age }</td>
- <td>${user.group.name }</td>
- </tr>
- </c:forEach>
- </c:otherwise>
- </c:choose>
- </table>
- <li>演示循环控制标签:forTokens</li><br>
- <c:forTokens items= "${strTokens }" delims="#" var="v">
- ${v }<br>
- </c:forTokens>
- </body>
- </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <h3>采用forEach标签,varstatus</h3> <table border="1"> <tr> <td>用户名称</td> <td>年龄</td> <td>所属组</td> </tr> <c:choose> <c:when test="${empty users }"> <tr> <td clospan="3">没有符合条件的数据</td> </tr> </c:when> <c:otherwise> <c:forEach items="${users }" var="user" varStatus="vs"> <c:choose> <c:when test="${vs.count mod 2 == 0 }"> <tr bgcolor="red"> </c:when> <c:otherwise> <tr> </c:otherwise> </c:choose> <td>${user.username }</td> <td>${user.age }</td> <td>${user.group.name }</td> </tr> </c:forEach> </c:otherwise> </c:choose> </table> <li>演示循环控制标签:forTokens</li><br> <c:forTokens items= "${strTokens }" delims="#" var="v"> ${v }<br> </c:forTokens> </body> </html>
4 URL操作标签:import、url、redirect
- <SPAN style="FONT-SIZE: 12px; FONT-FAMILY: SimSun">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <li>c:import标签 </li>
- <c:import url="http://www.baidu.com"></c:import><br>
- <p>
- <li>c:url,c:param</li><br>
- <c:url value="http://www.baidu.com" var="u">
- <c:param name="userId" value="zhangsan"></c:param>
- <c:param name="age" value="20"></c:param>
- </c:url>
- ${u }<br>
- <li>c:redirect</li><br>
- <c:redirect url="http://hao.360.cn/"></c:redirect>
- </body>
- </html></SPAN>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<li>c:import标签 </li>
<c:import url="http://www.baidu.com"></c:import><br>
<p>
<li>c:url,c:param</li><br>
<c:url value="http://www.baidu.com" var="u">
<c:param name="userId" value="zhangsan"></c:param>
<c:param name="age" value="20"></c:param>
</c:url>
${u }<br>
<li>c:redirect</li><br>
<c:redirect url="http://hao.360.cn/"></c:redirect>
</body>
</html>
上面标签用法中对应的servlet代码如下:
- 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", "");
- //结构
- Group group = new Group();
- group.setName("动力节点603班");
- List users = new ArrayList();
- for(int i = 0;i < 10 ; i++){
- User user = new User();
- user.setUsername("张三_" + i);
- user.setAge(23);
- 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);
- }
- }
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", ""); //结构 Group group = new Group(); group.setName("动力节点603班"); List users = new ArrayList(); for(int i = 0;i < 10 ; i++){ User user = new User(); user.setUsername("张三_" + i); user.setAge(23); 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); } }