06 EL 表达式 1

EL表达式替代jsp表达式。因为开发jsp页面的时候遵守原则:在jsp页面中尽量少些甚至不写java代码。
EL表达式作用:向浏览器输出域对象中的变量或表达式计算的结果
基本语法: ${变量或表达式} 代替<%=变量或表达式%>

<body>
    <%
     String name = "eric";
     //把变量放入域对象中
     //pageContext.setAttribute("name",name);
     pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE);
      %>
      <%=name %>
      <br/>
      EL表达式: ${name }<br/>
      <%--
      1)EL从四个域中自动搜素
        ${name} 等价于:  <%=pageContext.findAttribute("name")%>
       --%>
       
       <%--
       2)EL从指定域中获取
            pageScope: page域
            requestScope: request域
            sessionScope: session域
            applicationScope: application域
        --%>
        指定域获取的EL: ${pageScope.name }
</body>
<body>
<%--
    1)普通的字符串
     --%>
     <%
        String email = "zhangsan@qq.com";
        //一定要把数据放入域中
        pageContext.setAttribute("email",email);
      %>
                普通字符串: ${email }
                <hr/>
      <%--
      2)普通的对象
      EL表达式的属性表示调用对象的getXX方法.例如  student.name 调用了Stduent对象的getName()方法  
       --%>
       <%
            Student student = new Student("eric","123456");
            pageContext.setAttribute("student",student);
        %>
        普通的对象: ${student}<br/>
       对象的属性: ${student.name } - ${student.password }
       
       <hr/>
       <%--
       3)数组或List集合
        --%>
        <%
            //数组
            Student[] stus = new Student[3];
            stus[0] = new Student("jacky","123456");
            stus[1] = new Student("rose","123456");
            stus[2] = new Student("lucy","123456");
            pageContext.setAttribute("stus",stus);
            //List
            List<Student> list = new ArrayList<Student>();
            list.add(new Student("eric","123456"));
            list.add(new Student("lily","123456"));
            list.add(new Student("maxwell","123456"));
            pageContext.setAttribute("list",list);
         %>
         数组: <br/>
         ${stus[0].name } - ${stus[0].password }<br/>
         ${stus[1].name } - ${stus[1].password }<br/>
         ${stus[2].name } - ${stus[2].password }<br/>
    List集合<br/>
        ${list[0].name } - ${list[0].password }<br/>
        ${list[1].name } - ${list[1].password }<br/>
        ${list[2].name } - ${list[2].password }<br/> 
         <hr/>
         <%--
        4) Map集合     
          --%>
          <%
            Map<String,Student> map = new HashMap<String,Student>();
            map.put("001",new Student("eric","123456"));
            map.put("002",new Student("jacky","123456"));
            map.put("003",new Student("rose","123456"));
            pageContext.setAttribute("map",map);
           %>
           Map集合: <br/>
           ${map['001'].name } - ${map['001'].password }<br/>
           ${map['002'].name } - ${map['002'].password }<br/>
           ${map['003'].name } - ${map['003'].password }<br/>
</body>
<body>
    <%--
    1)算术表达式: + - * /
    
     --%>
     <%
        int a = 10;
        int b = 5;
        pageContext.setAttribute("a",a);
        pageContext.setAttribute("b",b);
      %>
      ${a+b }<br/>
      ${a-b }<br/>
      ${a*b }<br/>
      ${a/b }
      <hr/>
      
      <%--
      2)比较表达式: >  < >= <= ==
       --%>
       ${a>b }<br/>
       ${a==b }<br/>
       
       <hr/>
      <%--
      3)逻辑表达式:  && (与)   || (或)   !(非)
       --%>
       ${true&&true }<br/>
       ${true||false }<br/>
       ${!true }<br/>
       ${!(a>b) }
       
       <hr/>
       <%--
       4)判空表达式      empty
            null:  ==null
            空字符串: ==""
        --%>
        <%
            //String name1 = null;
            String name1 = "";
            pageContext.setAttribute("name1",name1);
         %>
         null: ${name1==null }<br/>
                        空字符串: ${name1=="" }<br/>
         null或空字符串: ${name1==null || name1=="" }
         <br/>
          null或空字符串:${empty name1}              
  </body>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,823评论 18 399
  • 一、EL表达式简介 1.EL全名为Expression Language。主要作用: 获取数据:EL表达式主要用于...
    yjaal阅读 4,064评论 2 28
  • 一、JSP基础 1.1什么是JSP JSP(Java ServerPage)是Java服务器端动态页面技术。是su...
    晨星资源阅读 1,200评论 0 6
  • 1.学习内容 JSP技术入门和常用指令 JSP的内置对象&标签介绍 EL表达式&EL的内置对象 2.JSP技术入门...
    WendyVIV阅读 2,197评论 1 18
  • 【都市】劫缘(2) 文 / 伊米crystal 目录 上一章 下一章一缕光线柔和的穿过窗纱的缝隙照在孟辰的脸上,舒...
    伊米crystal阅读 512评论 0 2