JSP EL表达式学习笔记

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="hah.*"
    errorPage="common/zz.jsp"
    %>
 <%-- errorPage="common/zz.jsp" --%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<% String name1="name1";
int name2=111;
int[] a=new int[3];
//int xx=10/0;//错误
//out.write(xx);
a[1]=3;
Student student=new Student("ss",22);
request.setAttribute("name1", name1);
session.setAttribute("name2", name2);
request.setAttribute("intx", a);
session.setAttribute("student",student); 


List list=new ArrayList();
list.add(new Student("11",111));
list.add(new Student("22",222));
list.add(new Student("33",333));
request.setAttribute("listx",list);

Map<String,Student> map=new HashMap<String,Student>();
map.put("1", new Student("11",111));
map.put("2", new Student("22",222));
map.put("2", new Student("33",333));
pageContext.setAttribute("mapx",map);
%>
<%--  注意 <%@ pageisELIgnored="true" %> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言。 --%>
 <%=pageContext.findAttribute("name2")%> 
  <%-- 等效于${name2 } --%>
   <%--
        ${name } 等价于
            <%=pageContext.findAttribute("name")%>
    --%>
      EL表达式:
      ${name1 }
      <%--
         从指定的域中获取数据 否则它是按照小到大的顺序查找域对象的 
      pageScoep / requestScope / sessionScope / applicationScope
        --%>
      ${sessionScope.name2 }
      ${intx[1] }
      ${student.name } - ${student.age }
      ${student["name"] } -${student["age"] } <br/>
      <%--  (点相对于调用getXX()方法)
          <%=((Student)pageContext.findAttribute("student")).getName()%> --%>
      ${listx[2]["name"] } - ${listx[2].age }
      <br/>

      <%
        for(int i=0;i
             <%=list.get(i).getName() +" | "+list.get(i).getAge()%>

      <%
        }
          %>
          <br/>
          
          ${listx[1]["name"] }---${listx[1].age }
           <%--
       listx[0]等价于       (中括号相对于调用get(参数)方法)
            ((List)pageContext.findAttribute("list")).get(0)
        --%>
        <br/>
        ${mapx['1']["name"] }
        <br/>
<%--       ${
     3>=2 }
        ${
     10!=3 }
        
${ true||false} --%>
<% String haha=""; request.setAttribute("haha", haha); %> 判断null:${haha==null } 判断空字符: ${haha=="" }<br/> 判空:${haha==null||haha=="" } 另一种判空: ${empty haha } body> html>

你可能感兴趣的:(JAVA,Web基础)