数据在 Servlet 中获取,然后跳转到 jsp 页面显示,需要从作用域中获取数据,在 jsp 中获取数据需要使用 java 脚本。 jsp 中不要存在一行 java 脚本,所以使用 EL 表达式来获取作用域中的共享数据
需求:从作用域中设置数据,然后在 jsp 页面中获取作用域中的数据
EL 表达式:
作用:从 page, request, session, application 中从小到大检索数据
语法:${属性名}, 注意属性名不要使用引号
没有使用 EL 表达式:
1 2 3
| <%=pageContext.findAttribute("msg") == null ? "" : pageContext.findAttribute("msg")%>
${msg}
|
EL 表达式获取 JavaBean 中数据(普通类型,数组,集合,map)的三种方式:
- ${p.username}
- ${p.getUsername()}
- ${p[“username”]} map 的 key,如果属性名有一些特殊符号(. _)用第三种。
empty
${empty cookie.username ? "" : checked}
EL 中内置对象
pageScope
只能取值,
获取jsp中pageContext域属性,相当于${pageContext.getAttribute("xxx”)}
,可以使用${pageScope.objectName}
获取 jsp 页面中的对象, 也可以使用 pageScope.objectName.attributeName 访问对象的值
只能在 jsp 文件内部使用。
requestScope
requestScope 本身不是 request对象,相当于 pageContext.request.getAttribute("xxx")
该对象允许访问请求对象的属性。例如,EL 表达式可以使用 ${requestScope.objectName}
访问一个 JSP 请求范围的对象,还可以使用 ${requestScope.objectName.attributeName}
访问对象的属性。
sessionScope
该对象允许访问会话对象的属性。${sessionScope.name}
applicationScope
该隐式对象允许访问应用程序范围的对象。
EL 细节
获取上下路径 使用内置对象 pageContext
- ${pageContext.request.contextPath}
${pageContext.request.contextPath}/student?
算数运算
- ${1 + 3/2}
- ${empty list} 判断集合元素是否为空
判断
解决 EL 表达式失效问题
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="false"> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored> </jsp-property-group> </jsp-config> </web-app>
|
JSTL 标签库
导入jar包:taglibs-standard-impl-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
通过JSP的 taglib指令,引入标签库.
标签库 |
URI |
前缀 |
使用方式 |
核心标签库 |
http://java.sun.com/jsp/jstl/core |
c |
<c:tagname…> |
国际化标签(I18N) |
http://java.sun.com/jsp/jstl/fmt |
fmt |
<fmt:tagname…> |
SQL标签库 |
http://java.sun.com/jsp/jstl/sql |
sql |
<sql:tagname…> |
XML标签库 |
http://java.sun.com/jsp/jstl/xml |
x |
<x:tagname…> |
语法
每个<>都是标签
单条件判断
1 2
| <c:if test="${age > 18}"
|
多条件判断
1 2 3
| <c:choose> <c:when test="${age > 18}"> <c:otherwise>
|
循环迭代标签
1 2
| <c:forEach begin="" end="" var="" step="">
|
用法举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <%@page import="java.util.Date"%> <%@page import="java.util.Collections"%> <%@page import="java.util.ArrayList"%> <%@page import="cn.lizhaoloveit.forward.Person"%> <%@page import="java.util.List"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ 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> <br>username:${p.name} -> ${p.getName()} <br>age:${p.age } <% request.setAttribute("age", 19); %> <c:if test="${age > 18}"> age 大于 18 </c:if> <c:if test="${age < 18 }"> age 小于 18 </c:if> <c:if test="${age == 18 }"> age 等于 18 </c:if> <br> <c:choose> <c:when test="${age > 18 }"> age 大于 18 </c:when> <c:when test="${age < 18 }"> age < 18 </c:when> <c:otherwise> age = 18 </c:otherwise> </c:choose> <% List<Person> list = new ArrayList<>(); Collections.addAll(list, new Person(1l, "李朝1", 18), new Person(1l, "李朝2", 18), new Person(1l, "李朝3", 18)); request.setAttribute("list", list); request.setAttribute("date", new Date()); %> <c:forEach begin="1" end="10" var="num" step="1"> ${num} </c:forEach> <!-- 如果有时间,时间格式使用参数 pattern = "yyyy-MM-dd HH:mm:ss" --> <c:forEach var="person" items="${list }"> ${person.name } -> ${person.age } <br> </c:forEach> <fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/> </body> </html>
|