[转]EL表达式和JSTL表达式实例

/*
**Title:EL表达式和JSTL表达式实例
**Author:Insun
*/


为了方便写JSP,我们引入了EL和JSTL表达式
EL表示类似:<%=request.getAttribute("info")%>
JSTL类似:
<table border=1>
<tr><td>ID</td><td>用户名</td><td>密码</td></tr>
<c:forEach items="${requestScope.list}"  var="list" >
<tr>
  <td><c:out value="${list.id}"/></td>
  <td><c:out value="${list.name}"/></td>
  <td><c:out value="${list.password}"/></td>
</tr>
</c:forEach>
</table>

JSTL要引入库文件的,EL不用。
JSTL下载(C标签) :http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/                      
这个目录自己找个下载下copy到工程lib目录。
要记得引用:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

[转]EL表达式和JSTL表达式实例

 

Java Servlet实现注册和登录  http://yxmhero1989.blog.163.com/blog/static/11215795620111016103040944/

在这篇中把welcome.jsp写个JSTL示例:



=========================================================================================================

JSP中的EL表达式详细介绍
http://www.diybl.com/course/4_webprogram/jsp/jsp_js/2008229/102031.html


一、JSP EL语言定义


        E L(Expression Language)  目的:为了使JSP写起来更加简单。

       表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法。它是一种简单的语言,基于可用的命名空间(PageContext 属性)、嵌套属性和对集合、操作符(算术型、关系型和逻辑型)的访问符、映射到 Java 类中静态方法的可扩展函数以及一组隐式对象。

       EL 提供了在 JSP 脚本编制元素范围外使用运行时表达式的功能。脚本编制元素是指页面中能够用于在 JSP 文件中嵌入 Java 代码的元素。它们通常用于对象操作以及执行那些影响所生成内容的计算。JSP 2.0 将 EL 表达式添加为一种脚本编制元素。

二、JSP EL简介

1、语法结构
     ${expression}
2、[ ]与.运算符
     EL 提供“.“和“[ ]“两种运算符来存取数据。
     当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用“[ ]“。例如:
         ${user.My-Name}应当改为${user["My-Name"] }
     如果要动态取值时,就可以用“[ ]“来做,而“.“无法做到动态取值。例如:
         ${sessionScope.user[data]}中data 是一个变量
3、变量
     EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。
     因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。
     假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。
     属性范围在EL中的名称
         Page          PageScope
         Request          RequestScope
         Session          SessionScope
         Application      ApplicationScope

二、JSP EL 中的有效表达式

       有效表达式可以包含文字、操作符、变量(对象引用)和函数调用。我们将分别了解这些有效表达式中的每一种:

1、文字

JSP 表达式语言定义可在表达式中使用的以下文字:
 

 

文字 文字的值

Boolean

true 和 false

Integer

与 Java 类似。可以包含任何正数或负数,例如 24、-45、567

Floating Point

与 Java 类似。可以包含任何正的或负的浮点数,例如 -1.8E-45、4.567

String

任何由单引号或双引号限定的字符串。对于单引号、双引号和反斜杠,使用反斜杠字符作为转义序列。必须注意,如果在字符串两端使用双引号,则单引号不需要转义。

Null null
2、操作符

JSP 表达式语言提供以下操作符,其中大部分是 Java 中常用的操作符:

 

术语 定义

算术型

+、-(二元)、*、/、div、%、mod、-(一元)

逻辑型

and、&&、or、||、!、not

关系型

==、eq、!=、ne、、gt、<=、le、>=、ge。可以与其他值进行比较,或与布尔型、字符串型、整型或浮点型文字进行比较。

空操作符是前缀操作,可用于确定值是否为空。

条件型 A ?B :C。根据 A 赋值的结果来赋值 B 或 C。
3、隐式对象

JSP 表达式语言定义了一组隐式对象,其中许多对象在 JSP scriplet 和表达式中可用:

 

术语 定义

pageContext

JSP 页的上下文。它可以用于访问 JSP 隐式对象,如请求、响应、会话、输出、servletContext 等。例如,${pageContext.response} 为页面的响应对象赋值。

此外,还提供几个隐式对象,允许对以下对象进行简易访问:

 

术语 定义

param

