JSTL表达式应用

1.JSTL简介
JSP 标准标签库(JSP Standard Tag Library,JSTL)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问。
2.使用
1.导入jstl.jar和standard.jar包
2.在jsp页面中利用taglib标签,如下图:其中,prefix就是你下面使用的别名。JSTL表达式应用

3.有关逻辑的简单示范(通常和EL表达式一起使用)
1.if标签(属性:test--如果为true则返回标签体内容,相当于java中的if语句)
JSTL表达式应用JSTL表达式应用
2.choose标签(相当于Java中的switch语句)
JSTL表达式应用
JSTL表达式应用
3.foreach标签使用(相当于Java中的for循环)
JSTL表达式应用
其中,items表示当前你要遍历的对象,比如数组。var表示其中的一个元素,varStatus : 当前状态,可以获取序号.
JSTL表达式应用
4.综合使用案例
<%@ page import="com.lyg.servlet.simple_class.Student" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><!--导入格式化标签库 -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>综合案例</title>
</head>
<body>
<%
Student student = new Student("10001","张三","北京",18,new Date());
List<Student>studentList = new ArrayList<>();
studentList.add(student);
student = new Student("10001","张三","北京",18,new Date());
studentList.add(student);
student = new Student("10002","sfe","上海",19,new Date());
studentList.add(student);
student = new Student("10003","lili","兰州",20,new Date());
studentList.add(student);
student = new Student("10004","fefe","河南",28,new Date());
studentList.add(student);
student = new Student("10005","tmo","丰台",20,new Date());
studentList.add(student);
student = new Student("10006","lisi","浙江",34,new Date());
studentList.add(student);
student = new Student("10007","wangwu","温州",28,new Date());
studentList.add(student);
student = new Student("10008","jack","深圳",24,new Date());
studentList.add(student);
request.setAttribute("students",studentList);
%>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>序号</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>生日</td>
<td>地址</td>
</tr>
<c:forEach items="${students}" var="stu" varStatus="s">
<tr>
<td>${s.count}</td>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td><fmt:formatDate value="${stu.birthday}"></fmt:formatDate></td>
<td>${stu.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

JSTL表达式应用

猜你喜欢

转载自blog.51cto.com/13660798/2343727