一、JSTL简介
JSTL:(JSP Standard Tag Library)
JSP标准标签库
。原本是Sun公司定义的一套标准,而Apache组织基于这套标准开发的一套标签库后又转给Sun公司,被称为JSTL,称为Java5.0核心
。JSTL使用简单的标签来表现复杂的逻辑以及使用简单的形式表现运算的关系。
JSTL和EL结合替换页面中的<% %>
和<%= %>
,可以获得Bean中的属性,操作作用域,获得其中的值,做一些逻辑运算或关系运算,写一些循环或分支结构,使用JSTL中的标签还可以实现跳转,重定向,对作用域进行绑定和移除等。
JSTL的作用:
- ①增加可读性。
- ②将业务封装到JSTL可以方便重用。
- ③数据与显示分离。
- ④简化JSP开发,易于维护。
- ⑤可以对其进行自定义扩展。
二、如何使用JSTL
将标签库对应的jar包copy到WEB-INF/lib目录下
使用taglib指令导入要使用的JSP标签
<%@taglib uri="http://java.sum.com/jsp/jstl/core" prefix="c" %>
-
uri:标签的命名空间。
-
prefix:命名空间的前缀。
三、JSTL标签
(1)IF标签
语法:
<c:if test =" " var = " " scope=" ">
</c:if>
test属性值
为true时,执行标签体的内容。Test属性
可以使用EL表达式赋值。Var属性
指定绑定的范围(page,request,session,application)- 注意:var和scopr要配合使用。
下面的代码演示IF标签的使用:
定义具有name和gender属性的Employee类。在JSP页面中创建Employee对象,并为属性赋值。gender属性存储man和woman用来代表男和女,使用if标签判断gender属性的值,值是man输出男,不是man则输出女。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<%
Employee employee = new Employee("tom","man");
request.setAttribute("employee",employee);
%>
姓名:${employee.name}<br/>
性别:
<c:if test="${employee.gender=='man' }" var="rs" scope="request">男</c:if>
<c:if test="${!rs}">女</c:if>
</body>
</html>
Employ.java雇员类
package bean;
/**
* 雇员类
*
* @author QianliangGuo
*/
public class Employee {
private String name;
private String gender;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, String gender) {
super();
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
(2)Choose标签
语法:
<c:choose>
<c:when test=" "></c:when>
...
<c:otherwise></c:otherwise>
</c:choose>
when表示一个处理分支,当test为true时会执行该分支,可以出现1次或者多次。otherwise表示例外,可以出现0次或1次。
仍然使用上面IF标签中的例子:
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<%
Employee employee = new Employee("tom","man");
request.setAttribute("employee",employee);
%>
姓名:${requestScope.employee.name}<br/>
性别:
<c:choose>
<c:when test="${requestScope.employee.gender=='man'}">男</c:when>
<c:when test="${requestScope.employee.gender=='woman'}">女</c:when>
<c:otherwise>其他</c:otherwise>
</c:choose>
</body>
</html>
(3)Foreach标签
用来遍历集合或数组,语法:
<c:forEach var="" items="">
...
</c:forEach>
var属性
指定一个绑定名称,容器每次从集合中取一个对象,然后绑定到pageContext对象上。Items属性
指定要遍历的集合,一般使用EL表达式来赋值。varStatus属性
指定一个绑定名称,绑定值是一个由容器创建的对象,该对象封装了当前迭代的状态。Index
返回正在被迭代的对象的下标,从0开始。Count
返回时第几次迭代,从1开始。
使用ForEach标签输出request中绑定的集合中对象的属性值,实现不同对象隔行变色的功能。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
<style type="text/css">
.s0{
color:red;
}
.s1{
color:pink;
}
</style>
</head>
<body>
<%
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Jim","man"));
employees.add(new Employee("Kitty","woman"));
employees.add(new Employee("KangKang","man"));
employees.add(new Employee("Tom","man"));
session.setAttribute("employees",employees);
%>
<c:forEach var ="emp" items="${sessionScope.employees}" varStatus="s">
<p class="s${s.index%2}">
姓名:${emp.name }
性别:<c:if test="${emp.gender=='man'}" var="rs">男</c:if>
<c:if test="${!rs}">女</c:if>
index:${s.index}
count:${s.count}
</p>
</c:forEach>
</body>
</html>
效果如下:
四、JSTL其他标签
标签名 | 作用 |
---|---|
c:url | 实现跳转 |
c:redirect | 自动重定向跳转 |
c:set | 作用域添加值 |
c:remove | 作用域移除值 |
c:catch | 捕捉异常 将异常信息存入msg变量 |
c:import | 将其他页面嵌入到当前页面 |
c:out | 输出变量 |
对jstl的标签做演示jstl01.jsp
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<!--引入标准标签库-->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!--定义具有name和gender属性的Employee类,在JSP页面中创建Employee对象,并为属性赋值.
gender属性存储man和woman用来代表男和女,使用if标签判断gender属性的值,值是man输出男,不是man则输出女.
-->
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
<style type="text/css">
.s0{
color:red;
}
.s1{
color:bule;
}
</style>
</head>
<body>
<!-- 创建Employee对象,并为属性赋值,且存入request作用域 -->
<%
Employee employee = new Employee("tom","man");
request.setAttribute("employee",employee);
//创建List<Employee>
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Jim","man"));
employees.add(new Employee("Kitty","woman"));
employees.add(new Employee("KangKang","man"));
%>
<h1>if标签</h1>
<!-- el获得绑定的参数,var记录test结果的变量 scope确定var变量的作用范围 当rs为true时输出男 -->
姓名:${requestScope.employ.name}<br/>
性别:
<c:if test="${employee.gender=='man' }" var="rs" scope="request">
男
</c:if>
<!-- 当rs为false时,输出女-->
<c:if test="${!rs}">
女
</c:if>
<h1>choose标签</h1>
性别:
<c:choose>
<c:when test="${requestScope.employee.gender=='man'}">男</c:when>
<c:when test="${requestScope.employee.gender=='woman'}">女</c:when>
<c:otherwise>其他</c:otherwise>
</c:choose>
<hr>foreach标签</h1>
<c:forEach var ="emp" items="${sessionScope.employees}">
<p class="${s.index%2}">
姓名:${emp.name }
性别:<c:if test="${emp.gender=='man"}" var="rs">男</c:if>
<c:if test="${!rs}">女</c:if>
index:${s.index}
count:${s.count}
</p>
</c:forEach>
<h1>c:url实现跳转</h1>
<!-- 使用c:url,即使禁用cookie的情况,也会自动在连接加上jsessionid,保证能够访问session对象 -->
<%session.setAttribute("name","hello kitty"); %>
<a href="<c:url value='jstl02.jsp'/>">跳转到jstl02.jsp</a>
<h1>c:redirect重定向</h1>
<!-- 访问jstl01.jsp时直接重定向到jstl02.jsp -->
<%session.setAttribute("age", 100); %>
<%-- <c:redirect url="jstl02.jsp"/> --%>
<h1>c:set作用域添加值 c:remove作用域移除值</h1>
<c:set var="num" scope="session" values="${1+1}"></c:set>
set之后:num=
<c:remove var="num" scope="session"/>
remove之后:num=${session.Scope.num}
<h1>c:catch标签获得异常</h1>
<c:catch var="msg">
<%Integer.parseInt("123a");%>
</c:catch>
异常信息:${msg}
<h1>c:import 将其他页面嵌入当前页面</h1>
<c:import url="index.jsp"/>
<h1>c:out输出变量</h1>
<c:out value="${num default="123"}"/><br/>
<c:out value="123"/>
<c:out value="<table>" />
<!-- escapeXml属性可以过滤特殊字符,false时开启过滤 -->
<c:out value="<table>" escapeXml="false"/>
</body>
</html>
jstl02.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
name:${sessionScope.name }
age:${sessionScope.age}
</body>
</html>
演示效果: