我认为EL类似于js,不过功能没那么强大、操作简单:
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="yuan" class="Pojo.Yuan"></jsp:useBean>
<jsp:setProperty property="r" name="yuan" value="2" />
<p>${yuan.a}</p>
<%
String[] arr1 = { "手动", "自动" };
String[] arr2 = { "c1", "c2" };
ArrayList<String[]> list = new ArrayList<>();
list.add(arr1);
list.add(arr2);
pageContext.setAttribute("arr1", arr1);
pageContext.setAttribute("arr2", arr2);
pageContext.setAttribute("list", list);
int a = 10;
String name = "某某";
pageContext.setAttribute("a", a);
pageContext.setAttribute("name", name);
String s1 = null;
String s2 = "";
pageContext.setAttribute("s1", s1);
pageContext.setAttribute("s2", s2);
ArrayList list1 = new ArrayList<>();
pageContext.setAttribute("list1", list1);
%>
<p>${arr1[0]}</p>
<p>${arr2[0]}</p>
<p>${list[1][1]}</p>
<p>${a>=10 and name eq "某某"}</p>
<p>${empty s1}</p>
<p>${empty s2}</p>
<p>${empty list}</p>
<p>${empty s1?"不合格":"合格" }</p>
</body>
</html>
EL主要用于取出作用域中的数据,简化代码格式:
在EL中可以使用+-*/还有逻辑运算,以及一些方法
具体方法可以借鉴这里:EL表达式完全攻略 (biancheng.net)
取出前一个页面传回的数据:
html代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="EL3.jsp" method="post">
<table border="1px">
<tr>
<td>姓名:</td>
<td><input type="text" name="textName" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="xb" value="男" />男 <input
type="radio" name="xb" value="女" />女</td>
</tr>
<tr>
<td>职业:</td>
<td><select name="xlk">
<option value="医生">医生</option>
<option value="教师">教师</option>
<option value="程序猿">程序猿</option>
</select></td>
</tr>
<tr>
<td>喜欢的图书:</td>
<td><input name="checkbox" type="checkbox" value=" 中国近代史 " />中国近代史
<input name="checkbox" type="checkbox" value=" 大国崛起 " />大国崛起
<input name="checkbox" type="checkbox" value=" 大唐诗圣 " />大唐诗圣
<input name="checkbox" type="checkbox" value=" 玛卡巴卡 " />玛卡巴卡</td>
</tr>
<tr>
<td colspan="2">
<div style="float: right;">
<input type="submit" /> <input type="reset" />
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
取数据代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<h3>姓名:${param.textName}</h3>
<h3>性别:${param.xb}</h3>
<h3>职业:${param.xlk}</h3>
<h3>喜欢的图书:${paramValues.checkbox[0]}${paramValues.checkbox[1]}${paramValues.checkbox[2]}${paramValues.checkbox[3]}</h3>
</body>
</html>