EL表达式---jstl

EL表达式

EL表达式
1.EL表达式核心点是将对象存储到某个域中,在需要的页面中直接使用表达式来使用,来减少强制 类型转换的操作;

EL表达式可自动类型转换

<%
//存储
session.setAttribute("name","曾璐");
//存储对象
Emp emp = new Emp();
emp.setEname("樊瑞泽");
emp.setSex("男");
session.setAttribute("emp",emp);
%>
name:${name}
<hr>
ename:${emp.ename}
sex:${emp.sex}

EL运算
1.关系运算符

== 或 eq判断是否相等
< 或 lt< 或 lt
> 或 gt判断是否大于
empty判断是否为空
!或not

2.逻辑运算符

&& 或 and
or
! 或or取反

.3.。多个作用域有相同的key

<%
request.setAttribute("name","a");
application.setAttribute("name","b");
session.setAttribute("name","c");
pageContext.setAttribute("name","d");
%>
<%=
${name}
%>

EL 获取四个特定域中的属性

EL JSP
pageScope pageContext
requestScope Request
sessionScope Session
applicationScope ServletContext

<body>
	<%
		// 从上往下, 优先级从高到低
		pageContext.setAttribute("key1", "pageContext1");
		pageContext.setAttribute("key2", "pageContext2");
		request.setAttribute("key2", "request");
		session.setAttribute("key2", "session");
		application.setAttribute("key2", "application");
	%>
	${ key2 }					// 默认选择优先级最高  值:pageContext2
	${ pageScope.key2 }			// 值: pageContext2
	${ requestScope.key2 }		// 值: request
	${ sessionScope.key2 }		// 值: session
	${ applicationScope.key2 }	// 值: 	application
</body>

JSTL标准标签库

jstl是一个标准标签库;可以简化jsp页面中的小脚本的使用频率,让jsp页面更整洁

步骤 :
1 .关联jia包
2 .引入jia包

引入方式:<!%--prefix:别名 uri:引入的标准库路径--%>
例:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

标签展示

<%--1~5循环--%>
<c:forEach begin="1" end="5" var="index">
  ${index}
</c:forEach>
<%--遍历数组
  items:指定要遍历的对象
    var:每次遍历对应的对象
    begin:开始值
    end:结束值
    varStatus:下标对象
    --%>
    <%--遍历数组-->
  <%
	int[] arr = {1,2,3,4}
	session.setAttribute("arr",arr);
	%>
 <c:forEach items="${strArr}" var="str" varStatus="indexObj">
  ${str},${indexObj.index}
</c:forEach>

<%		
		List<Emp> empList = new ArrayList<>();
        empList.add(new Emp(1,"李白"));
        empList.add(new Emp(2,"杜甫"));
        empList.add(new Emp(3,"苏轼"));
        session.setAttribute("empList",empList);
%>

<!--
  for(int i=0;i<empList.size();i++){empList.get(i);}
for(Emp emp : empList){}-->
<c:forEach items="${empList}" var="emp">
  姓名:${emp.ename},序号:${emp.eno}<br>
</c:forEach>
<hr>
<%
	//	创建Map集合
 	  Map map = new HashMap<>();
        map.put("法师","安其拉");
        map.put("射手","后羿");
        map.put("战士","亚瑟");
        session.setAttribute("map",map);
%>
<%--遍历Map集合--%>
<c:forEach items="${map}" var="map">
  ${map.key},${map.value}
</c:forEach>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值