jsp学习笔记之EL让取值更加方便(七)

七. EL

Expressopm Language,可以代替JSP页面中的JAVA代码

a. 传统JSP

用java代码显示数据的弊端:类型转换、需要处理null、代码参杂

b. el

jsp学习笔记之EL让取值更加方便(七)_第1张图片

  1. 表达方式:
    ${域对象 . 域对象中的属性 . 属性的属性}

    ${requestScope.student.sno}

    一般用点操作符,但不能有特殊字符(. 、-)
  2. [""]操作符就可以,比如["my-name"] (单双引号都可以)
  3. 可以获取变量值
    String name = "student";
    ${requestScope[name]}<br>
  1. 还可以获取数组元素
    ${requestScope.list[0]}

  2. 可以获取map

        Map<String,Object> map = new HashMap<>();
        map.put("cn","中国");
        map.put("us","美国");
        req.setAttribute("map",map);
    ${requestScope.map.cn}<br>
    ${requestScope.map["us"]}<br>

c. el的运算符

jsp学习笔记之EL让取值更加方便(七)_第2张图片

c. el的empty运算符

判断有值为false,不存在或者null则为true
在这里插入图片描述

c. el表达式的隐式对象

不需要new就能使用的对象(自带的对象)

  1. 作用域访问对象(EL对象)
    pageScope、requestScope、sessionScope、appl icationScope
    如果不指定域对象,则默认会根据从小到大的顺序依次取值

  2. 参数访问对象:获取表单数据(超链接中的值、地址栏的值)
    普通:request.getParameter()、request.getParameterValues()
    el: p a r a m s 、 {params}、 params{paramsValues}

  3. JSP隐式对象
    在el中可以通过pageContext获取其他jsp隐式对象
    例如:${pageContext.session} \ ${pageContext.request.serverPort}端口号

示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表单</title>
</head>
<body>
    <form action="el.jsp">
        用户名:<input name= "uname" type= "text"/ ><br/>
        兴趣: <br/>
        足球<input name= "hobbies" value= "football" type= "checkbox"/>、
        篮球<input name= "hobbies" value= "basketball" type= "checkbox"/><br/>
        乒乓球<input name= "hobbies" value= "pingpang" type= "checkbox"/><br/>
        <input type="submit" value="添加">
    </form>
</body>
</html>
<%@ page import="three_tier.entity.Student" %><%--
  Created by IntelliJ IDEA.
  User: qsX
  Date: 2020/5/17
  Time: 11:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el</title>
</head>
<body>
<%--<%
    Student student = (Student) request.getAttribute("student");
    out.print("-0
"
); out.print(student.getSno() + "---1
"
); out.print(student.getSname() + "---2
"
); out.print(student.getSaddress() + "---3
"
); out.print(student.getSage() + "---4
"
); %>--%> <br> 点操作符<br> ${requestScope.student.sno}<br> ${requestScope.student.sname}<br> [""]操作符<br> ${requestScope.student["saddress"]}<br> ${requestScope.student.sage}<br> <%-- String name = "student"; ${requestScope[name]}<br> --%> ----hobbies----<br> ${requestScope.hobbies[0]}/ ${requestScope.hobbies[1]}/ ${requestScope.hobbies[2]}<br> ----map----<br> ${requestScope.map.cn}<br> ${requestScope.map["us"]}<br> ----关系运算----<br> ${3>2} 、 ${3 gt 2}<br> ${3>2 || 3<2} 、${3>2 or 3<2} ----关系运算(有值为false)----<br> ${empty requestScope["student"]}<br> ----参数对象----<br> ${param.uname}/ ${paramValues.hobbies[0]}/ ${paramValues.hobbies[1]}/ ${paramValues.hobbies[2]}/ </body> </html>
package demo;

import three_tier.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("ELInitServlet")
public class ELInitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = new Student();
        student.setSno(1);
        student.setSname("zs");
        //student.setSaddress("zs");
        req.setAttribute("student", student);

        String[] hobbies = new String[] {"a","b","c"};
        req.setAttribute("hobbies",hobbies);

        Map<String,Object> map = new HashMap<>();
        map.put("cn","zg");
        map.put("us","mg");
        req.setAttribute("map",map);

        req.getRequestDispatcher("/demo/el/el.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

你可能感兴趣的:(笔记,jsp,jsp)