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" --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</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<Student> list=new ArrayList<Student>();
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.size();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 }
        <br/>
        ${true||false}  --%>
        <%
            String haha="";
        request.setAttribute("haha", haha);
        %>
           判断null:${haha==null }
          判断空字符: ${haha=="" }<br/>
         判空:${haha==null||haha=="" }
         另一种判空:
         ${empty haha }




</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/80938660