EL表达式与JSTL

目录

EL表达式

 JSTL基本标

EL表达式

EL(Expression Language)表达式:

  1. 替代jsp页面中的复杂代码
  2. EL表达式只适用于域对象

JSP内置对象中的域对象有以下四个:

  1. pageContext:属性的作用范围仅限当前的jsp页面(范围最小)
  2. request:属性的作用范围仅限同一个请求(转发有效)
  3. session:属性的作用范围仅限于一次会话,浏览器打开直到关闭称为一次会话(在此期间会话不失效)
  4. application:属性的作用范围仅限于当前web应用(范围最大)

范围大小:application>session>request>pageContext
EL语法:

  1. ${EL expression}
  2. ${bean.name}

EL中的隐含对象:

  1. pageScope
  2. requestScope
  3. sessionScope
  4. applicationScope

EL运算符:

与Java中一致
${empty list}:判断集合中值是否为空,返回true或者false

实例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<%
	application.setAttribute("msg", "hello world");
	session.setAttribute("msg1", "hello");
	request.setAttribute("msg2", "world");
	pageContext.setAttribute("msg3", "world hello");
%>
<h1>
<!-- 
application:${applicationScope.msg}
session:${sessionScope.msg1}
request:${requestScope.msg2}
pageContext:${pageScope.msg3}
相当于底下的代码
 -->
application:${msg}<br>
session:${msg1}<br>
request:${msg2}<br>
pageContext:${msg3}<br>
</h1>
</body>
</html>

 效果如下:

 

 如果Attribute的命名相同,则只会返回最小的域对象的值:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<%
	application.setAttribute("msg", "hello world");
	session.setAttribute("msg", "hello");
	request.setAttribute("msg", "world");
	pageContext.setAttribute("msg", "world hello");
%>
<h1>
<!-- 
如果Attribute的命名相同,则只会返回最小的域对象的值
 -->
application:${msg}<br>
session:${msg}<br>
request:${msg}<br>
pageContext:${msg}<br>
</h1>
</body>
</html>

 效果如下:

 

 JSTL基本标签

因为JSTL与EL是连用的,所以JSTL中所有的值都是需要从域对象中取得的,不然是会报错的

使用JSTL前一定要导入jar包,还需要导入JSTL标签库(prefix为JSTL的名字)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
通用标签:

set:scope=域对象类型,value=值,var=域对象名字
out:value=${域对象名字}(建议使用EL直接获得,因为out多此一举)
remove:var=域对象名字,scope=域对象类型
条件标签:

if:test=是否执行(填true或者false)
迭代标签:

forEach:var=给取到的对象一个名字,items=需要遍历的数组(只能遍历域对象中的对象哦)
取到的对象可以调用对象的方法,但是不会有任何提示

<%@page import="java.util.ArrayList"%>
<%@page import="com.pojo.User"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>绥彼岸</title>
</head>
<body>
<%
	List<User> list=new ArrayList<>();
	//循环给数组设置值
	for(int i = 0; i <= 3; i++){
		User user=new User();
		user.setUserName("userName"+i);
		user.setUserPwd("userPwd"+i);
		list.add(user);
	}
	//将数组添加到域对象当中去
	pageContext.setAttribute("list", list);
%>

<!-- 分别为set、out、remove -->
<c:set scope="page" var="msg" value="hello world"></c:set>
<c:out value="${msg}"></c:out>
<c:remove var="msg" scope="page"/>

<!-- if标签 -->
<c:if test="true">
<h1>hello world</h1>

<!-- forEach标签 -->
<c:forEach var="user" items="${list}">
<h1>
${user.userName}<br>
${user.userPwd}<br>
</h1>
</c:forEach>
</c:if>
</body>
</html>

 效果如下:

 

以上就是Java JSP    EL表达式与JSTL的一些内容!!

JAR 包可以私信博主

 感 谢 阅 读 ……

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值