el表达式和jstl标签库

一:el表达式:表达式语言,jsp页面获取数据比较简单
1、el表达式的语法(掌握)
el表达式通常取值是获取作用域对象中的属性值:${属性名}=>是el表达式的简写的形式
跟jquery不一样,$(选择器)jquery对象,代码写在js的脚本块中
完整的书写形式:
  四个作用域 四种取值方式获取不同作用域中的属性值
${pageScope.attrname } pageScope.属性名
${requestScope.attrname }
${sessionScope.attrname }
${applicationScope.attrname }
注:jsp2.0以上版本,对应 servlet 3.0以上版本,jsp默认忽略EL表达式,所以在使用el表达式的时候需要在 page指令中加上。isELIgnored="false" 开启EL表达式,true忽略(默认是忽略)
EL表达式取值的两种方式例子:test.jsp

 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%
//将数据放到作用域对象中 //page域 当前页面有效
pageContext.setAttribute("uname", "印度阿三"); //一次请求有效,可以是多个页面,转发页面
request.setAttribute("fav", "睡觉"); //session域取值 一次会话,多个请求
session.setAttribute("value", "躺在床上听音乐"); //应用域中取值一个web应用
application.setAttribute("小喜庆", "小云云");
%>
<!--page域用el表达式取值 -->
page域用el表达式取值:
${uname }&emsp;
${pageScope.uname }<hr> <!--request域用el表达式取值 -->
request域用el表达式取值:
${fav }&emsp;
${requestScope.fav }<hr> <!--session域用el表达式取值 -->
session域用el表达式取值:
${value }&emsp;
${sessionScope.value }<hr> <!--application域用el表达式取值 -->
application域用el表达式取值:
${小喜庆 }&emsp;
${applicationScope.小喜庆 }<hr> </body>
</html>

在测试作用范围页面之前,必须先运行 test.jsp将数据放到作用域对象中
测试test.jsp四个作用域的作用范围【getTest.jsp】

 <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'getTest.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<p>在测试 当前页面之前,必须先运行 test.jsp将数据放到作用域对象中</p><hr>
测试test.jsp四个作用域的作用范围 <br><hr> <!--page 当前页面有效 -->
page域用el表达式取值:
${uname }&emsp;<br>
<!--request 一次请求有效,可以是多个页面,转发页面 -->
request域用el表达式取值:
${fav }&emsp;<br>
<!--session 一次会话,多个请求 -->
session域用el表达式取值:
${value }&emsp;<br>
<!--application 应用域中取值一个web应用 -->
application域用el表达式取值:
${小喜庆 }&emsp;<br>
</body>
</html>

如果不同的作用域,但是属性名相同

<!-- 【取值】如果不同的作用域,但是属性名相同 -->

注意:在省略 ***Scope对象的时候,取值的顺序,先从小范围获取数据 page,如果获取到了就返回,如果page获取不到,会去找request域,依次类推,找application ,如果都找不到,则返回null
2、el表达式获取不同数据类型的值(java 代码 字符串,数值,对象,list,map,数组)
①对象


注:在获取对象属性的时候,el表达式的解析的工具类,底层调用 的 get方法,不是直接调用的属性。el的解析对象用的反射,调用 get方法 Class ----getMethod("get方法")。${student.id }<=> ${student.getId }
②list


③map


注:map取值有两个:
点获取.简单【${map.fav }】
["key"] 比较灵活,可以处理特殊符号【${map.fav,ff }错误的 =>${map["fav,ff"] }】
④数组


student.java

 package boom.el.entity;

 import java.util.Date;

 /**
* 学生实体类对象
* @author Administrator
*
*/
public class Student {
private int id;
private String name;
private Date hiredate; public Student() {
} public Student(int id, String name, Date hiredate) {
super();
this.id = id;
this.name = name;
this.hiredate = hiredate;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getHiredate() {
return hiredate;
} public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
} @Override
public String toString() {
return "Sutudent [id=" + id + ", name=" + name + ", hiredate="
+ hiredate + "]";
} }

test.jsp

 <%@ page language="java" isELIgnored="false" import="java.util.* , boom.el.entity.*"  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>el表达式获取不同数据类型的值 </title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body> <!-- 获取对象的值 -->
