EL详解&jstl标签

1 基本使用

EL 不是jsp 研发的,而是第三方,但是在jsp 当中默认支持EL的。

1: 作用: 取代在jsp 页面上嵌入代码的。 主要是用来向页面输出内容的;

2: EL 语法:
${ }

3: EL : 在jsp 页面上默认支持,可以通过page 指令当中: isElIgnore=true;

4: EL能够输出内容:
常量:
数值:
数值运算后的结果:
域对象当中的内容:

5: EL表达式,不输出null。 null 在页面上不显示:

6: 运算符

 empty  判断是否为null: 
 语法: ${empty "内容"}

String str=""; //默认为null :
创建一个空的list 集合:  默认为null :
创建一个空的map 集合: 默认为null;

三目运算符:
${empty 表示式 ? “”:""};

案例: ${sex==‘nan’?‘checked’:’’ }
代码如下:

<%@ page language="java" import="java.util.*,com.yidongxueyuan.domain.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '02-EL.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>
   <!-- EL的基本使用:  -->
   <jsp:useBean id="user" class="com.yidongxueyuan.domain.User"></jsp:useBean>
   

   输出常量: 
   ${"hello" }  <br/>
   ${1+2 } <br/>
   ${user } <br/>
   ${null } <br/>
   
   
   <%
   
   // 空字符, 被EL 默认是null。 
      String str="" ;
      pageContext.setAttribute("str", str);
     
    // 创建一个list 集合: 
      List list= new ArrayList();
     // list.add(1);
      pageContext.setAttribute("list", list, PageContext.REQUEST_SCOPE);
      
      
      Map<String ,String> map = new HashMap<String,String>(); 
        pageContext.setAttribute("map", map, PageContext.REQUEST_SCOPE);
    %>
  <%--   <%
      pageContext.findAttribute("str");
     %> --%>
    
    <hr/>
     ${empty str } <br/>
     ${empty list } <br/>
     ${empty map } <br/>
   
   
     <%
         String sex="nan";
         pageContext.setAttribute("sex", "nan", PageContext.REQUEST_SCOPE);
        
      %>
      
      <input type="radio" name="sex" value="nan"  ${sex=='nan'?'checked':'' }/>男
      <input type="radio" name="sex" value="nv"   ${sex=='nv'?'checked':'' }/>女
     
  </body>
</html>

7: 输出域对象当中的内容:

${attName} 从全域当中进行查询 属性名称为attName 对应的值:
查找的顺序: pageContext request session application:
全局找不到, 返回null ,但是null 不输出:

等价于:
pageContext.findAttribute(“attName”);

指定域对象进行快速的查询:
${username }

${requestScope.username }

${sessionScope.username }

${applicationScope.username }

8: 使用EL 表达式的导航连结构:

2 向指定域输出

代码如下:

在指定域查找效率更高,域的使用原则,优先使用小的域,生命周期比较短。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '03-EL.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>
   <%
   	 pageContext.setAttribute("username", "page");
   	 pageContext.setAttribute("username", "request",PageContext.REQUEST_SCOPE);
   	 pageContext.setAttribute("username", "session",PageContext.SESSION_SCOPE);
   	 pageContext.setAttribute("username", "application",PageContext.APPLICATION_SCOPE);
    %>
    
    <!-- 使用EL表达式输出:  -->
    ${username } <!-- 使用EL表达式输出:  --> <br/>
    ${requestScope.username } <br/>
    ${sessionScope.username }<br/>
    ${applicationScope.username }<br/>
   
  </body>
</html>

当然在实际开发当中存的不是键值对,一般是数组或者集合之类的
如下:

EL表达式的导航链结构(重点):

下面有一段代码用到了请求转发,补充上
https://blog.youkuaiyun.com/a327736051/article/details/49147651

本质上是一连串的get方法,即 注释里面的三行get方法用一行EL表达式表示。
具体操作:
1 去掉get
2 首字母小写

