EL表达式的详细用法

     EL与JSTL作为JAVA WEB开发者来说,熟练的运用它们,在View模式下效率更高.因为它们的存在,基本上在视图页是不会出现 JAVA 代码的!它们的强大之处将在下面一一列举!至于概念性的就不多说了,就以例子来介绍!


    target.jsp


<%@ page language="java" contentType="text/html; charset=GB2312" import="cn.itcast.el.*,


java.util.*"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'target.jsp.jsp' starting page</title>
  </head>
 
  <body>
   <%--
    Object obj = session.getAttribute("person");
    if(obj != null){
     Person p = (Person)obj;
   %>
    name: <%= p.getName() %><br>
    email: <%= p.getEmail() %><br>
    password: <%= p.getPassword() %>
    <%
     }
    --%>
   
    1. <br>
    name: ${sessionScope.person.name } -- ${sessionScope["person"].name }<br>
    email: ${sessionScope.person.email }<br>
    password: ${sessionScope.person.password }<br><br>
   
    2.
    <%
     //pageContext.setAttribute("username", "Bob");
     
     application.setAttribute("username", "Bob");
    %>
    <br>依次从 page request session application 中寻找指定的属性值<br>
    username: ${username } -- (原理: <%= pageContext.findAttribute("username") %>)
    <br><br>
   
    3.<br>
    <%
     String age = request.getParameter("age");
     out.println("age + 12(error): " + (age + 12) + ", (right): " + (Integer.parseInt(age) +


12));
    %>
    <br>
    age + 12: ${param.age + 12}
    <br><br>
   
    4. <br>
    <%
     Person p = new Person();
     p.setEmail("email123");
     p.setName("Tom123");
     p.setPassword("123456123");
     request.setAttribute("person123", p);
    %>
    requestScope.person.email: ((强转成 javaBean 类型)request.getAttribute("person123")).getEmail


();<br>
    email: ${requestScope.person123.email }
    <br><br>
   
    5. <br>
    <%
     int i = 0;
    %>
    <%= i %>
    name: ${param.name} --- <%= request.getParameter("name") %><br>
    age: ${param.age} --- <%= request.getParameter("age") %><br>
    interesing: ${paramValues.interesing[0] } &nbsp;&nbsp; ${paramValues.interesing[1] }
                ---
                <%= request.getParameterValues("interesing")[0] %>&nbsp;&nbsp;
                <%= request.getParameterValues("interesing")[1] %>
    <br><br>
               
    6. <br>
    initParam: ${initParam.initParam } --- <%= application.getInitParameter("initParam") %>
    <br><br>
   
    7. <br>
    method: ${pageContext.request.method } --- <%= ((HttpServletRequest)pageContext.getRequest


()).getMethod() %><br>
    localAddr: ${pageContext.request.localAddr }<br> 
    remoteHost: ${pageContext.request.remoteHost }<br>
    contentType: ${pageContext.response.contentType }<br>
    <br><br>
   
    8. <br>
    <%
     request.setAttribute("number", 100);
    %>
    number + 12: ${requestScope.number + 12 } (不是:  ${requestScope.number} + 12)
    <br><br>
   
    9. <br>
    number > 12: ${requestScope.number > 12 }
    <br><br>
   
    10. <br>
    <%
     List list = new ArrayList();
     request.setAttribute("list", list);
     request.setAttribute("flag", null);
    %>
    empty list: ${empty requestScope.list }<br>
    empty flag: ${empty requestScope.flag }<br>
    <%
     request.setAttribute("flag", "flagValue");
     list.add("^^");
    %>
    empty flag: ${empty requestScope.flag }<br>
    empty list: ${empty requestScope.list }<br>
         和 10 比         :  ${requestScope.number > 10 ? " > 10" : " < 10" }
  </body>
</html>


    

你可能感兴趣的:(EL表达式的详细用法)