JSTL引入
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库
如果要使用JSTL,则必须将jstl.jar和 standard.jar文件放到classpath中,如果你还需要使用XML processing及Database access (SQL)标签,还要将相关JAR文件放到classpath中,这些JAR文件全部存在于下载回来的zip文件中。
c:out内容输出标签
c_out.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("people", "陈晨" );
%>
<h2><c:out value="${people }"></c:out></h2>
<h2><c:out value="${people2 }" default="没有取到"></c:out></h2>
</body>
</html>
c:set 用来设置4种属性范围值的标签
com.ruanku.model
People.java
package com.ruanku.model;
public class People {
private int id;
private String name;
private int age;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
c_set.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="people" value="皮卡丘" scope="request"></c:set>
<h2><c:out value="${people }"></c:out></h2>
<jsp:useBean id="people2" class="com.ruanku.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people2 }" value="007"></c:set>
<c:set property="name" target="${people2 }" value="cc"></c:set>
<c:set property="age" target="${people2 }" value="12"></c:set>
<h2>编号:${people2.id }</h2>
<h2>姓名:${people2.name }</h2>
<h2>年龄:${people2.age }</h2>
</body>
</html>
c:remove用来处理程序中产生的异常删除指定范围中的属性
c_remove.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="people" value="皮卡丘" scope="request"></c:set>
<h2><c:out value="${people }" default="没人啊"></c:out></h2>
<c:remove var="people" scope="request"/>
<h2><c:out value="${people }" default="没人了"></c:out></h2>
</body>
</html>
c:catch用来处理程序中产生的异常
c_catch.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:catch var="errMSG">
<%
int a=1/0;
%>
</c:catch>
<h2>${errMSG }</h2>
</body>
</html>
c:if 用来条件判断;
c_if.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="people" class="com.ruanku.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people }" value="007"></c:set>
<c:set property="name" target="${people }" value="cc"></c:set>
<c:set property="age" target="${people }" value="12"></c:set>
<c:if test="${people.id=='007' }" var="r" scope="page">
<h2>是我本人</h2>
</c:if>
<c:if test="${people.age<18 }">
<h2>未成年人</h2>
</c:if>
</body>
</html>
c:choose,c:when,c:otherwise用来多条件判断;
<pre name="code" class="html"><c:choose>
<c:when test="${people.age<18 }">
<h2>小于18</h2>
</c:when><c:when test="${people.age==18 }">
<h2>等于18</h2>
</c:when>
<c:otherwise>
<h2>大于18</h2>
</c:otherwise>
</c:choose>
c_forEach.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.ruanku.model.People" %>
<%@ page import="java.util.*" %>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String dogs[]={"dog1","dog2","dog3","dog4"};
pageContext.setAttribute("dogs", dogs);
%>
<c:forEach var="dog" items="${dogs }">
${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" step="2">
${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" begin="1" end="2">
${dog }
</c:forEach>
<hr/>
<%
List<People> pList=new ArrayList<People>();
pList.add(new People(1,"张三",10));
pList.add(new People(2,"李四",20));
pList.add(new People(3,"王五",30));
pageContext.setAttribute("pList", pList);
%>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<c:forEach var="p" items="${pList }">
<tr>
<td>${p.id }</td>
<td>${p.name }</td>
<td>${p.age }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
c:fortokens分隔输出;
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String str1="www.baidu.com";
String str2="张三,李四,王五";
pageContext.setAttribute("str1", str1);
pageContext.setAttribute("str2", str2);
%>
<c:forTokens items="${str1 }" delims="." var="s1">
${s1 }
</c:forTokens>
<c:forTokens items="${str2 }" delims="," var="s2">
${s2 }
</c:forTokens>
</body>
</html>
c:import 导入页面
c_import.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:import url="c_forEach.jsp"></c:import>
<c:import url="c_if.jsp"></c:import>
</body>
</html>
c:url生成一个url地址;
c_url.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:url value="http://www.baidu.com" var="url">
<c:param name="name" value="cc"></c:param>
<c:param name="age" value="22"></c:param>
</c:url>
<a href="${url }">百度</a>
</body>
</html>
c:redirect客户端跳转
c_redirect.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:redirect url="target.jsp">
<c:param name="name" value="cc"></c:param>
<c:param name="age" value="22"></c:param>
</c:redirect>
</body>
</html>
target.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>name:${param.name }</h2>
<h2>age:${param.age }</h2>
</body>
</html>