<%
/* 获取对象的值 【创建一个实体类 ,获取对象的属性值】*/
Student student = new Student(0,"林徽因",new Date());
//将 Student对象 放到作用域对象中
request.setAttribute("student", student);
%>
${student }<hr>
${student.id }&emsp; ${student.name }&emsp; ${student.hiredate }&emsp;
<%-- 在获取对象属性的时候 ,el表达式的解析的工具类 底层调用 的 get方法,不是直接调用的属性
el的解析对象用的反射,调用 get方法 Class ----getMethod("get方法")
${student.id }<=> ${student.getId }
--%> <!-- 获取list的值 -->
<%
/* 创建对象存入list中 */
Student stu1 = new Student(1,"陆小曼",new Date());
Student stu2 = new Student(2,"周旋",new Date());
Student stu3 = new Student(3,"阮玲玉",new Date());
/* <!-- list存值 --> */
List<Student> list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
request.setAttribute("list", list);
%>
<!-- el表达式取值 -->
<hr> 获取 作用域对象中list<br>
${list.get(0) }<br>
<!-- list获取属性的具体值 -->
${list.get(1).name }<br>
${list.get(2) }<br><hr> <!-- 获取map的值 -->
<%
Map map = new HashMap();
map.put("fav", "music");
map.put("codeing", "Java");
map.put("fav,ff", "run");
request.setAttribute("map", map);
%>
<!-- el表达式取值 -->
${map }<br>
${map.fav }<br>
${map["fav,ff"] }<br><hr> <%-- map取值有两个
. 简单
["key"] 比较灵活 ,可以处理特殊符号
${map.fav,ff } 错误的 =>${map["fav,ff"] } --%> <!-- 获取数组的值 -->
<%
String[] arr = {"haha" , "gaga" ,"heihei"};
request.setAttribute("arr", arr);
%>
<!-- el表达式取值 -->
获取数组的元素:
${arr }&emsp;
${arr[0] } </body>
</html>

3、el表达式的基本运算
+ 字符串相加 非数值型字符串,在el表达式中不能直接相加,需要存放到request域中

4、el表达式可以在html代码块中,javascript 的脚本块中

5、el表达式的内置对象11个(掌握其中的一部分)
隐含对象 描述 
和作用域相关的【前4】,存取数据的隐含对象:【主要作用:获取作用域对象的数据 】



二:jstl标签库
1、jstl标签库,jsp标准标签库(只要jsp,标签库就起作用)
jstl标签库常用标签:jstl 标签库 for 循环,条件判断【for循环方法的封装 ,if 判断方法的封装】
2、jstl标签库的分类:
 ①核心标签库:c标签库 
 ②常用标签:foreach 标签 遍历数据、逻辑判断标签:c:if、c:when、c:choose、c:otherwise
 ③格式化标签库:时间格式化标签
 ④函数标签库
 ⑤xml标签库
 ⑥数据库sql标签库
3、jstl标签库使用
jstl 标签,就是 java代码对 函数的封装
myeclipse创建web项目的时候,自动加载jstl

①引入相应的jstl标签库
<!-- 核心标签库 -->
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!-- 函数标签库 -->
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
解释:@taglib引入、uri 标签库地址、
tld 文件 将标签库底层的java实现类和 jsp连接起来的文件

②使用标签库

参数解释:
<c:set></c:set>:存储数据、var 存储数据的变量、scope 作用范围、value 存储的数据 
<c:out value=""></c:out>:value 输出数据
<c:remove var="name"/>:移除数据
遍历:
<!--属性 :
var   遍历的变量 
items 要被遍历的数据
-->
<c:forEach var="stu1" items="${list }">
  ${stu1.id }=>${stu1.name }<hr>
</c:forEach>

最新文章

  1. android多线程断点续传下载文件
  2. 【51Nod 1674】【算法马拉松 19A】区间的价值 V2
  3. 学习使用 SVG 创建材料设计涟漪(Ripple)效果
  4. 【leetcode】Multiply Strings(middle)
  5. Android 指定日期时间执行任务的Timer
  6. 三级联动数据表db_nove.sql
  7. Wap touch flispan demo
  8. I/O浅析
  9. 2017年最受欢迎的UI框架
  10. RAID5当一块硬盘离线后处理
  11. IDEA快捷建使用
  12. Python基础:数据类型-列表与元组(6)
  13. hdu 4578 Transformation 线段树多种操作裸题
  14. ES6中字符串模板的使用
  15. luogu2542 航线规划 (树链剖分)
  16. C++基础知识:异常处理
  17. android有关生命周期探讨
  18. How to properly release Excel COM objects
  19. jenkins调用本地搭建sendmail邮件服务器发送邮件
  20. 前端统计利器:Sentry &amp; Matomo

热门文章

  1. 树莓派-Ubuntu Mate开启远程桌面xrdp服务
  2. Django 虚拟化环境创建
  3. javaweb期末项目-stage1-part2-UML设计
  4. 【VS开发】uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
  5. SQL ------ JDBC 删除指定的某条记录
  6. mysql数据库之函数、流程控制
  7. python装饰器使用详解
  8. Django模块
  9. 什么是阿里云ACE认证
  10. 链表操作Java实现