将请求参数名称映射到单个字符串参数值(通过调用 ServletRequest.getParameter (String name) 获得)。getParameter (String) 方法返回带有特定名称的参数。表达式 $(param.name) 相当于 request.getParameter (name)。

paramValues

将请求参数名称映射到一个数值数组(通过调用 ServletRequest.getParameter (String name) 获得)。它与 param 隐式对象非常类似,但它检索一个字符串数组而不是单个值。表达式 ${paramvalues.name) 相当于 request.getParamterValues(name)。

header

将请求头名称映射到单个字符串头值(通过调用 ServletRequest.getHeader(String name) 获得)。表达式 ${header.name} 相当于 request.getHeader(name)。

headerValues

将请求头名称映射到一个数值数组(通过调用 ServletRequest.getHeaders(String) 获得)。它与头隐式对象非常类似。表达式 ${headerValues.name} 相当于 request.getHeaderValues(name)。

cookie 将 cookie 名称映射到单个 cookie 对象。向服务器发出的客户端请求可以获得一个或多个 cookie。表达式 ${cookie.name.value} 返回带有特定名称的第一个 cookie 值。如果请求包含多个同名的 cookie,则应该使用 ${headerValues.name} 表达式。
initParam 将上下文初始化参数名称映射到单个值(通过调用 ServletContext.getInitparameter(String name) 获得)。

除了上述两种类型的隐式对象之外,还有些对象允许访问多种范围的变量,如 Web 上下文、会话、请求、页面:

 

术语 定义

pageScope

将页面范围的变量名称映射到其值。例如,EL 表达式可以使用 ${pageScope.objectName} 访问一个 JSP 中页面范围的对象,还可以使用 ${pageScope.objectName.attributeName} 访问对象的属性。

requestScope

将请求范围的变量名称映射到其值。该对象允许访问请求对象的属性。例如,EL 表达式可以使用 ${requestScope.objectName} 访问一个 JSP 请求范围的对象,还可以使用 ${requestScope.objectName.attributeName} 访问对象的属性。

sessionScope

将会话范围的变量名称映射到其值。该对象允许访问会话对象的属性。例如:


$sessionScope.name}

applicationScope

将应用程序范围的变量名称映射到其值。该隐式对象允许访问应用程序范围的对象。

三、特别强调:

1、注意当表达式根据名称引用这些对象之一时,返回的是相应的对象而不是相应的属性。例如:即使现有的 pageContext 属性包含某些其他值,${pageContext} 也返回 PageContext 对象。

2、 注意 <%@ page isELIgnored="true" %> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言。

四、举例说明

1、例如,

      < %=request.getParameter(“username”)% >       等价于       ${ param.username }

2、EL语言可以完成如果得到一个username为空,则不显示null,而是不显示值。     

<%=user.getAddr( ) %>      等价于     ${user.addr}。

3、例如:

<% =request.getAttribute(“userlist”) %>     等价于$     { requestScope.userlist }

4、例如,原理如上例3。

${ sessionScope.userlist } 1

${ sessionScope.userlist } 2

${ applicationScope.userlist } 3 

${ pageScope.userlist } 4

${uselist} 含义:执行顺序为4 1 2 3。

“.”后面的只是一个字符串,并不是真正的内置对象,不能调用对象。

4、例如,

   <%=user.getAddr( ) %>      等价于     ${user.addr}

第一句前面的user,为一个变量。

   第二句后面user,必须为在某一个范围里的属性。


JSTL实例:http://leaderbird.iteye.com/blog/275834


<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,com.j2ee14.ch12.User"%>
<%
   Collection users_c=new ArrayList();
   for(int i=0;i<3;i++)
   {
      User user=new User();
      user.setUserName("foo"+i);
      user.setPassword("foo"+i);  
      users_c.add(user);
   }
   session.setAttribute("users",users_c);
%>
<html>
<head>
  <title>JSTL:c:forEach的使用之一</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>迭代某个collection中的元素。</h4>
<table border=1>
<tr><td>用户名</td><td>密码</td></tr>
<c:forEach var="users" items="${users}">
  <tr>
  <td><c:out value="${users.userName}"/></td>
  <td><c:out value="${users.password}"/></td>
  </tr>
</c:forEach>
   </table>  
</center></body>
</html>

------------------------------------------------------------

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <tcounttle>JSTL:c:forEach的使用之二</tcounttle>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>第二种迭代:50-60</h4>

