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

    一.概述

    EL的全称是Expression Language,EL定义了一系列的隐含对象和操作符,使开发人员能够很方便地访问页面的上下文,以及不同作用域内的对象,而无需在JSP页面嵌入Java代码。 EL表达式提供了在Java代码之外,访问和处理应用程序数据的功能,通常用于在某个作用域(page、request、session、application等)内取得属性值,或者做简单的运算和判断。EL表达式有如下特点。       (1) 自动类型转换 (2) 使用简单

    二.语法

    EL表达式以“${”开始,以“}”结尾,语法如下:       ${ EL expression }       EL提供“.”和“[]”两种运算符来存取数据。       (1) 点操作符,例如:${admin.loginName}       (2) []操作符,例如:${ admin["loginName"]}  当属性名中包含了特殊字符如“.”或“—”等的情况下,就不能使用点操作符来访问,这时只能使用“[]”操作符。   访问数组,如果有一个对象名为array的数组,那么我们可以根据索引值来访问其中的元素,如${array[0]}、${array[1]}等。

    jsp中提供了EL隐式对象可以是一下几类:

    类别

    对象标识符

    作   用

    JSP隐式对象

    pageContext

    提供对页面信息和JSP内置对象的访问

    作用域访问对象

    pageScope

    与页面作用域page中的属性相关联的Map类

    requestScope

    与请求作用域request中的属性相关联的Map类

    sessionScope

    与会话作用域session中的属性相关联的Map类

    applicationScope

    与应用程序作用域application中的属性相关联的Map类

    参数访问对象

    param

    按照参数名称访问单一请求值得Map对象

    paramValues

    按照参数名称访问数组请求值得Map对象

    请求头访问对象

    header

    与请求头名称相对应的字符串的Map集合

    headerValues

    与请求头名称相对应的字符串数组的Map集合

    cookie

    所有cookie组成的Map集合

    初始化参数对象

    initParam

    Web应用程序上下文初始化参数的Map集合

    三.代码演示

    AdminServler:

    
    @WebServlet("/AdminServlet")
    public class AdminServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            //单个值
            adminService adminService = new adminServiceImpl();
            int maxId = adminService.getMaxId();
            request.setAttribute("maxId",maxId);
            //对象
            HttpSession session = request.getSession();
            Admin admin = new Admin(12,"zhangsan","123");
            session.setAttribute("admin",admin);
            //集合 map
            HashMap countries = new HashMap();
            countries.put("CN","china");
            countries.put("UK","English");
            countries.put("USA","American");
            request.setAttribute("countries",countries);
            // 集合 list
            List cities = new ArrayList();
            cities.add(0,"beijing");
            cities.add("shanghai");
            request.setAttribute("cities",cities);
    
            request.getRequestDispatcher("EL.jsp").forward(request,response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

    jsp:

    
    <%@ 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>Insert title here</title>
    </head>
    <body>
    <%
        int maxId = (int)request.getAttribute("maxId");
        Admin admin = (Admin)session.getAttribute("admin");
    
    %>
    小脚本方式<%=maxId%>
    el表达式方式${requestScope.maxId}<br>
    对象取值:<<br>
    ${sessionScope.admin.id}<br>
    ${sessionScope.admin.loginName}<br>
    ${sessionScope.admin.loginPwd}<br>
    集合取值:<br>
    国家:${requestScope.countries.CN}<br>
    城市:${requestScope.cities[0]}----${requestScope.cities[1]}
    
    </body>
    </html>

  • 相关阅读:
    bash的shebang行
    Step By Step(Lua调用C函数)
    [Android问答] px、dp和sp,这些单位有什么区别?
    Android中Application类用法
    Android中SharedPreferences用法
    Android中SQLite用法
    SQLite应用实例
    Android中Parcelable接口用法
    iOS block 基本用法及代替代理
    Xcode插件
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767545.html
Copyright © 2011-2022 走看看