E L(Expression Language) 目的:为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。
index 页面:目的,连接servelet类加载数据,然后转发页面。
<a href="servlet/JstlElServlet">测试EL表达式</a><br>
JstlElServlet 类:
package com.bjpowernode.jstl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 测试el表达式
* @author Administrator
*
*/
public class JstlElServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//普通字符串
request.setAttribute("hello", "Hello World");
//结构
Group group = new Group();
group.setName("动力节点603班");
User user = new User();
user.setUsername("张三");
user.setAge(23);
user.setGroup(group);
request.setAttribute("user", user);
//map
Map map = new HashMap();
map.put("k1", "v1");
map.put("k2", "v2");
request.setAttribute("map", map);
//字符串数组
String[] strArray = new String[]{"a", "b", "c"};
request.setAttribute("str_array", strArray);
//对象数组
User[] users = new User[10];
for (int i=0; i<users.length; i++) {
users[i] = new User();
users[i].setUsername("张三_" + i);
}
request.setAttribute("users", users);
//list
List userList = new ArrayList();
for (int i=0; i<10; i++) {
User usr = new User();
usr.setUsername("李四_" + i);
userList.add(usr);
}
request.setAttribute("userList", userList);
//empty
request.setAttribute("v2", "");
request.setAttribute("v3", new ArrayList());
request.setAttribute("v4", "12345");
request.setAttribute("v5", null);
request.getRequestDispatcher("/jstl_el.jsp").forward(request, response);
}
}
Group类:
package com.bjpowernode.jstl;
public class Group {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
user类:
package com.bjpowernode.jstl;
public class User {
private String username;
private int age;
private Group group;
public Group getGroup() {
return group;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setGroup(Group group) {
this.group = group;
}
}
jstl_el.jsp页面:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>测试EL表达式</h1>
<hr>
<li>普通字符串</li><br>
hello(jsp脚本):<%=request.getAttribute("hello") %><br>
hello(el表达式,语法:$和{}):${hello }<br>
hello(el表达式,el的内置对象pageScope,requestScope,sessionScope,applicationScope)<br>
如果不指定范围,它的搜索顺序为pageScope~applicationScope):${requestScope.hello }<br>
hello(el表达式,指定范围从session取得):${sessionScope.hello }<br>
<p>
<li>结构,采用.进行导航,或称为存取器</li><br>
姓名:${user.username }<br>
年龄:${user.age }<br>
所属组:${user.group.name }<br>
<p>
<li>map</li><br>
map.k1:${map.k1 }<br>
map.k2:${map.k2 }<br>
<p>
<li>字符串数组,采用[]下标</li><br>
strArray[1]:${str_array[1] }<br>
<p>
<li>对象数组,采用[]下标</li><br>
users[5].username:${users[5].username }<br>
<p>
<li>list,采用[]下标</li><br>
userList[6].username:${userList[6].username }<br>
<p>
<li>el表达式对运算符的支持</li><br>
1+1=${1+1 }<br>
10/5=${10/5 }<br>
div 5=${10 div 5 }<br>
% 3=${10 % 3 }<br>
mod 3 = ${ 10 mod 3 }<br>
<!--
==/eq
!=/ne
</lt
>/gt
&&/and
||/or
!/not
//div
%/mod
-->
<p>
<li>测试empty</li><br>
v1:${empty v1}<br>
v2:${empty v2}<br>
v3:${empty v3}<br>
v4:${empty v4}<br>
v5:${empty v5}<br>
</body>
</html>
感觉和session 、cookie 的作用差不多,但是EL表达式只是后台先前台页面传值。数据不能共享!