EL和JSTL

第一部分:EL表达式

EL表达式,需要引入JSTL标记库 ,因为Jsp把EL表达式加入时放在jstl中定义的
${error_msg}本质和<%%>一样,都是作为java代码出现在生成的servlet中,
由jsp引擎完成java代码的转换工作

基本语法点:
gt 大于 lt 小于 eq 等于
${}中可以写 对象,对象的方法,和表达式

四个范围:
${xxxxScope.user}
pageScope  pageContext(PageContext 类型) 一个页面对应一个pageContext对象,是临时存放数据的空间
requestScope 
sessionScope
applicationScope application(ServletContext 类型) 一个应用只有唯一的application对象(ServletContext)
   (在线人数)
对应方法
xxxx.setAttribute(... , ...)
xxxx.getAttribute(...)

自动范围搜索
${msg}依照范围从小到大的顺序,依次${pageScope.msg}/${requestScope.msg}/${sessionScope.msg}/${applicationScope.msg}
从某个范围中找到即停止。

//可能有的问题
${}中可以写 对象,对象的方法,和表达式

el.jsp:
<h1>${requestScope.msg}</h1>

<h1>
<%
Object obj = request.getAttribute("msg");
if (obj != null) {
 out.write(obj.toString());
} else {
 out.write("");
}
%>
</h1>

<h1>
Name: ${requestScope.user_one.name}
<br />
Age:${requestScope.user_one.age}
</h1>

<h2>${100 / 5}</h2>

 


第二部分:JSTL标记库--jsp的灵魂

引入jar包:jstl.jar standard.jar

引入标记库:<%@taglib uri="http://java.sun.com/jsp/jstl/core " prefix="c"%>
2.5版本需要加入:<%@page isELIgnored="false"%>  不忽略EL表达式

<c:if test="${requestScope.num gt 200}">
 <h1>
  ${requestScope.num}
 </h1>
</c:if>

<c:choose>
 <c:when test="${requestScope.num gt 200}">
  <h1>200</h1>
 </c:when>
 <c:when test="${requestScope.num gt 100}">
  <h1>100</h1>
 </c:when>
 <c:otherwise>
  <h1>ERROR</h1>
 </c:otherwise>
</c:choose>


<c:forEach items="${requestScope.arry1}" var="str">
 <h1>${str}</h1>
</c:forEach>

<table border="1" width="80%">
<tr>
 <td>Name</td>
 <td>Age</td>
</tr>

<c:forEach items="${requestScope.user_list}" var="u">
<tr>
 <td>${u.name}</td>
 <td>${u.age}</td>
</tr>
</c:forEach>
</table>

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值