`
hl756799782
  • 浏览: 74892 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

王勇视频El表达式详解例子(转载)

阅读更多

原文链接:http://suo.iteye.com/blog/407690

 

对JSTL中的EL表达式做下测试(具体过程可以参见尚学堂的视频)

第一步:添加JstlElAction类

Java代码 复制代码 收藏代码
  1. package com.bjsxt.struts;   
  2. /**  
  3.  * 测试EL表达式  
  4.  */  
  5. public class JstlElAction extends Action {   
  6.   
  7.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  8.             HttpServletRequest request, HttpServletResponse response)   
  9.             throws Exception {   
  10.         //普通字符串   
  11.         request.setAttribute("hello""hello world");   
  12.            
  13.         //结构   
  14.         Group group = new Group();   
  15.         group.setName("尚学堂");   
  16.            
  17.         User user = new User();   
  18.         user.setUsername("张三");   
  19.         user.setAge(18);   
  20.         user.setGroup(group);   
  21.            
  22.         request.setAttribute("user", user);   
  23.            
  24.         //map   
  25.         Map mapValue  = new HashMap();   
  26.         mapValue.put("key1""value1");   
  27.         mapValue.put("key2""value2");   
  28.            
  29.         request.setAttribute("mapvalue", mapValue);   
  30.            
  31.         //字符串数组   
  32.         String[] strArray = new String[]{"a""b""c"};   
  33.         request.setAttribute("strarray", strArray);   
  34.            
  35.         User[] users = new User[10];   
  36.         for (int i=0; i<10; i++) {   
  37.             User u = new User();   
  38.             u.setUsername("U_" + i);   
  39.             users[i] = u;   
  40.         }   
  41.         request.setAttribute("users", users);   
  42.            
  43.         List userList = new ArrayList();   
  44.         for (int i=0; i<10; i++) {   
  45.             User uu = new User();   
  46.             uu.setUsername("UU_" + i);   
  47.             userList.add(uu);   
  48.         }   
  49.         request.setAttribute("userlist", userList);   
  50.            
  51.         //empty   
  52.         request.setAttribute("value1"null);   
  53.         request.setAttribute("value2""");   
  54.         request.setAttribute("value3"new ArrayList());   
  55.         request.setAttribute("value4""123456");   
  56.                    
  57.         //html   
  58.         request.setAttribute("htmlValue""<font color='red'>html</font>");   
  59.                    
  60.         return mapping.findForward("success");   
  61.     }      
  62. }  

第二步:添加测试的jsp页面(jstl_el.jsp)

Html代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  7. <title>测试EL表达式</title>  
  8. </head>  
  9. <body>  
  10.     <h1>测试EL表达式</h1><br>  
  11.     <hr>  
  12.     <li>普通字符串</li><br>  
  13.     hello(jsp脚本):<%=request.getAttribute("hello") %><br>  
  14.     hello(el表达式,el表达式的使用方法$和{}):${hello }<br>  
  15.     hello(el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope,<br> 如果未指定scope,它的搜索顺序为pageScope~applicationScope):${requestScope.hello }<br>  
  16.     hello(el表达式,scope=session):${sessionScope.hello }<br>  
  17.     <p>  
  18.     <li>结构,采用.进行导航,也称存取器</li><br>  
  19.     姓名:${user.username }<br>  
  20.     年龄:${user.age }<br>  
  21.     所属组:${user.group.name }<br>  
  22.     <p>  
  23.     <li>输出map,采用.进行导航,也称存取器</li><br>  
  24.     mapvalue.key1:${mapvalue.key1 }<br>  
  25.     mapvalue.key2:${mapvalue.key2 }<br>  
  26.     <p>  
  27.     <li>输出数组,采用[]和下标</li><br>  
  28.     strarray[2]:${strarray[1] }<br>  
  29.     <p>  
  30.     <li>输出对象数组,采用[]和下标</li><br>  
  31.     userarray[3].username:${users[2].username }<br>  
  32.     <p>  
  33.     <li>输出list,采用[]和下标</li><br>  
  34.     userlist[5].username:${userlist[4].username }<br>  
  35.     <p>  
  36.     <li>el表达式对运算符的支持</li><br>  
  37.     1+2=${1+2 }<br>  
  38.     10/5=${10/5 }<br>  
  39.     10 div 5=${10 div 5 }<br>  
  40.     10%3=${10 % 3 }<br>  
  41.     10 mod 3=${10 mod 3 }<br>  
  42.     <!--  
  43.          ==/eq   
  44.          !=/ne    
  45.          </lt   
  46.          >/gt   
  47.          <=/le   
  48.          >=/ge   
  49.          &&/and   
  50.          ||/or   
  51.          !/not   
  52.          //div   
  53.          %/mod   
  54.      -->     
  55.      <li>测试empty</li><br>  
  56.      value1:${empty value1 }<br>  
  57.      value2:${empty value2 }<br>  
  58.      value3:${empty value3 }<br>  
  59.      value4:${empty value4 }<br>  
  60.      value4:${!empty value4 }<br>  
  61.         
  62.      <p>  
  63.     <li>测试html输出--self</li><br>  
  64.     html:${htmlValue}<br>  
  65.     <p>  
  66.         
  67. </body>  
  68. </html>  

 

第三步:显示出来的结果如下:

 

 

 

特别说明:本人转载文章纯为技术学习,总结经验,并无其他目的,若有他人继续转载,请链接原作者的地址,而不是本文的地址,以示对作者的尊重。最后对原作者的辛勤劳动表示感谢!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics