<%@ page language="java" isELIgnored="false"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
//向page作用域中设置属性
pageContext.setAttribute("pageAttr", "page中的属性");
pageContext.setAttribute("pname", "pageName");
pageContext.setAttribute("count", 100);
pageContext.setAttribute("nil", null);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EL表达式+JSTL</title>
<script type="text/javascript">
window.onload = function() {
console.log('${color}');
}
</script>
</head>
<body style="color:${color}">
<h1>获取作用域中的值</h1>
<p>xxxScope.key</p>
<ul>
<li>page中:${pageScope.pageAttr }</li>
<li>request中:${requestScope.requestAttr }</li>
<li>session中:${sessionScope.sessionAttr }</li>
<li>application中:${applicationScope.applicationAttr }</li>
</ul>
<hr />
<p>实际上可以省略sope</p>
<ul>
<li>page中:${pageAttr }</li>
<li>request中:${requestAttr }</li>
<li>session中:${sessionAttr }</li>
<li>application中:${applicationAttr }</li>
</ul>
<p>获取name</p>
<p>${name }</p>
<p>tips:在没有指定作用域的情况下,默认查找顺序 page>request>session>application</p>
<p>el表达式运算</p>
算数运算:${count+100 }
<br /> 关系运算:${count>200 }
<br /> 逻辑运算:${count>90 && count+100<201 }
<p>EL对null的处理</p>
${nil } ${abc } ${def==null?'为null':"不为null" }
<p>tips:当el获取一个为null的值时,不会做任何输出</p>
<hr />
<p>POJO属性获取</p>
<ul>
<li>String属性:${com.brand }</li>
<li>float属性:${com.price }</li>
<li>属性导航:${com.board.brand }</li>
</ul>
<p>获取集合</p>
<ul>
<li>整个集合${coms }</li>
<li>获取其中的某一个:${coms[2] }</li>
<li>获取电脑的属性:${coms[2].brand }</li>
</ul>
<p>获取map</p>
<ul>
<li>获取整个map:${map }</li>
<li>获取其中一个值:${map.com1 }</li>
<li>获取其中一个值:${map["com1"] }</li>
<li>获取其中一个值的属性:${map["com1"].brand }</li>
</ul>
<p>EL获取请求参数</p>
<ul>
<li>获取单个参数 :${param.name }</li>
<li>获取所有的请求值:${paramValues.op }</li>
</ul>
<p>EL获取请求头</p>
<ul>
<li>获取单个的请求头:${header.Host }</li>
<li>获取所有的请求头map:${headerValues }</li>
</ul>
<p>获取Cookie</p>
<ul>
<li>获取所有的cookie:${cookie }</li>
<li>获取单个cookie:${cookie.loginTime} }</li>
</ul>
<p>获取当前web应用的名称</p>
<h2>${pageContext.request.contextPath }</h2>
<hr />
<p>JSTL</p>
<p>输出:c:out</p>
<p>
<c:out value="abc" />
--
<c:out value="${param.name }" />
</p>
<p>c:if标签</p>
<p>
<c:if test="${count>100 }" var="r" scope="request">
count>100<br />
</c:if>
r=${requestScope.r }
</p>
<p>c:choose</p>
<p>
<c:choose>
<c:when test="${count>100 }">
大于100
</c:when>
<c:when test="${count==100 }">
等于100
</c:when>
<c:otherwise>
小于100
</c:otherwise>
</c:choose>
</p>
<p>c:foreach</p>
<p>
<c:forEach begin="1" end="6" step="2" varStatus="status">
${status.index }geekHome<br>
</c:forEach>
</p>
<p>
<c:forEach items="${coms }" var="com" varStatus="status">
${status.index }--${com.brand }-${com.price }<br/>
</c:forEach>
</p>
</body>
</html>
EL表达式+JSTL,forEach的两种用法
猜你喜欢
转载自blog.csdn.net/qq_15204179/article/details/82594400
今日推荐
周排行