JSTL一般要配合EL表达式一起使用,通过提供大多数 Web 应用程序常用功能的标准实现,JSTL 有助于加速开发周期。
一、JSTL和El表达式的关系
EL与四个 JSTL 定制标记库提供的操作结合起来允许页面作者不使用脚本元素即可实现表示层逻辑(不需要对表示层程序编写java代码),这极大地简化了 JSP 应用程序的维护。
二、使用方法:
1、如果只是使用EL表达式不需要引入任何jar包,只要jsp/servlet容器实现了J2EE1.4/Servlet2.4、JSP2.0规范就可以
2、JSTL标签的配置和使用:
(1) 将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/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
(3)使用自定义函数:
* 定义类MyFunctions,方法必须为静态全局的
* 提供tld描述文件(仿照其他标准tld文件写即可),将tld放到WEB-INF或WEB-INF下的任意目录中
* 在jsp中采用taglib进行引入
* 在EL表达式中采用前缀+冒号+函数名的方式进行调用
三、测试示例:
项目概览
1、EL表达式
Servlet
package com.jialin.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; /** * 测试El表达式Servlet * @author jialin * */ public class ElServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Group group = new Group(); group.setGroupName("第一组"); //普通对象 User user = new User(); user.setGroup(group); user.setName("张三"); user.setAge(17); //map Map map = new HashMap(); map.put("k1", "k1value"); map.put("k2", "k2value"); //字符串数组 String[] strArr = new String[] { "a", "b", "c", "d" }; // 对象数组\ User[] arrUser = new User[10]; for (int i = 0; i < arrUser.length; i++) { arrUser[i] = new User(); arrUser[i].setName("张三" + i); } // List List listUser = new ArrayList(); for (int i = 0; i <= 10; i++) { User usr = new User(); usr.setName("战三" + i); listUser.add(usr); } //放入request request.setAttribute("arrUser", arrUser); request.setAttribute("listUser", listUser); request.setAttribute("arr", strArr); request.setAttribute("map", map); request.setAttribute("user", user); request.setAttribute("v1", ""); request.setAttribute("v2", new ArrayList()); request.setAttribute("v3", null); request.setAttribute("v4", "434"); request.setAttribute("hello", "Hello World"); //转发 request.getRequestDispatcher("/ElTest.jsp").forward(request, response); } }
jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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> <a href="servlet/ElServlet"></a>测试EL表达式 </h1> <li> 普通字符串 </li> <br /> Jsp脚本:<%=request.getAttribute("hello")%><br /> EL表达式:${hello} <br /> RequestScope:${requestScope.hello} <li> 结构 </li> 用户名:${user.name} <br /> 用户年龄:${user.age } <br /> 用户所属组:${user.group.groupName } <br /> <li> Map </li> <br /> map.k1:${map.k1} <br /> map.k2:${map.k2 } <br /> <li> 数组 </li> <br /> arr[1]:${arr[1]}; <br /> <li> 对象数组 </li> user数组:${arrUser[4].name } <br /> <li> List </li> userList:${listUser[5].name} <br /> <br /> <li> 运算符 </li> 1+1=${1+1} <br /> 1*2=${1*2} <br /> 21/7=${21/7} <br /> 21 div 7=${21 div 7} <br /> 21 mode 7=${21 mod 7} <br /> 21 % 7=${21 % 7} <br /> <li> 测试Empty </li> <br /> v1:${empty v1 } <br /> v2:${empty v2 } <br /> v3:${empty v3 } <br /> v4:${empty v4 } <br /> v5:${empty v5 } <br /> </body> </html>
结果:
2、测试jstl
servlet
package com.jialin.jstl; import java.io.IOException; import java.util.ArrayList; import java.util.Date; 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; /** * 测试JSTLServlet * @author jialin * */ public class JstlServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //字符串 request.setAttribute("str", "fdfsdfsd432"); request.setAttribute("strs", "fd#fs#dfsd#432"); //日期和数字 request.setAttribute("today", new Date()); request.setAttribute("num", new Double(1234567.123456789)); request.setAttribute("pnum", new Double(0.1233456)); //List List listUser = new ArrayList(); for (int i = 0; i <= 10; i++) { User usr = new User(); usr.setName("战三" + i); usr.setAge(10); Group group = new Group(); group.setGroupName("战三group" + i); usr.setGroup(group); listUser.add(usr); } //map Map map = new HashMap(); map.put("k1", "k1value"); map.put("k2", "k2value"); map.put("k3", "k3value"); map.put("k4", "k4value"); map.put("k5", "k5value"); map.put("k6", "k6value"); //加入request request.setAttribute("map", map); request.setAttribute("listUser", listUser); request.setAttribute("strTokens", "1@2@3@4@5@6@7"); request.setAttribute("v1", "10"); request.setAttribute("v2", "11"); request.setAttribute("hello", "hello world"); request.setAttribute("list", new ArrayList()); request.setAttribute("hello1","<font color=\"red\">hello world</font>"); //转发 request.getRequestDispatcher("/JstlTest.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"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib uri="http://blog.csdn.net/shan9liang/myfunctions" prefix="my"%> <!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> <h4> 测试 JSTL </h4> <ui> <li> 测试c:out标签 </li> hello(使用标签): <c:out value="123" /> <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="${hello2312}" default="hi" /> <br /> hello(使用标签): <c:out value="${hello2312}" /> hi <br /> hello world(使用EL表达式):${hello1} <br /> hello world(使用escapeXML): <c:out value="${hello1}" escapeXml="true" /> hi <br /> hello world(使用escapeXML): <c:out value="${hello1}" escapeXml="false" /> hi <br /> <br /> <li> 测试c:set 标签 </li> <c:set var="hi" value="hello world!" /> <br /> hi:${hi} <br /> <c:set var="hoho" /> hi:${hoho} <br /> <br /> <li> 测试 c:if 标签 </li> <br /> <c:if test="${v1 < v2}"> “v1 < v2” </c:if> <br /> <br /> <li> 测试 c:choose c:when c:otherwise </li> <br /> <c:choose> <c:when test="$(v1>v2)"> "v1>v2" </c:when> <c:otherwise> "v1 < v2" </c:otherwise> </c:choose> <br /> <c:choose> <c:when test="${empty list}"> 无数据 </c:when> <c:otherwise> 有数据 </c:otherwise> </c:choose> <br /> <li> 测试 c:forEach </li> <table> <tr> <td> 姓名 </td> <td> 年龄 </td> <td> 组别 </td> </tr> <c:choose> <c:when test="${empty listUser}"> <tr> <td colspan="3"> 没有数据 </td> </tr> </c:when> <c:otherwise> <c:forEach items="${listUser}" var="user"> <tr> <td> ${user.name} </td> <td> ${user.age } </td> <td> ${user.group.groupName} </td> </tr> </c:forEach> </c:otherwise> </c:choose> </table> <br /> <li> 测试c:forEach varStatus </li> <table> <tr> <td> 姓名 </td> <td> 年龄 </td> <td> 组别 </td> </tr> <c:choose> <c:when test="${empty listUser}"> <tr> <td colspan="3"> 没有数据 </td> </tr> </c:when> <c:otherwise> <c:forEach items="${listUser}" var="user" varStatus="vs"> <c:choose> <c:when test="${vs.count % 2==0}"> <tr bgcolor="green"> <td> ${user.name} </td> <td> ${user.age } </td> <td> ${user.group.groupName} </td> </tr> </c:when> <c:otherwise> <tr> <td> ${user.name} </td> <td> ${user.age } </td> <td> ${user.group.groupName} </td> </tr> </c:otherwise> </c:choose> </c:forEach> </c:otherwise> </c:choose> </table> <br /> <li> 测试forEach begin end </li> <table> <tr> <td> 姓名 </td> <td> 年龄 </td> <td> 组别 </td> </tr> <c:choose> <c:when test="${empty listUser}"> <tr> <td colspan="3"> 没有数据 </td> </tr> </c:when> <c:otherwise> <c:forEach items="${listUser}" var="user" begin="2" end="7" step="1"> <tr> <td> ${user.name} </td> <td> ${user.age } </td> <td> ${user.group.groupName} </td> </tr> </c:forEach> </c:otherwise> </c:choose> </table> <li> 测试forEach 取出map中的值 </li> <c:forEach items="${map}" var="entry"> ${entry.key},${entry.value}<br /> </c:forEach> <br /> <li> 测试forTokens </li> <c:forTokens items="${strTokens}" delims="@" var="t"> ${t}<br /> </c:forTokens> <li> 测试c:catch </li> <c:catch var="msg"> <% Integer.parseInt("rewrw"); %> </c:catch> ${msg} <br /> <li> 测试 c:import </li> <c:import url="http://www.baidu.com"></c:import> <li> 测试 c:url param </li> <c:url value="http://www.baidu.com" var="uri"> <c:param name="id1" value="321"> </c:param> <c:param name="id2" value="876"> </c:param> </c:url> <br /> ${uri} <br /> <li> 测试 fn </li> <br /> str.length(函数必须放在EL表达式中,用法:前缀+冒号+函数名称):${fn:length(str)} <br /> <c:forEach items="${fn:split(strs,\"#\")}" var="v1"> ${v1 }<br /> </c:forEach> <br /> ${my:Hello("张三")} <li> 测试 fmt </li> <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(dateStyle="short") <fmt:formatDate value="${today}" dateStyle="short" /> <br /> today(dateStyle="medium") <fmt:formatDate value="${today}" dateStyle="medium" /> <br /> today(dateStyle="long") <fmt:formatDate value="${today}" dateStyle="long" /> <br /> today(dateStyle="full") <fmt:formatDate value="${today}" dateStyle="full" /> <br /> today(pattern="yyyy/MM/dd hh:mm:ss") <fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" /> <br /> <fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="nowDate" /> <br /> ${nowDate } <br /> <li> 测试格式化数字 </li> <br /> number(default) <fmt:formatNumber value="${num}" /> <br /> number(pattern="###,###,###.####") <fmt:formatNumber value="${num}" pattern="###,###,###.####" /> <br /> number(pattern="###,###,###.000") <fmt:formatNumber value="${num}" pattern="###,###,###.000" /> <br /> number(groupingUsed="false") <fmt:formatNumber value="${num}" groupingUsed="false" /> <br /> number(minIntegerDigits="10" maxIntegerDigits="10") <fmt:formatNumber value="${num}" minIntegerDigits="10" maxIntegerDigits="10" /> <br /> number(minFractionDigits="4" maxFractionDigits="5") <fmt:formatNumber value="${num}" minFractionDigits="4" maxFractionDigits="5" /> <br /> number(type="currency") <fmt:formatNumber value="${num}" type="currency" /> <br /> number(type="currency" currencySymbol="$") <fmt:formatNumber value="${num}" type="currency" currencySymbol="$" /> <br /> number(type="percent" maxFractionDigits="5" minFractionDigits="3") <fmt:formatNumber value="${pnum}" type="percent" maxFractionDigits="5" minFractionDigits="3" /> <br /> <br /> </body> </html>
结果:
姓名 | 年龄 | 组别 |
战三0 | 10 | 战三group0 |
战三1 | 10 | 战三group1 |
战三2 | 10 | 战三group2 |
战三3 | 10 | 战三group3 |
战三4 | 10 | 战三group4 |
战三5 | 10 | 战三group5 |
战三6 | 10 | 战三group6 |
战三7 | 10 | 战三group7 |
战三8 | 10 | 战三group8 |
战三9 | 10 | 战三group9 |
战三10 | 10 | 战三group10 |
姓名 | 年龄 | 组别 |
战三0 | 10 | 战三group0 |
战三1 | 10 | 战三group1 |
战三2 | 10 | 战三group2 |
战三3 | 10 | 战三group3 |
战三4 | 10 | 战三group4 |
战三5 | 10 | 战三group5 |
战三6 | 10 | 战三group6 |
战三7 | 10 | 战三group7 |
战三8 | 10 | 战三group8 |
战三9 | 10 | 战三group9 |
战三10 | 10 | 战三group10 |
姓名 | 年龄 | 组别 |
战三2 | 10 | 战三group2 |
战三3 | 10 | 战三group3 |
战三4 | 10 | 战三group4 |
战三5 | 10 | 战三group5 |
战三6 | 10 | 战三group6 |
战三7 | 10 | 战三group7 |