<%@ page language="java" import="java.util.*,com.yidongxueyuan.domain.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'show.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>
    <!-- 展示用户的信息:  -->
 <%--    <%
      User user =(User)  request.getAttribute("user");
      String name=  user.getUsername();
     %> --%>
     
     <!-- 语法:   map.key
                            对象.属性名称; getUserName(); getPassword getXxx()
          username
     -->
    姓名:  ${requestScope.user.username }<br/>
    密码: ${requestScope.user.password } <br/>
       ${requestScope.user.xxx } <br/>
       
       
       <%
           User user =(User)  request.getAttribute("user");
           Address add=  user.getAdd();
           add.getCity();
           
           pageContext.setAttribute("user-name", "lisi");
        %>
  city: ${requestScope.user.add.city } <br/>
  street: ${user.add.street }
  <hr/>
  
  name: ${pageContextScope['user-name'] }
    
    
  </body>
</html>

EL操作集合

在这里插入图片描述

(一)操作list集合:

servlet文件

package com.yidongxueyuan.domain;

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;

public class ShowUser extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		User user = new User(); //从数据库当中查询: 
		
		Address add= new Address("qd","laoshan");
		user.setPassword("123");
		user.setUsername("lisi");
		user.setAdd(add);
		
		User user1 = new User(); //从数据库当中查询: 
		
		Address add1= new Address("hl","xxxx");
		user1.setPassword("123sssss");
		user1.setUsername("lisissss");
		user1.setAdd(add1);
		
		List<User> list = new ArrayList<User>();
		list.add(user);
		list.add(user1);
		
		
		//将对象的信息 展示在页面上:  对象“: 
		//request.setAttribute("user", user);
		//request.getRequestDispatcher("/show.jsp").forward(request, response);
		
		
		//List集合: 
		//request.setAttribute("list", list);
		//request.getRequestDispatcher("/showAll.jsp").forward(request, response);
		
		Map<String,User> map = new HashMap<String,User>();
		map.put("u1", user1);
		map.put("u2", user);
		
		request.setAttribute("map", map);
		
		request.getRequestDispatcher("/showAllByMap.jsp").forward(request, response);
	}

}

因为在下面代码中 - 可能被当做运算符,所以后者的功能更加强大。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showAll.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>
   
   <!-- 使用EL 表达式直接输出:  -->
  user对象:  ${list[0] }<br/>
  user对象username:  ${list[0].username }<br/>
  user对象password:  ${list[0].password }<br/>

  
  <hr/>
    <!-- 使用EL 表达式直接输出:该方式功能更加的强大:   -->
  user对象:  ${list[0] }<br/>
  user对象username1:  ${list[0]['username'] }<br/>
  user对象password2:  ${list[0]['password'] }<br/>
  
  
  </body>
</html>

(二) 操作map集合

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showAllByMap.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>
    <!-- EL输出map
    集合内容: 
     map.key 
     -->
     
    u1 对象: ${map.u1 }<br/>
    u1 对象名称: ${map.u1.username }<br/>
    u1 对象密码: ${map.u1.password }<br/>
    
  </body>
</html>

EL 的11个内置对象:

作用: 向页面输出内容:
内置对象在页面可以直接使用
<%
pageContext.setAttribute(“username”,“lisi”);
%>

