06 EL 表达式 1

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


    <%
     String name = "eric";
     //把变量放入域对象中
     //pageContext.setAttribute("name",name);
     pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE);
      %>
      <%=name %>
      
EL表达式: ${name }
<%-- 1)EL从四个域中自动搜素 ${name} 等价于: <%=pageContext.findAttribute("name")%> --%> <%-- 2)EL从指定域中获取 pageScope: page域 requestScope: request域 sessionScope: session域 applicationScope: application域 --%> 指定域获取的EL: ${pageScope.name }

<%--
    1)普通的字符串
     --%>
     <%
        String email = "[email protected]";
        //一定要把数据放入域中
        pageContext.setAttribute("email",email);
      %>
                普通字符串: ${email }
                
<%-- 2)普通的对象 EL表达式的属性表示调用对象的getXX方法.例如 student.name 调用了Stduent对象的getName()方法 --%> <% Student student = new Student("eric","123456"); pageContext.setAttribute("student",student); %> 普通的对象: ${student}
对象的属性: ${student.name } - ${student.password }
<%-- 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 list = new ArrayList(); list.add(new Student("eric","123456")); list.add(new Student("lily","123456")); list.add(new Student("maxwell","123456")); pageContext.setAttribute("list",list); %> 数组:
${stus[0].name } - ${stus[0].password }
${stus[1].name } - ${stus[1].password }
${stus[2].name } - ${stus[2].password }
List集合
${list[0].name } - ${list[0].password }
${list[1].name } - ${list[1].password }
${list[2].name } - ${list[2].password }

<%-- 4) Map集合 --%> <% Map map = new HashMap(); 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集合:
${map['001'].name } - ${map['001'].password }
${map['002'].name } - ${map['002'].password }
${map['003'].name } - ${map['003'].password }

    <%--
    1)算术表达式: + - * /
    
     --%>
     <%
        int a = 10;
        int b = 5;
        pageContext.setAttribute("a",a);
        pageContext.setAttribute("b",b);
      %>
      ${a+b }
${a-b }
${a*b }
${a/b }
<%-- 2)比较表达式: > < >= <= == --%> ${a>b }
${a==b }

<%-- 3)逻辑表达式: && (与) || (或) !(非) --%> ${true&&true }
${true||false }
${!true }
${!(a>b) }
<%-- 4)判空表达式 empty null: ==null 空字符串: =="" --%> <% //String name1 = null; String name1 = ""; pageContext.setAttribute("name1",name1); %> null: ${name1==null }
空字符串: ${name1=="" }
null或空字符串: ${name1==null || name1=="" }
null或空字符串:${empty name1}

你可能感兴趣的:(06 EL 表达式 1)