EL表达式

目录

一.EL表达式

二.EL表达式显示数据

1.显示基本数据类型及String类型

2.显示引用类型

3.显示数组

4.显示集合类型

①显示ArrayList集合

②显示HashMap集合

三.EL表达式中的运算符

1.算术运算符

2.关系运算符

3.empty运算符


一.EL表达式

EL(Expression Language 表达式语言)定义了一系列隐含对象和操作符,无须Java代码即可方便地访问pageContext、request、session、application作用域中的对象

二.EL表达式显示数据

1.显示基本数据类型及String类型

以下是将int类型数据及String类型数据存入request作用域中的java代码,并将显示页面跳转到target.jsp:

package club.affengkuang.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("age",10);
		request.setAttribute("name","Tom");
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

以下是target.jsp中用于显示request作用域中内容的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>Insert title here</title>
	</head>
	<body>
		${age }
		<br/>
		${name }
	</body>
</html>

 显示结果如下:

2.显示引用类型

现创建一个Student引用类型,其中有三个属性,并将其封装好:

package club.affengkuang.vo;

public class Student {

	private String id;
	private String name;
	private String address;
	public Student(String id, String name, String address) {
		this.id = id;
		this.name = name;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getAddress() {
		return address;
	}
}

这时再将该类的对象放入request作用域中:

package club.affengkuang.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import club.affengkuang.vo.Student;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Student student = new Student("110","Tom","黑龙江省大庆市");
		request.setAttribute("student",student);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

 当在request中获取一个引用类型对象时,可以通过 对象+“.” 来获取该对象的属性值:

<%@ 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>
		${student.id }
		<br/>
		${student.name }
		<br/>
		${student.address }
	</body>
</html>

显示结果如下:

3.显示数组

创建一个数组放入request作用域中:

package club.affengkuang.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import club.affengkuang.vo.Student;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int []scores = {100,0,60,88};
		request.setAttribute("scores",scores);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

在request中获取数组元素时的格式为 $+{数组名[元素序号] }

<%@ 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>
		${scores[0] }
		<br/>
		${scores[2] }
		<br/>
		${scores[3] }
	</body>
</html>

 显示结果如下:

4.显示集合类型

①显示ArrayList集合

先创建一个ArrayList集合并放三个数据进去:

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<String> list = new ArrayList<String>();
		list.add("Tom");
		list.add("Jim");
		list.add("Kate");
		request.setAttribute("list",list);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

ArrayList集合的特点是有序性,所以第一种显示方法就是和显示数组的方式一样:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		${list[0] }
		<br/>
		${list[1] }
		<br/>
		${list[2] }
	</body>
</html>

结果如下:

 第二种显示方法是使用jstl标签库中的forEach标签,详情见博客: JSTL常用标签和函数

②显示HashMap集合

先创建一个HashMap集合并放三组key-value进去:

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim",0);
		map.put("Kate",60);
		request.setAttribute("scores",map);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

显示HashMap集合时格式为:${集合名+[key值] }:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		${scores['Tom'] }
		<br/>
		${scores['Jim'] }
		<br/>
		${scores['Kate'] }
	</body>
</html>

 显示结果如下:

当然,HashMap集合也可以使用jstl标签库中的forEach标签。

这里要注意,如果要显示HashSet集合,就只能使用jstl标签库中的forEach标签,因为HashSet集合的特点是无序性,所以不能使用显示数组的方式显示。

三.EL表达式中的运算符

1.算术运算符

功能取余
运算符+-*/或div%或mod

先将两个整型放入request中:

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("a",1);
		request.setAttribute("b",2);
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

 以下是在EL表达式中使用算术运算符:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		${a+b }
		<br/>
		${a/b }
		<br/>
		${a div b }
		<br/>
		${a mod b }
	</body>
</html>

显示结果如下:

2.关系运算符

关系运算符和java代码中一样,返回值为true或false:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		${a>b }
		<br/>
		${a!=b }
	</body>
</html>

  运行结果如下:

3.empty运算符

empty运算符用于判断对象是否为空,以下java代码中list集合中未添加任何元素,list2集合中添加一个空字符串,list3集合中添加null:

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<String> list2 = new ArrayList<String>();
		list2.add("");
		List<String> list3 = new ArrayList<String>();
		list3.add(null);
		
		request.setAttribute("list",new ArrayList<String>());
		request.setAttribute("list2",new ArrayList<String>());
		request.setAttribute("list3",new ArrayList<String>());
		
		request.getRequestDispatcher("target.jsp").forward(request, response);
	}
}

再使用empty表达式判断:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		${empty list }
		${not empty list }
		<br/>
		${empty list }
		${not empty list }
		<br/>
		${empty list }
		${not empty list }
	</body>
</html>

显示结果如下,说明这三个集合被empty判断都为空:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值