<c:forEach var="count" begin="50" end="60">
  <c:out value="${count}"/> **
</c:forEach>
<h4>第二种迭代:10 to 100,step=10</h4>
<c:forEach var="count" begin="10" end="100" step="10">
  <c:out value="${count}"/>**
</c:forEach>
</center>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:c:remove的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:set value="10000" var="maxUser" scope="application"/>
<c:set value="10" var="count" scope="session"/>
maxUser=<c:out value="${maxUser}"/><br>
count=<c:out value="${count}"/>
<hr>调用c:remove...
<c:remove var="maxUser" scope="application"/>
<c:remove var="count"/>

调用了c:remove后,参数值为:
maxUser=<c:out value="${maxUser}"/>,
count=<c:out value="${count}"/>,
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>

<jsp:useBean />
<html>
<head>
  <title>JSTL:的使用c:set</title>
</head>
<body bgcolor="#FFFFFF">
<hr>
设置一个属性,然后输出它<br>
<c:set var="maxCount" value="100"/>
<c:out value="${maxCount}"/>
<hr>设置属性时,把它的值放在标签的body中。
<c:set var="password">
ksdjfxsdf234234
</c:set>
<c:out value="${password}"/>
<hr>设置javaBean的属性,然后输出这些属性值:
<c:set value="hellking" target="${user}" property="userName"/>
userName=<c:out value="${user.userName}"/>

<hr>设置属性,并且指定它们的范围,属性的默认范围是page。
<c:set value="20" var="maxIdelTime" scope="session"/>
<c:set value="next.jsp" var="nextPage" scope="page"/>

</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL c:url 的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:url var="footer" value="footer.jsp" scope="page">
<c:param name="id" value="hellking"/>
</c:url>
<c:out value="${footer}"/>
<br>另一种没有参数的URL<br>
<c:url value="footer.jsp"/>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:catch的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:catch var="error">
<%
  Integer.parseInt("sdkfj");
%>
</c:catch>
<hr>异常:
<c:out value="${error}"/>
<hr>异常 exception.getMessage=
<c:out value="${error.message}"/>
<hr> 异常exception.getCause=
<c:out value="${error.cause}"/>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<c:set var="count" value="100"/>

<html>
<head>
  <title>JSTL:c:choose的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:choose>
    <c:when test="${count <=0}">
      <font color="blue">
    </c:when>
    <c:when test="${count<=60&&count>0}">
      <font color="red">
    </c:when>
    <c:otherwise>
      <font color="green">
    </c:otherwise>
</c:choose>
count的值是:<c:out value="${count}"/>
</font>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:c:if的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:set var="count" value="100"/>
<c:if test="${count>78}">
count>78
</c:if>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:c:import的使用</title>
</head>
<body bgcolor="#FFFFFF">
<h3>绝对路径 URL</h3>
<blockquote>
<ex:escapeHtml>
  <c:import url="http://127.0.0.1:8080/ch12/footer.jsp"/>
</ex:escapeHtml>
</blockquote>

<h3>相对路径并且传递参数到指定的URL</h3>
<blockquote>
  <c:import url="footer.jsp" charEncoding="gb2312">
  <c:param name="userName" value="hellking"/>
</c:import> 
</blockquote>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL: c:out的使用</title>
</head>
<body bgcolor="#FFFFFF">
<hr>
<c:set var="sessionAttr" value="sessionValue" scope="session"/>
<c:set var="pageAttr" value="pageValue" scope="page"/>
<c:set var="requestAttr" value="requestValue" scope="request"/>
<c:out value="以下输出的是前面设置的属性<br>" escapeXml="false"/>

<c:out value="${sessionAttr}"/>
<c:out value="${pageAttr}"/>
<c:out value="${requestAttr}"/>
<c:out value="${test_nodef}" default="没有test_nodef这个变量"/>

</html>



<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL c:param的使用</title>
</head>
<body bgcolor="#FFFFFF">

<c:redirect url="footer.jsp">
<c:param name="userName">
   hellking
</c:param>
</c:redirect>
</body>
</html>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:c:redirect的使用</title>
</head>
<body bgcolor="#cc99cc">
<c:url value="footer.jsp" var="nextpage"><c:param name="userName" value="hellking"/></c:url>
<c:redirect url="${nextpage}"/>
</body>
</html>