EL表达式实例

EL 表达式
EL隐式对象、存取器、运算符
----
今天刚看了《尚学堂_王勇_Struts_JSTL_EL表达式》视频,其中的实例分享给大家。
说明:网上的语言讲解特多,这里只举些实例。
--------------------

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
  --> 
  <li>测试empty</li><br>
  value1:${empty value1 }<br>
  value2:${empty value2 }<br>
  value3:${empty value3 }<br>
  value4:${empty value4 }<br>
  value4:${!empty value4 }<br>
--------------------------------------------页面输出
.el表达式对运算符的支持
1+2=3
10/5=2.0
10 div 5=2.0
10%3=1
10 mod 3=1

.测试empty
value1:true
value2:true
value3:true
value4:false
value4:true

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值