1 JSP现有的问题
还不够简单(展示数据仍需要大量的java代码)。
复杂页面开发更为困难。
对前端人员不友好(java代码和html标签混用)。
2 EL表达式
作用:EL(Expression Language)用于从作用域中获取数据,并输出到页面,无需任何脚本,替换原有输出脚本。
1.使用EL获取作用域中的数据。
- 获取基本数据类型+字符串类型数据:
语法:${作用域标识.命名属性名}
如图所示:
演示得到代码如下:
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%-- EL(Expression Language)表达式语言(EL表达式)--%>
<%-- 作用:用于获取作用域中的数据于隐含对象--%>
<% pageContext.setAttribute("name", "pageContext"); // 向pageContext作用域添加数据
request.setAttribute("name","request"); // 向request作用域添加数据
session.setAttribute("name","session"); // 向session作用域添加数据
application.setAttribute("name","application"); // 向application作用域添加数据
%>
${pageScope.name} <%--从pageContext作用域中获取名称为name的属性--%>
${requestScope.name} <%--从request作用域中获取名称为name的属性--%>
${sessionScope.name} <%--从session作用域中获取名称为name的属性--%>
${applicationScope.name} <%--从application作用域中获取名称为name的属性--%>
<br/>
</body>
</html>
如图所示:
简写形式:${命名属性名} 从pageContext到application作用域中依次查找该命名属性。
例如:
${name}
- 获取对象类型数据的属性值
语法:${作用域标识.命名属性名.属性名}
演示的代码如下:
<%@ page import="com.txw.entity.User" %>
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%
User u = new User(1,"Adair","123456");
request.setAttribute("user",u);
%>
${user.username} <%-- 从作用域中获取user对象并自动调用getUsername方法获取属性值--%>
${user.password} <%--从作用域中获取user对象并自动调用getPassword方法获取属性值--%>
</body>
</html>
如图所示:
编写User的代码如下:
package com.txw.entity;
import java.io.Serializable;
import java.util.Objects;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(id, user.id) && Objects.equals(username, user.username) && Objects.equals(password, user.password);
}
@Override
public int hashCode() {
return Objects.hash(id, username, password);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
如图所示:
注意:对象的属性必须有其对应的get方法,否则无法获取。
注意:一旦写错属性名,访问会出现异常如下:
javax.el.PropertyNotFoundException: Property [id] not found on type [com.txw.entity.User]
2.El表达式的运算符,如图所示:
3 El表达式中的内置对象
概念:在el表达式中可以直接使用,无需手动创建的对象。
1.pageContext[重点]
作用:可以获取jsp中的8个内置对象。
演示的代码如下:
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
${pageContext.request }<br/>
${pageContext.response }<br/>
${pageContext.session }<br/>
${pageContext.servletContext }<br/>
${pageContext.page }<br/>
${pageContext.out }<br/>
${pageContext.exception }<br/>
${pageContext.servletConfig }
<%-- jsp隐含对象 request response out pageContext config exception ....--%>
<%= request.getContextPath() %>
<%-- 使用el获取项目名称--%>
${pageContext.request.contextPath}
</body>
</html>
如图所示:
典型应用:
// 获取当前应用的项目名[重点]
jsp: request.getContextPath();
el表达式: ${pageContext.request.contextPath}
2.param
作用:用来在jsp页面获取请求中携带的数据(等同于servlet中的getPrameter() 收参)
语法如下:
${param.参数名}
演示:获取请求中的参数
地址栏如下:
http://localhost:8080/test2.jsp?nane=Adair
如图所示:
在test.jsp中获取参数:
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%--判断对象是否为null --%>
${empty qweqweqwe}
${qweqweqwe==null}<br/>
<%-- 获取请求参数 --%>
<%= request.getParameter("name") %>
${param.name}
</body>
</html>
如图所示:
4 JSTL(Java Server Pages Tag Library)
作用:jsp标准标签库,与el表达式配合在页面上输出数据。
JSTL标签库使用步骤:
- 导入jar包
将 jstl-1.2.jar 复制到WEB-INF/lib目录里并导入到项目中。
**导入方式与导入mysql-connector-javaxx.jar一致。
如图所示:
2. 在页面导入jstl标签库 (类似于在Java中导包)。
<%@ taglib uri="jstl标签库标识" prefix="前缀" %>
演示的代码如下: 导入jstl的core核心标签库。
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
</body>
</html>
如图所示:
3. 在页面使用jstl标签库。
<前缀:标签名 属性="属性值"></前缀:标签名>
演示的代码如下:使用core标签库。
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<c:if test="">
</c:if>
</body>
</html>
如图所示:
1.if标签
作用:根据条件的真假决定代码是否执行。
语法如下:
<c:if test="条件">
条件为true执行的代码
</c:if>
演示的代码如下:如果Person性别为男,显示猛男,如果为女,显示仙女。
<%@ page import="com.txw.entity.Person" %>
<%@ page import="java.util.Date" %>
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%--获取request作用域命名属性对象中的sex属性,验证性别--%>
<c:if test="${p.sex=='男'}">
<h1>猛男</h1>
</c:if>
<c:if test="${p.sex=='女'}">
<h1>猛女</h1>
</c:if>
</body>
</html>
注意:该标签库中没有if else标签。
2.choose when标签
作用:多条件判断,与java中的if-else if 相同。
语法如下:
<c:choose>
<c:when test="条件1"> 条件1为true执行的代码 </c:when>
<c:when test="条件2"> 条件2为true执行的代码 </c:when>
....
<c:oterwise>以上条件都不满足时执行的代码</c:oterwise>
</c:choose>
演示的代码如下:
<%@ page import="com.txw.entity.Person" %>
<%@ page import="java.util.Date" %>
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%
Person person = new Person(100,"阿森",39,"女","19283717293","哈撒");
request.setAttribute("p",person);
%>
<%-- age<18 输少年,
age>=18且age<30输出青年,
age>=30且age<40输出中年,
age>=40且age<50输出中老年
age>=50输出老年--%>
<c:choose>
<c:when test="${p.age<18}">少年</c:when>--%>
<c:when test="${p.age<30}">青年</c:when> --%>
<c:when test="${p.age<40}">中年</c:when> --%>
<c:otherwise>中老年</c:otherwise> --%>
</c:choose>
</body>
</html>
如图所示:
package com.txw.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class Person implements Serializable {
private Integer id;
private String name;
private Date birthday;//java.util.Date
private String sex;
private String mobile;
private String address;
private String face;//用户的头像
public Person() {
}
public Person(Integer id, String name, Date birthday, String sex, String mobile, String address, String face) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.sex = sex;
this.mobile = mobile;
this.address = address;
this.face = face;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
", face='" + face + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) && Objects.equals(name, person.name) && Objects.equals(birthday, person.birthday) && Objects.equals(sex, person.sex) && Objects.equals(mobile, person.mobile) && Objects.equals(address, person.address) && Objects.equals(face, person.face);
}
@Override
public int hashCode() {
return Objects.hash(id, name, birthday, sex, mobile, address, face);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
}
choose-when执行流程和java中if-else-if相同。
3.forEach标签
作用:遍历数组、集合或循环。
1.遍历数组、List、Set。
语法如下:
<c:forEach var="元素名" items="数组或集合">
${元素名}获取元素
</c:forEach>
演示的代码如下:
<%@ page import="com.txw.entity.Person" %>
<%@ page import="java.util.List" %>
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%-- jsp脚本遍历--%>
<%List<Person> li =(List<Person>) request.getAttribute("list");
for(Person per: li ){
%>
<%= per.getId()%>
<%= per.getName()%>
<%= per.getBirthday()%>
<%= per.getSex()%>
<%= per.getMobile()%>
<%= per.getAddress()%>
<br/>
<%}%>
<%--jstl forEach标签遍历 list set 数组 --%>
<c:forEach var="pe" items="${list}">
${pe.id}
${pe.name}
${pe.age}
${pe.sex}
${pe.mobile}
${pe.address}<br/>
</c:forEach>
</body>
</html>
如图所示:
与Java中的forEach进行对比。如图所示:
遍历数组与遍历Set集合方式同上。
2. 遍历Map集合
与键值遍历一致。
语法如下:
<c:forEach var="元素名(键值对)" items="Map集合">
${元素名.key}
${元素名.value}
</c:forEach>
演示的代码如下:
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<%-- java代码键值遍历--%>
<% Map<Integer,String> m = (Map<Integer, String>) request.getAttribute("map");
Set<Map.Entry<Integer,String>> entrys = m.entrySet();
for(Map.Entry<Integer,String> entry:entrys){
%>
<%= entry.getKey()%>
<%= entry.getValue()%><br/>
<%}%>
<br/>
<%-- jstl forEach遍历 map --%>
<c:forEach var="entry" items="${map}">
${entry.key}
${entry.value}<br/>
</c:forEach>
</body>
</html>
如图所示:
与Java中的forEach进行对比,如图所示:
3. 循环
语法如下:
<c:forEach var="" begin="" end="" step="" >
</c:forEach>
属性含义,如图所示:
演示的代码如下:循环输出10次HelloWorld n。
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<c:forEach var="i" begin="1" end="10" step="1" >
HelloWorld ${i}<br/>
</c:forEach>
</body>
</html>
如图所示:
4 格式化标签
- 引入格式化标签库。
演示的代码如下:
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
</body>
</html>
如图所示:
2. 格式化输出日期
演示的代码如下:
<%@page pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta charset="UTF-8" />
<title>EL表达式</title>
</head>
<body>
<% java.util.Date date = new java.util.Date();
pageContext.setAttribute("now",date); %>
<fmt:formatDate value="${pageScope.now }" pattern="yyyy-MM-dd HH:mm:ss"/>
</body>
</html>
如图所示: