对于MVC 设计模式来讲,一直强调,在一个JSP 页面之中Scriptlet 的代码越少越好,但是只靠最终的概念是很难实现的的,因为标签的开发本身是非常麻烦的,所以为了简化标签的开发,也为了标签更具备一些通用性,所以一般在开发中(不使用框架的前提下) 可以使用JSTL 组件完成
现在开发环境是JDK 1.6 、tomcat 6.0, 但是对于JSTL 它的稳定版本是1.1 ,而现在的JSTL 1.2 暂时还无法直接下载,此时所用的开发包,并不是直接通过Apache 上下载而来的,而是从Myeclipse 中拷贝来的。
标签的开发中本身需要*.tld 文件和所有的标签的支持类,那么这些*.tld 文件可以直接从jar 包中拷贝出来,存放到web-info 的文件夹之中
将所有的jar包拷贝到Tomcat/lib 文件夹之中
下面先完成一个简单的功能,认识一下这些表情的操作
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<h3><c:out value="hello world"/></h3>
</body>
</html>
此标签就是完成了一个熟悉 = out.print() 操作
这些标签已经不再需要用户自己编写了,而可以直接通过标签库完成,当然,如果现在需要的话,也可以直接在web.xml 文件之中配置这些的映射路径
<jsp-config>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/fn</taglib-uri>
<taglib-location>/WEB-INF/fn.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
</jsp-config>
以后程序页面之中,将直接通过映射路径完成
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<h3><c:out value="hello world"/></h3>
</body>
</html>
掌握核心标签库提供的主要标签
可以完成输出、判断、迭代等常见操作
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:set var="info" value="good" scope="request"/>
<h3>输入内容: ${info}</h3>
</body>
</html>
本操作只定义了一个普通的字符串熟悉而已
通过<c:set> 标签还可以进行已有JavaBean 的复制操作
package com.demo;
public class SimpleInfo {
private String content;
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
}
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.demo.SimpleInfo"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<%
SimpleInfo sim = new SimpleInfo();
request.setAttribute("simple",sim);
%>
<c:set value="good good" target="${simple}"
property="content"/>
<h3>输入内容: ${simple.content}</h3>
</body>
</html>
<taglib>
<taglib-uri>http://www.youkuaiyun.com/jst/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:set var="info" value="good" scope="request"/>
<c:remove var="info" scope="request"/>
<h3>输入内容: ${info}</h3>
</body>
</html>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:catch var="errmsg"/>
<%
int result = 10 / 0;
%>
<h3>异常信息: ${errmsg}</h3>
</body>
</html>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:if test="${param.ref == 'gz'}" var="hell" scope="page">
<h3>欢迎: ${param.ref}光临</h3>
</c:if>
<c:if test="${10<20}" var="hell2">
<h3>10比20小</h3>
</c:if>
</body>
</html>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<%
pageContext.setAttribute("num",20);
%>
<c:choose>
<c:when test="${num == 10}">
<h3>num 属性的内容是10</h3>
</c:when>
<c:when test="${num == 20}">
<h3>num 属性的内容是10</h3>
</c:when>
<c:otherwise>
<h3>没有满足的条件</h3>
</c:otherwise>
</c:choose>
</body>
</html>

在整个JSTL 之中最重要的两个标签: <c:if>、<c:forEach>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<%
String info[] = {"marry","liqiang","zhongtian"};
pageContext.setAttribute("ref",info);
%>
<h3>输出全部:
<c:forEach items="${ref}" var="for">
${for}、
</c:forEach></h3>
<h3>输出全部(间隔为2):
<c:forEach items="${ref}" var="for" step="2">
${for}、
</c:forEach></h3>
<h3>输出前两个:
<c:forEach items="${ref}" var="for" begin="0" end="1">
${for}、
</c:forEach></h3>
</body>
</html>
下面继续再完成个List集合的输出
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<% List all = new ArraList();
all.add("你好");
all.add("不好");
pageContext.setAttribute("ref",all);
%>
<h3>输出全部:
<c:forEach items="${ref}" var="for">
${for}、
</c:forEach></h3>
</body>
</html>
Map集合的输出,有一个前提,就是必须清楚的掌握Map和Map.Entry接口的关系
表达式语言操作的是反射
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<%
String info = "www.youkuaiyun.com";
pageContext.setAttribute("ref",info);
%>
<h3>拆分.结果是:
<c:forTokens items="${ref}" delims="." var="con">
${con}、
</c:forTokens>
</h3>
<h3>拆分:结果是:
<c:forTokens items="www:youkuaiyun.com" delims=":" var="con">
${con}、
</c:forTokens>
</h3>
</body>
</html>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:import url="http://www.youkuaiyun.com" charEncoding="UTF-8">
</c:import>
</body>
</html>
被包含的页面,里面需要传递参数
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<h3>name参数${param.name}</h3>
<h3>url参数${param.url}</h3>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:import url="param_1.jsp" charEncoding="UTF-8">
<c:param name="name" value="dageda"/>
<c:param name="url" value="school"/>
</c:import>
</body>
</html>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:url value="http://www.youkuaiyun.com" var="urlinfo">
<c:param name="name" value="好啊"/>
<c:param name="url" value="school"/>
</c:url>
<a href="${urlinfo} }">新地址</a>
</body>
</html>
实际上就相当于帮助用户自动的进行地址重写拼凑操作了
如果传递的是中文,会帮助用户自动的进行编码的转换
因为response.sendRedirect() 属于JSP 的代码,既然是JSP 代码,则肯定要使用Scriptlet 进行包含
而一个JSP 页面中最好不要包含任何的Scriptlet代码,那么就可以通过此标签完成了
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.youkuaiyun.com/jst/core"%>
<html>
<head> <title>欢迎光临</title>
</head>
<body>
<c:redirect url="param_1.jsp">
<c:param name="name" value="dageda"/>
<c:param name="url" value="school"/>
</c:redirect>
</body>
</html>
小结
核心标签库完成了一些基本的操作功能,可以定义属性、输出、判断、迭代
从开发上来讲,判断、迭代操作使用较多。
至于以后讲解的其他标签,在开发之中的使用机率是相当小,而只有c 标签最重要,也是必须掌握的部分