`
还有也许
  • 浏览: 164306 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

EL 表达式学习

    博客分类:
  • JSP
阅读更多

了解EL表达式:

要了解EL表达式首先要了解JSTL。JSTL即JSTL就是JSP Standard Tag Library。

JSTL 有两个版本:

Standard-1.0 Taglib 、 Standard-1.1 Taglib
Standard-1.0 Taglib ( JSTL1.0 )支持 Servlet2.3 和 JSP1.2 规范, Web 应用服务器 Tomcat4 支持这些规范,而它的发布也在 Tomcat 4.1.24 测试通过了。
Standard-1.1 Taglib ( JSTL1.1 )支持 Servlet2.4 和 JSP2.0 规范, Web 应用服务器 Tomcat5 支持这些规范,它的发布在 Tomcat 5.0.3 测试通过了。

其中大家主要使用的是SUN发布的Standard-1.11.1版本为主。

JSTL 标签 库由标签库和 EL(Expression Language)  表达式语言两个部分组成。即EL表达式是JSTL中的一部分,不过自从发布了1.1版本之后,EL 表达式已经可以单独使用了。

EL表达式有什么方便的呢?

举个例子:

Web 服务器对于 request 请求参数通常会以 String 类型来发送,在得到时使用的 Java 语言脚本就应该是 request.getParameter(“XXX”) ,这样的话,对于实际应用还必须进行强制类型转换。而 EL 就将用户从这种类型转换的繁琐工作脱离出来,允许用户直接使用 EL 表达式取得的值,而不用关心它是什么类型。

一个 EL 表达式包含变量和操作符两个内容。

1 .默认变量 pageScope 、 requestScope 、 sessionScope 、 applicationScope
这 4 个默认变量包含 Scope 作用范围的参数集合,相当于被保存在 java.util.Map 中的某个参数。

下面看个小例子:

使用 sessionScope 变量的 EL 表达式
<%request.getSession().setAttribute("sampleValue", new Integer(10));%>
${sessionScope.sampleValue}
取得保存在 Session 中参数的 sessionScope 变量的 EL 表达式,“ . ”是 property 访问操作符,在这里表示从 Session 中取得“键”为“ sampleValue ”的参数,并显示出来。显示结果为“ 10 ”。

2 .默认变量 param 、 paramValues
这两个默认变量包含请求参数的集合, param 表明请求包含的参数为单一控件, paramValues 表明请求包含的参数为控件数组。

下面是一些小例子,一看就会了:

1.普通字符串 

request.setAttribute("hello", "hello world"); 

---------------------------------------------El表达式获取 
<li>普通字符串</li><br> 
hello(jsp脚本):<%=request.getAttribute("hello") %><br> 
hello(el表达式,el表达式的使用方法$和{}):${hello }<br> 
hello(el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope, 
<br> 如果未指定scope,它的搜索顺序pageScope~applicationScope):${requestScope.hello }<br> 
hello(el表达式,scope=session):${sessionScope.hello }<br> 

--------------------------------------------页面输出 
.普通字符串 
hello(jsp脚本):hello world 
hello(el表达式,el表达式的使用方法$和{}):hello world 
hello(el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope, 
如果未指定scope,它的搜索顺序为pageScope~applicationScope):hello world 
hello(el表达式,scope=session): 
<><><><><><><><><><><><><><><><><><><> 

2.结构 

Group group = new Group(); 
group.setName("尚学堂"); 

User user = new User(); 
   user.setUsername("张三"); 
   user.setAge(18); 
   user.setGroup(group); 
  
request.setAttribute("user", user); 

---------------------------------------------El表达式获取 
<li>结构,采用.进行导航,也称存取器</li><br> 
姓名:${user.username }<br> 
年龄:${user.age }<br> 
所属组:${user.group.name }<br> 

--------------------------------------------页面输出 
.结构,采用.进行导航,也称存取器 
姓名:张三 
年龄:18 
所属组:尚学堂 
<><><><><><><><><><><><><><><><><><><> 

3.map 
Map mapValue  = new HashMap(); 
   mapValue.put("key1", "value1"); 
   mapValue.put("key2", "value2"); 

request.setAttribute("mapvalue", mapValue); 


---------------------------------------------El表达式获取 
<li>输出map,采用.进行导航,也称存取器</li><br> 
mapvalue.key1:${mapvalue.key1 }<br> 
mapvalue.key2:${mapvalue.key2 }<br> 

--------------------------------------------页面输出 
.输出map,采用.进行导航,也称存取器 
mapvalue.key1:value1 
mapvalue.key2:value2 
<><><><><><><><><><><><><><><><><><><> 

4.字符串数组 
String[] strArray = new String[]{"a", "b", "c"}; 
request.setAttribute("strarray", strArray); 

User[] users = new User[10]; 
  for (int i=0; i<10; i++) { 
User u = new User(); 
u.setUsername("U_" + i); 
users[i] = u; 
  } 
request.setAttribute("users", users); 

---------------------------------------------El表达式获取 
<li>输出对象数组,采用[]和下标</li><br> 
userarray[3].username:${users[2].username }<br> 

--------------------------------------------页面输出 
.输出对象数组,采用[]和下标 
userarray[3].username:U_2 
<><><><><><><><><><><><><><><><><><><> 

5.ArrayList 
List userList = new ArrayList(); 
   for (int i=0; i<10; i++) { 
User uu = new User(); 
uu.setUsername("UU_" + i); 
userList.add(uu); 
   } 
request.setAttribute("userlist", userList); 

---------------------------------------------El表达式获取 
<li>输出list,采用[]和下标</li><br> 
userlist[5].username:${userlist[4].username }<br> 

--------------------------------------------页面输出 
输出list,采用[]和下标 
userlist[5].username:UU_4 
<><><><><><><><><><><><><><><><><><><> 

6.empty 
request.setAttribute("value1", null); 
request.setAttribute("value2", ""); 
request.setAttribute("value3", new ArrayList()); 
request.setAttribute("value4", "123456"); 

---------------------------------------------El表达式获取 
<li>el表达式对运算符的支持</li><br> 
1+2=${1+2 }<br> 
10/5=${10/5 }<br> 
10 div 5=${10 div 5 }<br> 
10%3=${10 % 3 }<br> 
10 mod 3=${10 mod 3 }<br> 
<!-- 
   ==/eq 
   !=/ne 
   </lt 
   >/gt 
   <=/le 
   >=/ge 
   &&/and 
   ||/or 
   !/not 
   //div 
   %/mod 
  -->  

 

以上就是看别人的帖总结的一些。不敢当作自己的成果。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics