EL表达式

EL获取数据

 <body>
    <h3>1.存取是普通的单值数据</h3>
    <%
        pageContext.setAttribute("pname", "pvalue");
        request.setAttribute("rname", "rvalue");
        session.setAttribute("sname", "svalue");
        application.setAttribute("aname", "avalue");
     %>
     <%=pageContext.getAttribute("pname") %>
     <%=request.getAttribute("rname") %>
     <%=session.getAttribute("sname") %>
     <%=application.getAttribute("aname") %>
     <hr/>
     <!-- 采用el方式获取 -->
     ${ pageScope.pname } <!-- 返回的是"" -->
     ${ requestScope.rname }
     ${ sessionScope.sname }
     ${ applicationScope.aname }
     <h3>2.获取数组的数据</h3>
     <%
        String[] arr = {"张三","李四","王五"};
        pageContext.setAttribute("Arrays", arr);
      %>
      ${ Arrays[0] }
      ${ Arrays[1] }
      ${ Arrays[2] }
      <h3>3.获取List集合的数据</h3>
      <%
        List<String> list = new ArrayList<String>();
        list.add("赵六");
        list.add("钱八");
        list.add("孙九");
        pageContext.setAttribute("List", list);
       %>
       ${ List[0] }
       ${ List[1] }
       ${ List[2] }
      <h3>4.获取Map集合的数据</h3>
      <%
        Map<String,String> map = new HashMap<String,String>();
        map.put("a", "张海");
        map.put("b", "钱素");
        map.put("c.1", "王时");
        pageContext.setAttribute("Map", map);
       %>
       ${ Map.a }
       ${ Map.b }
       ${ Map["c.1"] }
       <h3>5.获取对象的数据</h3>
     <%
    User user1 = new User(1,"aaa","123");
    User user2 = new User(2,"bbb","123");
    User user3 = new User(3,"ccc","123");

    List<User> userList = new ArrayList<User>();
    userList.add(user1);
    userList.add(user2);
    userList.add(user3);

    pageContext.setAttribute("userList", userList);
%>
${ userList[0].id } - ${ userList[0].username } - ${ userList[0].password }<br/>
${ userList[1].id } - ${ userList[1].username } - ${ userList[1].password }<br/>
${ userList[2].id } - ${ userList[2].username } - ${ userList[2].password }<br/>

  </body>

EL运算

<body>
    <h1>EL的功能二:执行运算</h1>
    <h3>EL执行算数运算</h3>
    <%
        pageContext.setAttribute("n1", "10");
        pageContext.setAttribute("n2", "20");
        pageContext.setAttribute("n3", "30");
        pageContext.setAttribute("n4", "40");
    %>
    ${ n1 + n2 + n3 } 
    <h3>EL执行逻辑运算</h3> 
    ${ n1 < n2 } - ${ n1 lt n2 } <!-- less than --><br/>
    ${ n1 > n2 } - ${ n1 gt n2 } <!-- great than --><br/>
    ${ n1 <= n2 } - ${ n1 le n2 } <!-- less equal --><br/>
    ${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal --><br/>
    ${ n1 == n2 } - ${ n1 eq n2 } <!-- equal --><br/>

    <h3>EL执行关系运算</h3> 
    ${ n1<n2 && n3 < n4 } - ${ n1<n2 and n3 < n4 }<br/>
    ${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/>
    ${ !(n1 < n2) } - ${ not(n1<n2) }

    <h3>EL执行三元运算</h3>
    ${ n1 < n2 ? "正确":"错误" }

    <h3>empty运算</h3>
    ${ user == null } - ${ empty user }
    ${ user != null } - ${ not empty user }
  </body>

EL操作Web开发中的对象

<body>
  <!-- 
    pageScope,requestScope,sessionScope,applicationScope - 获取JSP中域中的数据
    param,paramValues   - 接收参数.
    header,headerValues - 获取请求头信息
    initParam           - 获取全局初始化参数
    cookie              - WEB开发中cookie
    pageContext         - WEB开发中的pageContext.
 -->
    <h3>接收请求的参数</h3>
    <!-- http://localhost:8080/Web12/demo2_el/object.jsp?id=111&name=aaa&hobby=soccer&hobby=basketball -->
    <%=request.getParameter("id") %>
    <%=request.getParameter("name") %>
    <!-- 遍历数组 -->
    <%=Arrays.toString(request.getParameterValues("hobby")) %>
    <hr/>
    ${param.id }
    ${param.name }
    ${paramValues.hobby[0] }
    ${paramValues.hobby[1] }

    <h3>获取请求头</h3>
    <%=request.getHeader("User-Agent") %>
    <hr/>
    <!-- header.xxx (特殊字符要用[]括起来) -->
    ${header["User-Agent"] }

    <h3>获取全局初始化参数</h3>
    ${ initParam.password }

    <h3>获取Cookie中的值</h3>
    <!-- cookies.名称.value -->
    ${cookies.history.value }

    <h3>获取PageContext中的对象</h3>
    IP地址:${ pageContext.request.remoteAddr }
    <br/>
            工程路径:${ pageContext.request.contextPath }
  </body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值