日期:2014-05-16  浏览次数:20369 次

jstl标签笔记
1.EL表达式

   jsp:

    <h1>Test El</h1>
    <hr>
    <h2>普通表达式</h2>
    第一种用java脚本直接访问:<%=request.getAttribute("hello") %>
    <br>
    第二种用EL表达式访问(EL表达式由$与{}组成):${hello}
    <br/>
    EL表达式的隐含对象:pageScope\requestScope\sessionScope\applicationScope:el查找顺序是从左到右进行查找
    知道找到为止,否则为空。因此可以这样直接取得:${requestScope.hello} 而如果用其他的隐含对象就取不到
    相关数据:${sessionScope.hello },但是最常用的还是第二种方式
   
    <h2>结构(采用“.”的方式进行导航,前提条件是属性中提供getter方法)</h2>
    <hr>
    姓名:${user.username }<br/>
    年龄:${user.age}<br/>
    用户组:${user.group.name }
   
    <h2>输出Map(格式map.key)</h2>
    <hr>
    用Key取得map中相应的value:${map.key1 }<br/>
    用Key取得map中相应的value:${map.key2 }<br/>
    用Key取得map中相应的value:${map.key3 }
   
    <h2>取得数组(采用“数组名[下标]”的方式)</h2>
    <hr>
    取得数组元素:${str[0]}<br/>
    取得数组元素:${str[1]}
   
    <h2>取得对象数组(采用“数组名[下标]”的方式)</h2>
    <hr>
    取得对象数组元素的属性如username(必须提供getter方法):${users[2].username }
   
    <h2>取得List(采用“List名[下标]”的方式)</h2>
    <hr>
    取得List元素的属性如username(必须提供getter方法):${lists[2].username }
   
   <h2>对运算符的支持"+,-,*,/,%,>,<,==,!="</h2>
    <hr>
    1+1=${1+1}<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
   -->
  
   <h2>测试empty(空串即“”、不存在、没有值、null等多会被认为是空的)</h2><br>
value1: ${empty value1 }<br>
value2: ${empty value2 }<br>
value3: ${empty value3 }<br>
value4: ${empty value4 }<br>
value5: ${!empty value5 }<br>

servlet:

Group group = new Group();
   group.setName("group1");
   User user = new User();
   user.setAge(10);
   user.setGroup(group);
   user.setUsername("zhangsan");
   request.setAttribute("user", user);
 
   Map map = new HashMap();
   map.put("key1", "value1");
   map.put("key2", "value2");
   map.put("key3", "value3");
   request.setAttribute("map", map);
 
   String str[]={"string1","string2"};
   request.setAttribute("str", str);
 
   User[] users= new User[3];
   for(int i=0;i<users.length;i++){
    users[i]=new User();
    users[i].setAge(i+50);
    users[i].setGroup(group);
    users[i].setUsername("name"+i);
 &nbs