一:EL表达式(Expression Language)
<%@ page import="cn.laowang.servlets.Address"%>
<%@ page import="cn.laowang.servlets.User"%>
<%@ page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--
EL(Expression Language)表达式
语法:${ }
--%>
<%
int a = 1234;
pageContext.setAttribute("a", a);
pageContext.setAttribute("scope", "pageContext");
request.setAttribute("scope", "request");
session.setAttribute("scope", "session");
application.setAttribute("scope", "application");
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
request.setAttribute("list", list);
Map map = new HashMap();
map.put("name", "zhangsan");
map.put("gender", "男");
map.put("address", "天河区");
request.setAttribute("map", map);
User user = new User("zhangsan","kahdfasd","张三","男",20,new Address("广东省","广州市","天河区"));
request.setAttribute("user", user);
request.setAttribute("aaa", "");
request.setAttribute("bbb", null);
request.setAttribute("ccc", new ArrayList());
request.setAttribute("ddd", new HashMap());
%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h3>EL表达式从哪里取值?一般情况下,从作用域对象中取值</h3>
${a}
<h3>EL表达式优先从哪个作用域对象中取值?默认情况下从最小作用域取值</h3>
${scope}
<h3>EL表达式从指定作用域对象中取值:pageScope、requestScope、sessionScope、applicationScope</h3>
${pageScope.scope} --- ${requestScope.scope} --- ${sessionScope.scope} --- ${applicationScope.scope}
<h3>EL表达式可以解析什么类型的数据?基本数据类型、对象类型、复合对象类型、Map、List</h3>
${list[2]} --- ${map.address} --- ${user.realname} --- ${user.addr.town}
<h3>EL也可以取请求参数的数据:param、paramValues</h3>
${param.name} --- ${paramValues.fav[2]}
<h3>EL表达式的[]内也可以做EL表达式的解析</h3>
${user.realname} --- ${user["realname"]} --- ${user[param.type]}
<h3>EL表达式可以做为空判断:空字符串、null、空map、空list这几个值为true</h3>
${empty aaa} --- ${empty bbb} --- ${empty ccc} --- ${empty ddd} --- ${empty scope}
<h3>EL表达式可以做基本的逻辑运算,和数学运算</h3>
<h4>EL表达式没有字符串拼接的概念,也没有字符的概念</h4>
${5+4} --- ${5-4} --- ${5*4} --- ${5/4} --- ${5%4} --- ${5+"4"} ---
${5>4} --- ${5 < 4} --- ${5>=4} --- ${5 le 4} --- ${5 eq 4}
<h3>EL表达式中pageContext可以获取pageContext作用域对象本身</h3>
${pageContext}
</body>
</html>
二.JSTL(JavaServer Pages Standard Tag Library,JSP标准标签库)
<%@page import="cn.laowang.servlets.User"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
request.setAttribute("weather", "今天天气怎么样?");
pageContext.setAttribute("scope", "pageContext");
request.setAttribute("scope", "request");
session.setAttribute("scope", "session");
application.setAttribute("scope", "application");
request.setAttribute("score", 80);
List list = new ArrayList();
list.add(new User("zhangsan","张三"));
list.add(new User("lisi","李四"));
list.add(new User("wangwu","王五"));
list.add(new User("zhaoliu","赵柳"));
request.setAttribute("persons", list);
request.setAttribute("d1", new Date());
%>
<%--
JSP标准标签库
c标签 ※
fmt标签
fn标签
sql标签
xml标签
--%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h3>c:out标签用来将作用域对象中数据输出到页面中</h3>
<c:out value="${weather}" default="出门自己看!"></c:out>
<h3>c:set标签用于将数据放入到作用域对象中,默认放入到最小的作用域对象中。通过scope属性指定作用域</h3>
<h4>c:set还可以在标签中间添加html代码作为标签的value属性值</h4>
<c:set var="name" value="zhangsan" scope="request"></c:set>
${requestScope.name}
<c:set var="mytable">
<table border="1" style="border-collapse: collapse;">
<tr><td>1.1</td><td>1.2</td></tr>
<tr><td>2.1</td><td>2.2</td></tr>
</table>
</c:set>
${mytable} --- ${mytable} --- ${mytable}
<h3>c:remove将指定key从作用域对象中删除,通过scope属性指定作用域</h3>
<c:remove var="scope" scope="page"/>
${scope}
<h3>c:if标签做判断</h3>
<c:if test="${score >= 90}" var="flag">
成绩优秀!
</c:if>
<c:if test="${!flag}">
成绩不优秀!
</c:if>
<h3>c:choose、c:when、c:otherwise组标签可以表示多条件判断</h3>
<h4>1.c:otherwise不能出现在c:when之上</h4>
<h4>2.c:otherwise和c:when之间、c:when和c:when之间不能出现其他的字符</h4>
<c:choose>
<c:when test="${score >= 90}">成绩优秀!</c:when>
<c:when test="${score >= 80}">成绩良好!</c:when>
<c:when test="${score >= 60}">成绩及格!</c:when>
<c:otherwise>成绩不及格</c:otherwise>
</c:choose>
<h3>c:forEach遍历数据,items里填入一个可以遍历的结构</h3>
<c:forEach begin="10" end="20" step="2" var="num" varStatus="status">
${num} --- ${status.first} --- ${status.last} --- ${status.index} --- ${status.count} <br/>
</c:forEach>
<table border="1" style="border-collapse: collapse;">
<tr><td>用户名</td><td>真实姓名</td></tr>
<c:forEach items="${persons}" var="user" >
<tr><td>${user.username}</td><td>${user.realname}</td></tr>
</c:forEach>
</table>
<%-- <%@ include file="http://www.baidu.com" %> --%>
<%-- <jsp:include page="http://www.baidu.com"></jsp:include> --%>
<!-- 将一个URL中的内容导入在本页面中一起显示 -->
<%-- <c:import url="http://www.baidu.com"></c:import> --%>
<!-- 重定向 -->
<%-- <c:redirect url="testel.jsp?aaa=bbb"></c:redirect> --%>
<fmt:formatDate value="${d1}" pattern="yyyy年MM月dd日 HH时mm分ss秒"/>
</body>
</html>