一.理解并会使用EL表达式
EL表达式Expression Language(表达式语言)
用来替代jsp中数据访问时的复杂编码
能够自动类型转换
使用简单
语法:${EL exression }
获取变量名:
<%--脚本预言的获取方式--%>
<h1>获取变量中的name属性:<%=request.getAttribute("name")%></h1>
<%--EL表达式的获取方式--%>
<h1>获取变量中的name属性:${name}</h1>
<%--也可指定在某个作用域中查找--%>
<h1>${requestScope.name}</h1>
<h1>${sessionScope.name}</h1>
<h1>${pageScope.name}</h1>
<h1>${applicationScope.name}</h1>
<%--el表达式获取对象中的数据1.点操作符 2.[]操作符--%>
<h1>点操作符:${user.uname}</h1>
<h1>中括号操作符:${user["uname"]}</h1>
<%--el表达式获取list集合--%>
<h1>list集合中的数据1:${list[0]}</h1>
<h1>list集合中的数据2:${list[1]}</h1>
<%--el表达式获取map集合--%>
<h1>map集合中的数据1:${map.one}</h1>
<h1>map集合中的数据2:${map["two"]}</h1>
<%--关系操作符 支持 > < == != >= <= gt lt eq ne ge le--%>
<h1>${35>45}</h1>
<h1>${35!=45}</h1>
<h1>${35 eq 45}</h1>
<%--逻辑运算符 && || ! and or not--%>
<h1>${45 > 30 or 30 < 45 }</h1>
<h1>${!(45 > 30) }</h1>
<h1>${45 > 30 && 30 < 45 }</h1>
<%--empty判空处理 可以 判断 长度为0的字符串 size为0集合 为null的对象--%>
<h1>${empty age}</h1>
<h1>${! empty map}</h1>
<h1>${not empty user}</h1>
<%--el表达式获取传递的参数 http://localhost:8080/ch07/user?age=18--%>
<h1>${param.age}</h1>
<%--获取多个参数--%>
<h1>${paramValues.age[0]}</h1>
<%--el表达式获取作用域 有了作用域就可以使用内置对象中的方法 --%>
<h1>${pageContext.request.contextPath}</h1><%--request作用域--%>
<h1>${pageContext.request.session}</h1><%--session作用域--%>
<h1>${pageContext.page}</h1><%--page作用域--%>
<h1>${pageContext.out}</h1><%--out对象--%>
<h1>${pageContext.servletContext}</h1><%--application对象--%>
二.理解并会使用常用的JSTL标签
JSTL:JSP的标准标签库(JSP Standard Tag Library)
提供了一组标准的标签 实现动态页面 简化原生jsp页面编码
与EL表达式配合使用
使用JSTL:
1.在项目中加入jstl的jar包 推荐1.2版本
2.在页面中导入核心包:
<%--导入jstl核心标签库
prefix:前缀 随便起名一般习惯性的用头一个字母
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
scope="request":所存储的作用域范围
value:值
var:变量
--%>
<c:set var="age" scope="request" value="18"></c:set>
<%--存储对象中属性的值--%>
<c:set target="${user}" property="upwd" value="123456"></c:set>
${user.upwd}
<%--输出语句 也可以设置默认值--%>
<c:out value="${age}" default="20"></c:out>
<%--使用el表达式和c:out语句对比--%>
${"<a href='http://www.baidu.com'>点我跳转百度</a>"}<%--el表达式能够智能识别标签--%>
<c:out value="<a href='http://www.baidu.com'>点我跳转百度</a>" ></c:out><%--out语句默认情况下不能输出标签--%>
<%--默认情况下是忽略标签的,false就是不忽略--%>
<c:out escapeXml="false" value="<a href='http://www.baidu.com'>点我跳转百度</a>" ></c:out>
<%--remove删除指定作用域内的变量--%>
<c:remove var="age" scope="request"></c:remove>
<%--使用if进行登录控制
var="flag":保存条件返回的值 true或者false
scope="request" 保存变量的作用域
--%>
<c:if test="${empty userLogin}" var="flag" scope="request">
请登录后再来!
</c:if>
<c:if test="${not empty userLogin}">
欢迎你,${userLogin}
</c:if>
<%--使用choose来解决多条件判断--%>
<c:choose>
<%--所有的when那个条件成立就执行哪个--%>
<c:when test="${!empty userLogin}">
陶老师欢迎你${userLogin}
</c:when>
<c:when test="${!empty admin}">
管理员欢迎你${admin}
</c:when>
<c:when test="${!empty name}">
用户欢迎你${name}
</c:when>
<%--前面的条件都不成立 执行otherwise--%>
<c:otherwise>
登录异常,请重试
</c:otherwise>
</c:choose>
<%--
begin="":开始
end="":结束s
tep="":间隔
varStatus: 里面包含了索引 index和计数 count
--%>
<%-- <c:forEach step="1" begin="0" end="10" varStatus="statu" >
<h1>下标是:${statu.index}高考加油!!计数:${statu.count}</h1>
</c:forEach>--%>
<%--使用foreach 来遍历查询过来的集合
items:项 (要遍历的集合)
var: 变量 (遍历出来的元素)
--%>
<table border="1">
<tr>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
</tr>
<c:forEach items="${userList}" var="user" varStatus="statu" >
<tr <c:if test="${statu.count%2==0}">style="background-color: green"</c:if> >
<td>${user.uid}</td>
<td>${user.uname}</td>
<td>${user.upwd}</td>
</tr>
</c:forEach>
<%--使用foreach遍历map集合--%>
<c:forEach items="${map}" var="entry">
<p>键:${entry.key} ->值:${entry.value}</p>
</c:forEach>