JSP EL JSTL表达式
一、什么是EL表达式
表达式语言(Expression Language,EL),EL表达式是用”${}”括起来的脚本,用来更方便的读取对象!
- EL表达式主要用来读取数据,进行内容的显示!
1、为什么要使用EL表达式?
EL语法很简单,最大的特点就是使用上很方便
使用EL表达式可以方便地读取对象中的属性、提交的参数、JavaBean、甚至集合!
EL表达式如果找不到相应的对象属性,返回的的空白字符串“”,而不是null,这是EL表达式最大的特点!
1、结构与用法
- EL主要的语法结构:
${sessionScope.user.sex}
-
[ ]与.运算符
EL 提供“.“和“[ ]“两种运算符来存取数据。
当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用“[ ]“。例如:
u
s
e
r
.
M
y
−
N
a
m
e
应
当
改
为
{user.My-Name}应当改为
user.My−Name应当改为{user[“My-Name”] }
如果要动态取值时,就可以用“[ ]“来做,而“.“无法做到动态取值。例如:
${sessionScope.user[data]}中data 是一个变量
- 变量
EL存取变量数据的方法很简单,例如:${username}
。它的意思是取出某一范围中名称为username的变量。
因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。
假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。
属性范围在EL中的名称
属性范围(jstl名称) | EL中的名称 |
---|---|
Page | PageScope |
Request | RequestScope |
Session | SessionScope |
Application | ApplicationScope |
我们也可以指定要取出哪一个范围的变量:
范例 | 说明 |
---|---|
${pageScope.username} | 取出Page范围的username变量 |
${requestScope.username} | 取出Request范围的username变量 |
${sessionScope.username} | 取出Session范围的username变量 |
${applicationScope.username} | 取出Application范围的username变量 |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp</title>
</head>
<body>
<%
application.setAttribute("name","application");
session.setAttribute("name","session");
request.setAttribute("name","request");
pageContext.setAttribute("name","pageContext");
%>
<br>--------------------使用java语言---------------------------<br>
application中的值:<%= application.getAttribute("name") %> <br>
session中的值:<%= session.getAttribute("name") %> <br>
request中的值:<%= request.getAttribute("name") %> <br>
pageContext中的值:<%= pageContext.getAttribute("name") %> <br>
<br>--------------------使用EL表达式---------------------------<br>
application中的值:${applicationScope.name} <br>
session中的值:${sessionScope.name} <br>
request中的值:${requestScope.name} <br>
pageContext中的值:${pageScope.name} <br>
<br>----------------使用EL表达式,省略域对象---------------------<br>
application中的值:${name} <br>
</body>
</html>
2、支持的运算
(1)数学运算
(2)比较运算 > gt < lt >= ge <= le == eq != !=
(3)逻辑预算 && || !
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL运算</title>
</head>
<body>
<%
request.setAttribute("num1","12");
request.setAttribute("num2","14");
application.setAttribute("flag1",true);
application.setAttribute("flag2",false);
%>
<br>--------------------使用java语言---------------------------<br>
<%
String num1 = (String)request.getAttribute("num1");
String num2 = (String)request.getAttribute("num2");
int num3 = Integer.parseInt(num1) + Integer.parseInt(num2);
boolean flag1 = (Boolean) application.getAttribute("flag1");
boolean flag2 = (Boolean) application.getAttribute("flag2");
boolean flag3 = flag1 && flag2;
//输出方式一
out.write(Boolean.toString(flag3));
%>
<!-- 输出方式二 -->
<h1><%=num3%></h1>
<br>--------------------使用EL表达式--------------------------<br>
<h1>${ requestScope.num1 + requestScope.num2 }</h1>
<h1>${ requestScope.num1 > requestScope.num2 }</h1>
<h1>${ applicationScope.flag1 && applicationScope.flag2 }</h1>
</body>
</html>
3、强调
1、注意当表达式根据名称引用这些对象之一时,返回的是相应的对象而不是相应的属性。例如:即使现有的 pageContext 属性包含某些其他值,${pageContext} 也返回 PageContext 对象。
2、 注意 <%@ page isELIgnored=“true” %> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言。
二、什么是JSTL
JSTL全称为 JSP Standard Tag Library 即JSP标准标签库。
JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历、数据的输出、字符串的处理、数据的格式化等等!
1、为什么要使用JSTL
- 实现了JSP页面中的代码复用(基于标签库原理,重复率较高的代码块支持复用,提高效率)
- 书写JSP页面时可读性更强(长得很像xml,方便前端查看和参与开发)
2、使用JSTL标签库步骤:
- 导入jstl.jar和standard.jar开发包
- 在JSP页面中用tablib指令引入需要用到的JSTL标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3、重要标签的使用
1、<c:set>
该标签有5个属性,用起来有稍微有些复杂了!现在要记住的就是:var属性操作的是Integer、Double、Float、String等类型的数据,target属性操作的是JavaBean或Map对象的数据,scope代表的是Web域,value是值,property是对象的属性!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title> c:set </title>
</head>
<body>
<!--
相当于
<%-- <% --%>
<%-- request.setAttribute("name","zhangsan");--%>
<%-- %> --%>
-->
<c:set scope="request" var="name" value="zhangsan" />
通过JSTL标签添加的作用域中的值:${requestScope.name} <br>
<c:set scope="application" var="name" value="lisi" />
通过JSTL标签添加的作用域中的值:${applicationScope.name} <br>
<c:set scope="request" var="name" value="wangwu" />
通过JSTL标签添加的作用域中的值:${requestScope.name} <br>
<c:set scope="page" var="name" value="zhaoliu" />
通过JSTL标签添加的作用域中的值:${pageScope.name} <br>
</body>
</html>
2、<c:if>
控制哪些内容能够输出到响应体
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> c:if </title>
</head>
<body>
<c:set scope="page" var="age" value="20"/>
<br>------------------------------使用java语言-------------------------------------<br>
<%
if( Integer.parseInt((String)pageContext.getAttribute("age")) >= 18 ){
%>
输入:欢迎光临!
<% } else { %>
输入:未满十八,不准入内!
<% } %>
<br>------------------------------使用JSTL标签-------------------------------------<br>
<c:if test="${ age ge 18 }">
输入:欢迎光临!
</c:if>
<c:if test="${ age lt 18 }">
输入:未满十八,不准入内!
</c:if>
</body>
</html>
3、<c:choose>
if标签没有else的功能,如果需要类似于java中的if else流程就需要使用choose标签。
choose标签需要联合when和otherwise标签一起使用!
在jsp中进行多分支判断,决定哪个内容写入响应体
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> c:choose </title>
</head>
<body>
<c:set scope="page" var="age" value="6"/>
<br>------------------------------使用java语言-------------------------------------<br>
<%
if( Integer.parseInt((String)pageContext.getAttribute("age")) == 18 ){
%>
输入:您今年成年了
<% } else if( Integer.parseInt((String)pageContext.getAttribute("age")) > 18 ){ %>
输入:您已经成年了
<% } else {%>
输出:您还是个孩子
<% } %>
<br>------------------------------使用JSTL标签-------------------------------------<br>
<c:choose>
<c:when test="${age eq 18}">
输入:您今年成年了
</c:when>
<c:when test="${age gt 18}">
输入:您已经成年了
</c:when>
<c:otherwise>
输入:您还是个孩子
</c:otherwise>
</c:choose>
</body>
</html>
4、<c:forEach>
循环遍历
使用方式
<c:forEach var="申明循环变量的名称" begin="初始化循环变量"
end="循环变量可以接受的最大值" step="循环变量的递增或递减值">
*** step属性可以不写,默认递增1
*** 循环变量默认保存在pageContext中
</c:forEach>
例子:
<%@ page import="com.zn.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> c:forEach </title>
</head>
<body>
<%
pageContext.setAttribute("students",new ArrayList(){{
add(new Student("s1","zhangsan",16));
add(new Student("s2","lisi",19));
add(new Student("s3","wangwu",15));
}});
pageContext.setAttribute("stuMap", new HashMap(){{
put("m1",new Student("s1","zhangsan",16));
put("m2",new Student("s2","lisi",18));
put("m3",new Student("s3","wangwu",15));
}});
%>
<br>------------------------使用java语言------------------------------<br>
<table>
<tr><td>学号</td><td>姓名</td><td>年龄</td></tr>
<%
List<Student> stus = (ArrayList<Student>)pageContext.getAttribute("students");
for (int i = 0; i < stus.size(); i++) {
%>
<tr><td><%=stus.get(i).getSid()%></td>
<td><%=stus.get(i).getName()%></td>
<td><%=stus.get(i).getAge()%></td>
</tr>
<% } %>
</table>
<br>----------------------使用JSTL标签读取list-----------------------<br>
<table>
<tr><td>学号</td><td>姓名</td><td>年龄</td></tr>
<c:forEach var="student" items="${students}">
<tr><td>${student.sid}</td>
<td>${student.name}</td>
<td>${student.age}</td>
</tr>
</c:forEach>
</table>
<br>---------------------使用JSTL标签读取map------------------------<br>
<table>
<tr><td>学号</td><td>姓名</td><td>年龄</td></tr>
<c:forEach var="student" items="${stuMap}">
<tr>
<td>${student.key}</td>
<td>${student.value.sid}</td>
<td>${student.value.name}</td>
<td>${student.value.age}</td>
</tr>
</c:forEach>
</table>
<br>--------------使用JSTL标签读取指定for循环-----------------------<br>
<select>
<c:forEach var="item" begin="1" end="10" step="1">
<option> ${item} </option>
</c:forEach>
</select>
</body>
</html>