Java Web基础入门第二十七讲 JSP技术——EL表达式和JSTL标签快速入门
由于我们将使用Servlet+JSP+JavaBean的开发模式来开发一个用户的登录注册功能,所以我们得提前入门EL表达式和JSTL标签。
EL表达式
EL表达式用于获取数据,在JSP页面中可使用${标识符}
的形式,通知JSP引擎调用pageContext.findAttribute()方法,以标识符为关键字从各个域对象中获取对象。如果域对象中不存在标识符所对应的对象,则返回结果为“”(注意,不是null)。
例,使用EL表达式获取request、session、application域中的数据。
<%@ 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>el表达式的使用</title>
</head>
<body>
<%
String data = "abcd";
request.setAttribute("data", data);
%>
${data } <!-- pageContext.findAttribute("data") page request session application -->
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
EL表达式中也可以使用${customerBean.address}
的形式来访问JavaBean对象的属性。例如,有如下两个JavaBean,使用EL表达式获取Bean属性。
-
Person.java
package cn.liayun.domain; import java.util.Date; public class Person { //------------------Person类封装的私有属性--------------------------------------- private String name = "liayun"; //getClass() private int age; private Date birthday; private Address address; //------------------Person类的无参数构造方法--------------------------------------- public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name) { super(); this.name = name; } //------------------Person类对外提供的用于访问私有属性的public方法--------------------------------------- public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
-
Address.java
package cn.liayun.domain; public class Address { private String city; public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
若el.jsp页面的内容如下:
<%@page import="cn.liayun.domain.Person"%>
<%@ 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>el表达式的使用</title>
</head>
<body>
<%
Person p = new Person();
p.setName("李阿昀");
request.setAttribute("person", p);
%>
${person.name }
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
若el.jsp页面的内容如下:
<%@page import="cn.liayun.domain.Address"%>
<%@page import="cn.liayun.domain.Person"%>
<%@ 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>el表达式的使用</title>
</head>
<body>
<%
Person p = new Person();
Address a = new Address();
a.setCity("天门");
p.setAddress(a);
request.setAttribute("person", p);
%>
${person.address.city }
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
EL表达式也可轻松获取各种集合中的元素。
例一,使用EL表达式获取List集合中的元素。若el.jsp页面的内容如下:
<%@page import="cn.liayun.domain.Address"%>
<%@page import="cn.liayun.domain.Person"%>
<%@ page language="java" import="java.util.*" 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>el表达式的使用</title>
</head>
<body>
<%
List<Person> list = new ArrayList<Person>();
list.add(new Person("aaa"));
list.add(new Person("bbb"));
list.add(new Person("ccc"));
request.setAttribute("list", list);
%>
${list[1].name }
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
例二,使用EL表达式获取Map集合中的元素。若el.jsp页面的内容如下:
<%@page import="cn.liayun.domain.Address"%>
<%@page import="cn.liayun.domain.Person"%>
<%@ page language="java" import="java.util.*" 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>el表达式的使用</title>
</head>
<body>
<%
Map<String, Person> map = new HashMap<String, Person>();
map.put("aa", new Person("aaaaa"));
map.put("bb", new Person("bbbbb"));
map.put("cc", new Person("ccccc"));
map.put("dd", new Person("ddddd"));
map.put("111", new Person("eeeee"));
request.setAttribute("map111", map);
%>
${map111.aa.name }<br/>
${map111.cc.name }<br/>
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
现在想要取出new Person("eeeee")
元素,该向下面这样做吗?
${map111.111.name }
答案是不可以,这样做会报异常。而应该这样做:
${map111['111'].name }
结论:用EL表达式在取数据时,通常用.
号,.
号取不出来时,用[]
。
使用EL表达式得到当前Web应用的Web工程的名称
如果在JSP中要得到当前Web应用的Web工程的名称,只需这样做:
${pageContext.request.contextPath }
这样会输出/项目名称
。
在实际开发中的应用场景,例如在el.jsp页面中,需要通过点击一个链接跳转到首页index.jsp,我们是不宜这样写的:
<a href="/day09/index.jsp">点点</a>
原因是当前Web应用的Web工程的名称不能写死在程序里,要动态获取,发布的工程名称是什么获取的就是什么。所以,应该这样做:
<a href="${pageContext.request.contextPath }/index.jsp">点点</a>
JSTL标签
JSTL是SUN公司开发的一套标签库,使用JSTL可以在页面中实现一些简单的逻辑,从而替换页面中的脚本代码。在页面中使用JSTL标签需完成以下2个步骤:
- 导入jstl.jar和standerd.jar这两个JSTL的jar文件;
- 在JSP页面中使用
<%@ tablib url="..." prifix="..." %>
元素导入标签库。
温馨提示:standard.jar是一个JSP标准标签库,在1.0的版本中和jstl.jar一起使用,但在jstl-1.2.jar就不再需要了。即项目里只需引用jstl-1.2.jar,项目并不会报错,jstl标签也能正常输出。如:
案例一,使用jstl+el完成List集合的迭代。
<%@page import="cn.liayun.domain.Person"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>使用jstl+el完成集合迭代</title>
</head>
<body>
<%
List<Person> list = new ArrayList<Person>();
list.add(new Person("aaa"));
list.add(new Person("bbb"));
list.add(new Person("ccc"));
request.setAttribute("list", list);
%>
<c:forEach var="person" items="${list }">
${person.name }<br/>
</c:forEach>
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
案例二,使用jstl+el完成Map集合的迭代。
<%@page import="cn.liayun.domain.Person"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>使用jstl+el完成集合迭代</title>
</head>
<body>
<%
Map<String, Person> map = new HashMap<String, Person>();
map.put("aa", new Person("aaaaa"));
map.put("bb", new Person("bbbbb"));
map.put("cc", new Person("ccccc"));
map.put("dd", new Person("ddddd"));
map.put("111", new Person("eeeee"));
request.setAttribute("map", map);
%>
<c:forEach var="entry" items="${map }"> <!-- map.entrySet()→Set<Map.Entry> -->
${entry.key } : ${entry.value.name }<br/>
</c:forEach>
</body>
</html>
在Google Chrome浏览器上访问以上页面,运行效果如下:
EL表达式也可使用类似${1==1}
的形式进行简单的逻辑判断,再结合<c:if test="..."></c:if>
JSTL标签可以判断用户是否登录成功,代码如下:
<!-- 代表用户登录了 -->
<c:if test="${user!=null }">
欢迎您,${user.username }
</c:if>
<!-- 代表用户没有登录 -->
<c:if test="${user==null }">
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
</c:if>