pageScope
requestScope
sessionScope
applicationScope
------------------------以上四个EL 的内置对象, 是和域对象相关:
param 一个key 对应一个值的情况:
paramValues 一个key 对应多个值的情况:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '01.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>
   <%--
     演示: EL 的 内置对象: param 
     一个key 对应一个值的情况
    --%>
    
    ${param.username }<br/>
    ${param.password }<br/>
    
    <br/>
    <!-- 一个key 对应多个值的情况: 
      取数组当中的内容, 和取llist 集合是一样子。 
     -->
    ${paramValues.hobby[0] }<br/>
    ${paramValues.hobby[1] }<br/>
    
    
    <!-- 和头信息相关的: -->
    
    <!-- header -->
    ${header.Host }<br/>
    ${header['User-Agent'] }<br/>
    
    <!-- headerValues -->
     ${headerValues['User-Agent'][0] }
     
     
    <!-- initParam; 初始化参数相关:
     配置初始化参数: web.xml当中进行配置: 
      -->
      ${initParam['encoding']}<br/>
      
      <!-- cookie对象 -->
   cookie对象:     ${cookie.JSESSIONID } <br/>
   cookie对象的名称:     ${cookie.JSESSIONID.name }  <br/>
   cookie对象的值:     ${cookie.JSESSIONID.value }  <br/>
   cookie对象的生命:     ${cookie.JSESSIONID.maxAge }  <br/>
   
   
  </body>
</html>


header; 一个head 对应一个值:

headerValues; 一个头对应多个值的情况: cookie


initParam : 能够获得web.xml当中配置的初始化参数:

cookie;
map.key 获得cookie 对象: 通过对象的导航链结构,调用相应的get方法。


总结: 以上十个EL 的内置对象都是map 结构: map.key
pageContext 对象: 类型PageContext类型(特殊一些):

在这里插入图片描述

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '02.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>
   <!-- 演示 PageContext 内置对象 -->
   请求的方法: ${pageContext.request.method }<br/>
   获得session 的id: ${pageContext.session.id }<br/>
   获得contextPath: ${pageContext.request.contextPath }
   
  </body>
</html>

在下面代码中,JSP页面访问的路径不能被写死,所以用contextpath路径来写:
凡是涉及到项目名称的,都应该这么来写。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'login.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="${pageContext.request.contextPath }/css/01.css">
	 </head>
  
  <body>
  <!-- ${pageContext.request.contextPath } -->
   <form action="<c:url value='/servlet/LoginServlet' />" >
   	<input type="text" name="username"/>
   	<input type="submit" value="提交"/>
   </form>
  </body>
</html>

EL 的函数库:

1: EL 函数库的作用: 对输出的字符串进行相关的操作:
2: EL 函数库,不是EL 的内容, 是jstl 对EL的扩展。 在使用的使用应该导入第三方库:
<%@ taglib uri="" prefix="" %>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '03.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>
  <!--  EL的函数库: 主要就是针对字符串进行了相关的操作:
    1: 使用taglib 指令,导入函数库: 
    
    2: 使用jstl提供的函数库
    -->
   length:  ${fn:length("hello") } <br/>
   
   upperCase: ${fn:toUpperCase("hello") }<br/>
    
    indexOf : ${fn:indexOf("Hello","o") }<br/>
    
    截取: ${fn:substringAfter("Hello-world","-") }   <br/>
    ${fn:escapeXml("<font color='red'>jjj</font>")}
  </body>
</html>

四: 自定义函数库:
(1)创建一个类:
(2)提供一个方法:
方法是静态的, 必须有返回值。 参数可有可无。
(3)编写一个tld文件: tld 放在web-inf下, 安全。
(4)在jsp 页面上直接引入tld文件。 就可以使用自定义函数。
不知道tld文件写啥的话,去他原来的fn.tld下面找一下,类似于xml的东西

前面部分 总结:
内置对象:
和域对象相关:
${}
${pageScope.key}
${requestScope.key}
${sessionScope.key}
${applicationScope.key}

// 和参数相关:
${param}
${paramValues}
//和请求头相关:
${header}
${headerValues}
//初始化参数相关:
${initParam}
${cookie.cookeName}; 获得cookie的对象:
pageContext: PageContext:

${pageContext.rerequest.contextPath};

函数库:
(1) 导入第三方库: <%taglib uri="" prefix=""%>
(2)jsp页面上使用 函数。

定义函数:
(1) 类。
(2)静态返回值的方法:
(3)tld文件。 参考人家设定的
(4)jsp页面上 直接使用自定义的函数。

jstl: java strand tag lib java 的标准标签库。

