EL_JSTL

EL表达式

在JSP页面中可以直接使用
EL用来代替<%= %>输出,EL表达式是用来输出 的。
在页面输出的几种方式

<%
		request.setAttribute("name","request");
%>
方式一  out:<% out.print(request.getAttribute("name"));%>
方式二  表达式:<%=request.getAttribute("name") %>
方式三 EL表达式:
	语法:	${表达式}   ${name }

11个内置对象
在这里插入图片描述
取值范围
四个域的寻找顺序是pageContext、request、session、application。若是找不到键值为指定属性的属性值,不会显示null,会显示空字符串。

${pageScope.name }
${requestScope.name }
${sessionScope.name }
${applicationScope.name }

取值List数组

	 // 集合
	List<String> list = new ArrayList<String>();
	list.add("刘备");
	list.add("关羽");
	list.add("张飞");
	HttpSession session =request.getSession();
	session.setAttribute("list", list);
	--------------------------------------------
	<!-- 集合 -->
	${list[1] }

缺点:EL表达式不支持遍历只能根据指定的下标获取值。

取值Map集合

   //Map
	Map<String,User> map = new HashMap<>();
	map.put("one", new User("c罗",33));
	map.put("two", new User("梅西",31));
	map.put("three", new User("内马尔",26));
	request.setAttribute("map", map);
	---------------------------------------
	<!-- Map 先获取键值,再根据键值获取属性值 -->
	${map.one.name }

支持运算符
1)语法${运算表达式 }
2)常见运算符

描述 格式
== eq
!= ne
< lt
> gt
<= le
>= ge
&& and
|| or
! not
判空 empty

三目运算符

${name==null?"null":name  }
${aaa==bbb?1:0}

JSTL标签

JSTL标签是apache对EL表达式的扩展,是标签语言。
不是JSP的内置标签,使用时需要导包。

JSTL标签库包括五个

描述 解释
core:核心标签库 核心标签,执行一些输出,设置值,可遍历
fmt:格式化标签库 格式化标签,可格式化日期、数字
sql:数据库标签库 用于在页面执行sql
xml:xml标签库 操作XML的标签
JSTL函数 字符串处理函数

JSP页面使用taglib指令导入标签库

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

prefix=“c”:指定标签库的前缀
uri=“http://java.sun.com/jsp/jstl/core”:指定标签库的uri

core标签
c:out 输出信息到页面

<%
	request.setAttribute("name", "张三");
%>

<c:out value="${name }" default="张某某"></c:out>

c:set设置值

`<!-- c:set 设置属性 	scope:可选四大域对象page、request、
session、application;默认page` -->
     <c:set var="sex" value="男" scope="request"></c:set>

c:if判断

<c:if test="${param.age<40 and param.age>=18}">
		<span style="color:blue;">青年</span></c:if>

c:choose 判断 相当于 if elseif else

<c:choose>
	<c:when test="${param.age<18 }">
		少年	<br>
	</c:when>
	<c:when test="${param.age<40 }">
		青年	<br>
	</c:when>
	<c:otherwise>
		中老年	<br>
	</c:otherwise>
	
</c:choose>

c:forEach遍历
在这里插入图片描述

	// 数组
	String[] arr = { "张三", "李四", "王五" };
	request.setAttribute("arr", arr);
	----------------------------------------
		<!-- C:forEach items:遍历的对象 var:每一次要遍历的元素 -->
	<c:forEach items="${arr }" var="str">
		${str }
	</c:forEach>

	  // 集合
		List<String> list = new ArrayList<String>();
		list.add("刘备");
		list.add("关羽");
		list.add("张飞");
		HttpSession session =request.getSession();
		session.setAttribute("list", list);
		----------------------------------------
		<c:forEach items="${list }" var="name">
			${name }
		</c:forEach>

		 //Map
		Map<String,User> map = new HashMap<>();
		map.put("one", new User("c罗",33));
		map.put("two", new User("梅西",31));
		map.put("three", new User("内马尔",26));
		request.setAttribute("map", map);
		--------------------------------
		<!-- 遍历Map -->
		<c:forEach items="${map }" var="entry">
			${entry.key}:${entry.value.name}:${entry.value.age }
		</c:forEach>

<!-- 遍历0-10之间的数字 -->
<c:forEach var="i" begin="0" end="10" step="2">
	${i }
</c:forEach>		

<c:forEach items="${users }" var="user" varStatus="status">
	${user.name }:${status.index }:${status.countl }
</c:forEach>

猜你喜欢

转载自blog.csdn.net/qq_43529877/article/details/84258235