zoukankan      html  css  js  c++  java
  • EL表达式Expression Language

    表达式语言Expression Language
    目的:简化jsp代码

    EL内置对象

    1、pageContext
    2、pageScope
    3、requestScope
    4、sessionScope
    5、applicationScpoe
    6、param
    7、paramValues
    8、header
    9、headerValues
    10、cookie
    11、initParam

    示例1

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>EL表达式</title>
    </head>
    <body>
    
    <%
    pageContext.setAttribute("name", "Allen");
    request.setAttribute("age",24);
    session.setAttribute("sex","女");
    application.setAttribute("cellphone",131);
    %>
    
    <h3>姓名:${name }</h3>
    <h3>年龄:${age }</h3>
    <h3>性别:${sex }</h3>
    <h3>电话:${cellphone }</h3>
    </body>
    </html>
    

      

    //寻找值的顺序
    //page>request>session>application
    

    当pageContext、request、session、application都设计键为name是,值value是谁的呢?  

    验证寻找顺序:

    <%
    pageContext.setAttribute("name", "Allen");
    request.setAttribute("name",24);
    session.setAttribute("name","女");
    application.setAttribute("name",131);
    %>
    
    <h3>姓名:${name }</h3>
    

      

     【EL表达式接受请求参数】

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>EL表达式</title>
    </head>
    <body>
    <%-- post提交 --%>
    <form method="post" action="target.jsp">
    <input type="text" name="userName" />
    <input type="submit" value="提交到target.jsp">
    </form>
    <%-- get提交 --%>
    <a href="target.jsp?age=24">get提交</a>
    <%-- post提交多个参数 --%>
    <form method="post" action="target.jsp">
    <input type="checkbox" name="instr" value="登山" />登山
    <input type="checkbox" name="instr" value="滑雪" />滑雪
    <input type="checkbox" name="instr" value="骑行" />骑行
    <input type="checkbox" name="instr" value="游泳" />游泳
    <input type="submit" value="提交到target.jsp">
    </form>
    
    </body>
    </html>
    

      

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>target</title>
    </head>
    <body>
    <% request.setCharacterEncoding("utf-8"); %>
    
    <h3>姓名:${param.userName }</h3>
    
    <h3>年龄:${param.age }</h3>
    
    <h3>兴趣1:${paramValues.instr[0] }</h3>
    <h3>兴趣2:${paramValues.instr[1] }</h3>
    <h3>兴趣3:${paramValues.instr[2] }</h3>
    <h3>兴趣4:${paramValues.instr[3] }</h3>
    </body>
    </html>
    

      【对象操作】简单java类不用page引入,使用JavaBean

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>EL</title>
    </head>
    <body>
    
    <jsp:useBean id="user" scope="page" class="com.zhiqi.model.User"></jsp:useBean>
    
    <jsp:setProperty property="id" name="user" value="10010"></jsp:setProperty>
    <jsp:setProperty property="name" name="user" value="王晓丽"></jsp:setProperty>
    <jsp:setProperty property="sex" name="user" value="女"></jsp:setProperty>
    <jsp:setProperty property="age" name="user" value="24"></jsp:setProperty>
    
    <jsp:getProperty name="user" property="id"></jsp:getProperty><br>
    <jsp:getProperty name="user" property="name"></jsp:getProperty><br>
    <jsp:getProperty name="user" property="sex"></jsp:getProperty><br>
    <jsp:getProperty name="user" property="age"></jsp:getProperty><br>
    
    <%--EL 与JavaBean比较 --%>
    
    ${user.id }<br><%--EL对象操作 --%>
    ${user.name }<br>
    ${user.sex }<br>
    ${user.age }<br>
    
    </body>
    </html>

     

    【运算符操作】

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>EL运算符操作</title>
    </head>
    <body>
    
    <% request.setAttribute("x", 10); %>
    <% request.setAttribute("y", 4); %>
    
    <%--算数四则原酸--%>
    ${x+y }<br>
    ${x-y }<br>
    ${x*y }<br>
    ${x/y }<br>
    ${x%y }<br>
    
    <% request.setAttribute("z", true); %>
    <% request.setAttribute("fal", false); %>
    
    <%--布尔运算 --%>
    ${z }<br>
    ${!z }<br>
    ${z && fal }<br>
    ${z || fal }<br>
    
    <%--空判断 --%>
    空判断:${empty z }<br>
    
    <% request.setAttribute("z", ""); %>
    空判断:${empty z }<br>
    
    <%--三目运算 --%>
    <h4>x与y的大小:${x>y? x:y }</h4>
    
    </body>
    </html>
    

      

    【EL操作List】

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
        <%@page import="java.util.*" %>
    <!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>EL操作List</title>
    </head>
    <body>
    <% 
    List l=new LinkedList();
    l.add(0,"北京");
    l.add(1,"上海");
    l.add(2,"广州");
    request.setAttribute("l",l);
    %>
    
    ${l[0] }<br>
    ${l[1] }<br>
    ${l[2] }<br>
    </body>
    </html>
    

      

    【补充】

    转义的使用-数组的使用-禁用EL的方法

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>EL2</title>
    </head>
    <body>
    
    <% request.setAttribute("userName","Anner"); %>
    
    <p>转义如何使用</p>
    ${10+2 }<br>
    ${10+2 }<br>
    
    ${requestScope.userName }<br>
    ${requestScope.userName }<br>
    
    <p>数组的使用</p>
    <%
    String [] str={"上海","南京","重庆","西安"};
    request.setAttribute("str",str);
    %>
    ${str[0] }<br>
    ${str['1'] }<br>
    ${str["2"] }<br>
    ${str[3] }<br>
    
    
    
    <p>设定页面不适用EL,当使用其他与EL冲突的技术,可以这样做!</p>
    <p>方法1:<%@ page isELIgnored="false" %> page指令设置为true</p>
    <p>方法2:xml配置</p>
    
    </body>
    </html>
    

      

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app>
    	<jsp-config>  
        	<jsp-property-group>  
            	<url-pattern>*.jsp</url-pattern>  
            	<el-ignored>true</el-ignored>  
        	</jsp-property-group>  
    	</jsp-config> 
    
    </web-app>
    

      【】

  • 相关阅读:
    Lua中的closure、泛型for
    Lua多重继承
    (转)C++ new详解
    C++重载操作符学习
    Lua中使用继承来组装新的环境
    DOS:变量嵌套和命令嵌套
    C++中成员的私有性
    ManualResetEvent 类
    在IIS中部署和注册WCF服务
    ArcGIS Server 10 地图缓存新特性
  • 原文地址:https://www.cnblogs.com/void-m/p/6193007.html
Copyright © 2011-2022 走看看