EL 表达式 基础

来源:互联网 发布:北大生命科学院知乎 编辑:程序博客网 时间:2024/06/05 08:57

              简介  :

                      EL表达式 主要是替代 JSP表达式 , 由sun公司设计出来 。

                      因为在开发JSP页面的时候要尽量遵守 少写 或者 不写 Java代码 。两种语法的混杂 可读性 和 维护性能都将会降低 。

                     注意  : 只能替代表达式 。不能替代脚本块儿。

           

             作用  :

                        向浏览器输出一个变量或者是一个表达式计算的结果 。

            

            基本语法 :

                        ${变量 ||  表达式}    替代 <%=  %>

          

            注意 :

                    EL表达式是输出在域对象中的表达式的和变量(EL是自动搜索) 。随便是四个域对象都可以 。

                    等价于 : <%=pageContext.findAttribute("name")% >

           

           从指定域中获取

                     ${resquestScope.name} 

                     pageScope        :

                     resquestScope    :

                     sessionScope     :

                     applicationScope :

             


          EL 可以获取的数据

                    1 ) 字符串

                    2 ) 普通的对象

                    3 ) 获取数组或者是集合

                    4 ) Map 集合   

         代码如下 :

              

<%@ page language="java" import="java.util.*,club.dohacker.entity.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>        <title>My JSP 'demo1.jsp' starting page</title>      </head>    <body>    <%       String mail = "1234556@qq.com";        //一定要将数据放置在域中使用       pageContext.setAttribute("mail",mail,pageContext.REQUEST_SCOPE);     %>     ${requestScope.mail}           <%-- EL 获取对象 --%>     <%        Student stu = new Student("chun","123456");        pageContext.setAttribute("stu",stu);       %>      <br/>      <%-- 会自动调用toString 方法哦 --%>      ${stu}      <br/>      <%--会自动调用getName 方法哦  --%>      ${stu.name}      <br/>       <%--会自动调用getPassword 方法哦  --%>      ${stu.getPassword()}                                <%--使用 EL获取数组里面数据 --%>      <%         Student [] stus = new Student[3];         stus[0] =  new Student("chun","123456");         stus[1] =  new Student("chun","123456");         stus[2] =  new Student("chun","123456");         pageContext.setAttribute("stus", stus);       %>       <br/>       ${stus[0].name}----${stus[0].password}<br/>       ${stus[1].name}----${stus[1].password }<br/>       ${stus[2].name}----${stus[2].password }<br/>                            <%--使用EL来获取list里面的数据 --%>       <%          List<Student > list = new ArrayList<Student>();          list.add(new Student("chun","111111"));          list. add(new Student("chun","111111"));          list.add(new Student("chun","111111"));          pageContext.setAttribute("list",list);        %>        <br/>       ${list[0].name}----${list[0].password}<br/>       ${list[1].name}----${list[1].password }<br/>       ${list[2].name}----${list[2].password }<br/>                          <%--使用EL来获取Map里面的数据                         类似于 PHP中的关联数组     --%>     <%        Map<String,Student> map = new HashMap<String,Student>();        map.put("c1",new Student("chun","222222"));        map.put("c2",new Student("chun","222222"));        map.put("c3",new Student("chun","222222"));        pageContext.setAttribute("map", map);      %>      <br/>         ${map['c1'].name}-----${map['c1'].password}<br/>      ${map['c2'].name}-----${map['c2'].password}<br/>      ${map['c3'].name}-----${map['c3'].password}<br/>  </body></html>


               EL 执行表达式 

                     算数表达式 :

                     比较表达式 :

                     逻辑表达式 :

                     三目表达式 :

                     判空表达式 :

代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>EL执行表达式</title> </head>    <body>     int a = 10;     int b = 20;     <br/>     <br/>     <br/>     1 ) 算数表达式 :       <%          int a = 10;         int b = 20;         //一定要将变量放置在域对象当中         pageContext.setAttribute("a", a);         pageContext.setAttribute("b", b);       %>       <br/>       a + b = ${a+b}<br/>       a - b = ${a-b}<br/>       a * b = ${a*b}<br/>       a / b = ${a/b}<br/>    <%-- 会转换成小数 --%>       <br/>       <br/>       <br/>     2 ) 比较     <br/>     a>b ? : ${a>b}<br/>     a<b ? : ${a<b}<br/>     a==b ?  :${a==b}<br/>       <br/>       <br/>       <br/>     3 )逻辑运算 :     <br/>     ${true && false}<br/>     ${false || true}<br/>     ${a>b ||  a<b }<br/>            <br/>       <br/>       <br/>     4 )判空表达式 :  判断是否为null  and  判断是否是空字符串     <%       String  str = null;       pageContext.setAttribute("str",str);      %>      <br/>      str== null || str == ""? : ${str==null || str==""}<br/>                          使用empty来判断空串和null的情况 : 与str == null || str =="" 等价       <br/>      empty ? :${empty str};               </body></html>


         EL 内置对象 :

                     pageContext

                     pageScope

                     requestScope

                     responseScope

                     applicationScope


                    //获取表单的参数

                     param                        ------>getParameter()

                     paramValues              ------->getParameterValues()

                  

                   //获取请求头

                     header                     ------>getHeader();

                     headerValues           ------>

                 

                  //cookie

                    cookie

                 //获取全局参数 :

                     initParam       



代码如下 :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>        <title>EL内置对象</title>      </head>    <body>      1 ) pageContext ---> 等价于JSP的内置对象,可以直接调用内部方法      <br/> 如下获取上下文路径 :      ${pageContext.request.contextPath}<br/>                     <hr/>      2 ) 从指定的域中获取数据  (略过)           pageScope  requestScope responseScope  applicationScope       <br/>                      <hr/>      3 ) 获取请求中的参数 : param        <br/>是以map的方式来发送数据;       <br/>        ${param['name']}        <br/>获取同名多值的情况 ,等价于getParamterValues();<br/>        ${paramValues['name'][0] }<br/>        ${paramValues['name'][1]}       <br/>                        <hr/>      4 ) 获取请求头的信息:header 内置对象  ----> 等价于request.getHeader()<br/>            ${header['host']}<br/>      ${headerValues['host'][0]}<br/>                <hr/>         5 )获取cookie , Servlet 返回的是一个数组 , 而EL返回的是一个MAP        <br/>       ${cookie['JSESSIONID'].name } --- ${cookie['JSESSIONID'].value  }             <hr/>         6) 获取全局参数  : 在web.xml中配置     -----》 代替ServletContext.getInitParameter();       <br/>      ${initParam['A']}  </body></html>


原创粉丝点击