JSTL和EL表达式的使用

来源:互联网 发布:吉他弹唱手机录音软件 编辑:程序博客网 时间:2024/05/21 09:22

第一部分: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

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

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

<c:choose>
 <c:when test="${requestScope.numgt 200}">
  <h1>200</h1>
 </c:when>
 <c:when test="${requestScope.numgt 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>

 

 

转载自http://blog.sina.com.cn/s/blog_45876a7c0100f79w.html

原创粉丝点击