1 是对EL 的扩展。 在使用的时候, 依赖EL,但不是内置的所以使用的时候需要导包 。
myeclipse会自动加进去,其他需要手动导入四个jar包。
手动导包方法

前缀官方建议使用:c
所以又叫做c标签库

2:作用: 和jsp的动作标签的作用一样, 取代java 片段的。

3: jstl 的四大库:
core: 核心库:重点。
fmt: 格式化库(只会两个就行了): 日期和数字的格式化。
xml: 过时
sql: 过时

4: core 库当中标签:

a: 输出内容:
<c: out value="" default="" escapeXml=“true” />
value="" 常量, EL 表达式:
default="" 当value 的值为null ,可以指定默认值。

指定默认值这一点比EL强大一些

escapeXm=""是否逃脱xml 的格式。 默认逃脱。

b: 向域对象当中设置值:
<c:set value="" var="" scope=""/>
var=""属性的名称:
value: 属性的值:
scope: 指定的存放的域: 值: page request session application

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '01.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>
   <!-- 
     演示jstl core 库当中的标签: 
    -->
    
    <!-- out标签:  -->
    <%
       pageContext.setAttribute("username", "lisi");
     %>
    hello   <br/>
    ${'hello' }  <br/>
    <c:out value="${'hello' }"/> <br/>
    <hr/>
    
    ${username2 }<br/>
    <c:out value="${username2 }" default="xxx" />
    <c:out value="${'<font color=red>hehe</font>' }" escapeXml="false" default="xxx" /><br/>
    
    <hr/>
    <!-- set 标签 -->
    <%
        pageContext.setAttribute("username", "requestName" , PageContext.REQUEST_SCOPE);
     %>
    <c:set var="username" value="wangbadan" scope="application" />
    <c:out value="${applicationScope.username }" ></c:out>
    
    <!-- remove 从域对象当中移除L -->
   <c:remove var="username" scope="page"/>
   page: <c:out value="${pageScope.username }"></c:out> <br/>
   request:<c:out value="${requestScope.username }"></c:out> <br/>
   application: <c:out value="${applicationScope.username }"></c:out>
   <hr color='red'/>
   <!-- url:  -->
   <c:url value="/servlet/LogingServlet"/><br/> 
   <c:url value="/" var="url"/>
   <c:out value="${url }" />
  </body>
</html>

c: 移除域对象当中的属性和属性值。
<c: remove var="" scope=""/>
scope=“”指定从某个域当中进行删除。

scope没有指定。 默认情况,从全域当中删除。

d: url 和路径相关的标签:
<c: url value="/" /> 直接将值输出:
<c: url value="" var="" scope=""/> 将value 的值存放的域对象当中。
作用: 能够自动的实现URL 重写:

重写:
http://localhost:8080/javaEE-16/servlet/LoginServlet ?jsessionId =“322323232”
在这里插入图片描述
无论是否禁用cookie,都可以传递给后台sessionid的值。

e:
if标签&choose表达式:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP '02.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>
  <!-- 
    if ----if
    if else if  else if  else  choose 
    
   -->
   <c:set var="username" value="lisi"/>
   <c:if test="${not empty username }">
   		标签体的内容执行了
   </c:if>
   
   <!-- 演示: 给定成绩答应等级:  -->
   <c:set var="score" value="${param.score }">
       
   </c:set>
   
   
   <c:choose>
   	   <c:when test="${score>=90 && score <=100 }">
   	         优秀
   	   </c:when>
   	   <c:when test="${score>=80 && score <=89 }">
   	         良好
   	   </c:when>
   	   <c:when test="${score>=70 && score <=79 }">
   	         中等
   	   </c:when>
   	   <c:when test="${score>=60 && score <=69 }">
   	         及格
   	   </c:when>
   	   <c:when test="${score>=0 && score <=59 }">
   	         不及格
   	   </c:when>
   	   <c:otherwise>
   	          错误的分数
   	   </c:otherwise>
   	   
   </c:choose>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方-教育技术博主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值