10.21 EL表达式(只能在jsp中使用)

10.21    EL表达式(只能在jsp中使用)

EL全称:Expression Language    表达式语言,可以替代JSP页面中数据访问时的复杂编码,可以非常便捷地取出域对象(pageContext、request、session、application)中保存的数据,前提是一定要先setAttribute,EL就相当于在简化getAttribute

${变量名}  变量名就是setAttribute对应的key值。

1、EL对于4种域对象的默认查找顺序:

pageContext-》request-》session-》application

按照上述的顺序进行查找,找到立即返回,在application中也无法找到,则返回null

 2、指定作用域进行查找

pageContext:${pageScope.name}

request:${requestScope.name}

session:${sessionScope.name}

application:${applicationScope.name}

数据级联:

<%
    //pageContext.setAttribute("name","page");
    //request.setAttribute("name","request");
    /session.setAttribute("name","session");
    //application.setAttribute("name","application");            //request.getRequestDispatcher("el2.jsp").forward(request,response);
    User user = new User(1,"张三",86.5,new Address(1,"小寨"));
    pageContext.setAttribute("user",user);
%>
<%--  ${applicationScope.name}--%>
<table>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>成绩</th>
        <th>地址</th>
    </tr>
    <tr>
        <td>${user.id}</td>    
        <%-- <td>${user.num}</td> --%>
        <%-- <%
            ((User)pageContext.getAttribute("user")).getNum();
        %>     --%>
        <td>${useruser["name"]}</td>
        <td>${user.score}</td>
        <td>${user.address.value}</td>
    </tr>
</table>

${user.name}也可以写成${user["id"]}

原文地址:https://www.cnblogs.com/wangdayang/p/14147066.html