说完了EL,自然得说到JSTL。
JSTL是一个JSP标签集合,它封装了JSP应用的通用核心功能。
对于这个标签库的详细介绍,菜鸟教程有着详细的介绍。就不用再复制一遍了。
菜鸟教程
这篇文章只是从实际应用的角度,将JSTL从导入到使用,以及常见案例罗列出来而已。
一般来说,我们使用MyEclipse的时候,都会主动或手动导入JSTL支持,基本上不需要太复杂的操作。其他情况可能会需要Apache Tomcat安装JSTL 库,这个需要注意。另外,我以前很容易忽视的一个就是页面是否导入JSTL功能。或者链接不对。
我把常用的JSTL列了一个表格,详见附件。
1.案例
1.后台代码
package com.cms.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cms.entity.Admin;
import com.cms.entity.User;
/**
*@author:gang
*@version:
**/
@Controller
@RequestMapping("/test")
public class JstlAction{
private List<String> list;
private String [] array ={"array1","array2","array3","array4"};
private Date date;
@RequestMapping("/jstltest")
public String jstltest(HttpServletRequest request,ModelMap map){
// 普通字符串
request.setAttribute("hello", "hello world");
//HTML字符串
request.setAttribute("welcome", "<font color='red'>welcome</font>");
//条件控制标签
request.setAttribute("v1", 10);
request.setAttribute("v2", 20);
list = new LinkedList<String>();
list.add("list1_1");
list.add("list1_2");
request.setAttribute("userList", list);
//结构
Admin admin = new Admin();
admin.setAdminName("ant-black");
List<User> users = new ArrayList<User>();
for (int i=0; i<10; i++) {
User user = new User();
user.setUserName("aaa");
user.setUserPassword("123456");
user.setUid(i);
user.setAdmin(admin);
users.add(user);
}
request.setAttribute("users", users);
//map
Map<String, String> map001 = new HashMap<String, String>();
map001.put("key1", "value1");
map001.put("key2", "value2");
map001.put("key3", "value3");
map001.put("key4", "value4");
request.setAttribute("map", map001);
//array
map.addAttribute("array", array);
//对象
date = new Date();
map.addAttribute("date", date);
//forTokens
request.setAttribute("strTokens", "1#2#3#4#5");
return "jstltest";
}
}
2.前台代码
<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@page import="com.cms.entity.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>JSTL test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style>
li {border-top: 4px dotted;margin-top:30px}
</style>
</head>
<body>
<h1>测试JSTL标签库</h1>
<ul>
<li>采用 c:out 标签</li>
hello(使用标签):<c:out value="123"></c:out> <br>
hello(使用标签,结合EL表达式):<c:out value="${hello}"></c:out><br>
hello(使用EL表达式):${hello}<br>
<p>----------默认值default的使用------------</p>
hello(使用EL表达式,default):${hello123}<br>
hello(使用标签,结合EL表达式,缺省值):<c:out value="${hello123}" default="hello123"></c:out><br>
hello(使用标签,结合EL表达式,缺省值):<c:out value="${hello123}" >hello123</c:out><br>
welcome(使用EL表达式):${welcome } <br>
<p>----------------是否忽略xml格式-------------</p>
welcome(使用标签,escapeXml=true,EL表达式):<c:out value="${welcome}" escapeXml="true"></c:out> <br>
welcome(使用标签,escapeXml=false,EL表达式):<c:out value="${welcome}" escapeXml="false"></c:out> <br>
*********************************************
<li>测试c:set 和 c:remove</li>
<c:set value="000000001" var="userId"></c:set><br>
userId:--->${userId } <br>
<c:remove var="userId"/> <br>
userId:--->${userId } <br>
*********************************************
<li>条件控制标签c:if</li>
<c:if test="${v1 lt v2 }">
v1 小于v2 <br>
</c:if>
*********************************************
<li>条件控制标签:c:choose,c:when,c:otherwise</li>
<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>
<p> *********************************************</p>
<li>循环控制标签:--->c:forEach</li>
<h3>采用jsp脚本显示</h3>
<table border="1px">
<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.getUserPassword()%></td>
<td><%=user.getUid() %></td>
</tr>
<%
}
}
%>
</table>
<h3>采用c:forEach 标签</h3>
<table border="1px">
<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.uid}</td>
<td>${user.admin.adminName}</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<h3>采用c:forEach ,varstatus</h3>
<table border="1px">
<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" varStatus="us">
<c:choose>
<c:when test="${us.count mod 2==0 }">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:choose>
<td>${user.uid}</td>
<td>${user.admin.adminName}</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>
<li>循环控制标签forTokens</li>
<c:forTokens items="${strTokens} " delims="#" var="v">
${v } <br>
</c:forTokens>
<li>c:catch标签</li>
<p>catch:出现错误的解决方式</p>
<p>
<%
try{
Integer.parseInt("dasdasdasd");
} catch(Exception e){
out.println(e.getMessage());
}
%>
</p>
<P>
<c:catch var="msg">
<%
Integer.parseInt("afdsfasdf");
%>
</c:catch>
<c:if test="${msg!=null}">
${msg} <br>
${msg.message}
</c:if>
</P>
<li>c:param标签</li>
<c:url var="myURL" value="main.jsp">
<c:param name="name" value="Runoob"/>
<c:param name="url" value="www.runoob.com"/>
</c:url>
<a href="<c:out value="${myURL}"/>"><c:out value="${myURL}"/></a>
<li>c:redirect跳转标签</li>
<p>这个不能写。。会跳转\<\c:redirect url="http://www.runoob.com"/></p>
</ul>
<ul>
<li>
<div>c:import 打印网站源码,或者说导入一个网站</div>
<div>
<c:import var="data" url="http://www.runoob.com"/>
<c:out value="${data}"/>
</div>
</li>
</ul>
</body>
</html>
3.展示效果
2.总结
JSTL大概就这些东西,JSTL和EL掌握好,JSP基本上就不会有什么疑问了。
3.附件
- 核心标签
核心标签是最常用的JSTL标签。引用核心标签库的语法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 格式化标签
JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
- SQL标签
JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。引用SQL标签库的语法如下:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
- XML 标签
JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下:
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
- JSTL函数
JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>