什么是EL表达式
Expression Language(表达式语言)
EL的功能
替代JSP页面中访问数据时的复杂编码
执行表达式
EL的特点
自动转换类型
使用简单
EL表达式搜索域数据的顺序
El表达式主要是在jsp页面中输出数据。
主要是输出域对象中的数据
当四个域中都有相同的key的数据时,EL表达式或按照四个域的从小到大的顺序进行搜索,找到输出
<body>
<%
pageContext.setAttribute("key","pageContext");
request.setAttribute("key","request");
session.setAttribute("key","session");
application.setAttribute("key","application");
%>
<h3>EL表达式输出key的值是:${key}</h3>
</body>
EL表达式session、List、循环取值
Servlet文件代码
@WebServlet("/test")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Dog> list=new ArrayList<>();
Dog d=new Dog();
d.setId(1);
d.setName("小黄");
Dog d1=new Dog();
d1.setId(1);
d1.setName("小白");
list.add(d);
list.add(d1);
req.setAttribute("list",list);
req.getSession().setAttribute("d",d);
req.setAttribute("a",10);
req.getSession().setAttribute("b",20);
req.getSession().setAttribute("flag","<a href='#'>哈哈哈</a>");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
JSP文件代码
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<!--1.美刀符+花括号用于从作用域中取值
2.可以直接通过.获取对象值,对象不能没有set/get方法,通过.实际上就是调用了方法的set/get方法
3.取值如果同名的话从四大作用域从小到大的顺序取出,要是没有此变量显示空串
-->
${requestScope.a}<br/>
${sessionScope.b}<br/>
${sessionScope.d.id}<br/>
${sessionScope.d.name}
<!--List只想要其中某个的值可以直接通过下标获取-->
<!--循环必备条件var对象名 items对象-->
<table border="2">
<c:forEach var="ob" items="${list}">
<tr>
<td>${ob.id}</td>
<td>${ob.name}</td>
</tr>
</c:forEach>
</table>
<c:set var="flag" value="千夫人" scope="session"></c:set>
<c:out value="${flag}">
</c:out>
</body>
</html>
结果:
EL表达式语法
属性范围 |
EL表达式中的名称 |
属性范围 |
EL表达式中的名称 |
page |
pageScope |
session |
sessionScope |