OGNL的用法举例。转自ITEYE

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <%@page import="com.rao.struts2.bean.Sex"%>   
  3. <%@ taglib prefix="s" uri="/struts-tags"%>   
  4. "-//W3C//DTD HTML 4.01 Transitional//EN">   
  5.   
  6.        
  7.         My JSP </span><span class="string"><span style="color:#0000ff;">'OGNL1.jsp'</span></span><span> starting page   
  8.   
  9.         "pragma" content="no-cache">   
  10.         "cache-control" content="no-cache">   
  11.         "expires" content="0">   
  12.         "keywords" content="keyword1,keyword2,keyword3">   
  13.         "description" content="This is my page">   
  14.            
  15.   
  16.        
  17.   
  18.        
  19.         <%   
  20.             request.setAttribute("req""request scope");   
  21.             request.getSession().setAttribute("sess""session scope");   
  22.             request.getSession().getServletContext().setAttribute("app",   
  23.                     "aplication scope");   
  24.         %>   
  25.     1.通过ognl表达式获取 属性范围中的值   
  26.         
      
  27.         "#request.req" />   
  28.            
  29.         "#session.sess" />   
  30.            
  31.         "#application.app" />   
  32.            
  33.         
      
  34.     2.通过"BACKGROUND-COLOR: #fafafa">ognl表达式创建list 集合 ,并且遍历出集合中的值   
  35.         
      
  36.         "list" value="{'eeeee','ddddd','ccccc','bbbbb','aaaaa'}">   
  37.         "#list" var="o">   
  38.                
  39.                
  40.                
  41.            
  42.            
  43.         
      
  44.     3.通过ognl表达式创建Map 集合 ,并且遍历出集合中的值   
  45.         
      
  46.         "map"  
  47.             value="#{'1':'eeeee','2':'ddddd','3':'ccccc','4':'bbbbb','5':'aaaaa'}">   
  48.         "#map" var="o">   
  49.                
  50.                
  51.             "key" />->"value" />   
  52.                
  53.            
  54.            
  55.         
      
  56.     4.通过ognl表达式 进行逻辑判断   
  57.         
      
  58.         if test="'aa' in {'aaa','bbb'}">   
  59.             aa 在 集合{'aaa','bbb'}中;   
  60.         if>   
  61.         else>   
  62.             aa 不在 集合{'aaa','bbb'}中;   
  63.         else>   
  64.   
  65.            
  66.   
  67.         if test="#request.req not in #list">   
  68.             不 在 集合list中;   
  69.         if>   
  70.         else>   
  71.             在 集合list中;   
  72.         else>   
  73.            
  74.         
      
  75.     5.通过ognl表达式 的投影功能进行数据筛选   
  76.         
      
  77.         "list1" value="{1,2,3,4,5}">   
  78.         "#list1.{?#this>2}" var="o">   
  79.                
  80.             ${o }   
  81.            
  82.            
  83.         
      
  84.     5.通过ognl表达式 访问某个类的静态方法和值   
  85.         
      
  86.         "@java.lang.Math@floor(32.56)" />   
  87.   
  88.         "@com.rao.struts2.action.OGNL1Action@aa" />   
  89.            
  90.            
  91.         
      
  92.     6.ognl表达式 迭代标签 详细   
  93.         
      
  94.         "list2"  
  95.             value="{'aa','bb','cc','dd','ee','ff','gg','hh','ii','jj'}">   
  96.         "1">   
  97.                
  98.                    
  99.                     索引   
  100.                    
  101.                    
  102.                     值   
  103.                    
  104.                    
  105.                     奇?   
  106.                    
  107.                    
  108.                     偶?   
  109.                    
  110.                    
  111.                     首?   
  112.                    
  113.                    
  114.                     尾?   
  115.                    
  116.                    
  117.                     当前迭代数量   
  118.                    
  119.                
  120.             "#list2" var="o" status="s">   
  121.                 "#s.even">pink">   
  122.                        
  123.                         "#s.getIndex()" />   
  124.                        
  125.                        
  126.                            
  127.                        
  128.                        
  129.                         if test="#s.odd">Yif>   
  130.                         else>Nelse>   
  131.                        
  132.                        
  133.                         if test="#s.even">Yif>   
  134.                         else>Nelse>   
  135.                        
  136.                        
  137.                         if test="#s.first">Yif>   
  138.                         else>Nelse>   
  139.                        
  140.                        
  141.                         if test="#s.isLast()">Yif>   
  142.                         else>Nelse>   
  143.                        
  144.                        
  145.                     "#s.getCount()"/>   
  146.                    
  147.                    
  148.   
  149.                
  150.            
  151.         
      
  152.         
      
  153.     7.ognl表达式:  if/else if/else 详细
      
  154.         <% request.setAttribute("aa",0); %>   
  155.         if test="#request.aa>=0 && #request.aa<=4">   
  156.             在0-4之间;   
  157.         if>   
  158.         "#request.aa>=4 && #request.aa<=8">   
  159.             在4-8之间;   
  160.            
  161.         else>   
  162.             大于8;   
  163.         else>   
  164.         
      
  165.         
      
  166.     8.ognl表达式: url 详细
      
  167.         <% request.setAttribute("aa","sss"); %>   
  168.         "testAction" namespace="/aa/bb">   
  169.             "aa" value="#request.aa">   
  170.             "id">100   
  171.            
  172.         
      
  173.         "myurl" value="'http://www.baidu.com'">   
  174.         value以字符处理:   "#myurl">
      
  175.         value明确指定以ognl表达式处理:    "%{#myurl}">   
  176.         
      
  177.         
      
  178.     9.ognl表达式: checkboxlist 详细
      
  179.         1> .list 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
  180.         name:checkboxlist的名字
      
  181.         list:checkboxlist要显示的列表
      
  182.         value:checkboxlist默认被选中的选项,checked=checked
      
  183.         "checkbox1" list="{'上网','看书','爬山','游泳','唱歌'}" value="{'上网','看书'}" >   
  184.         
      
  185.         以上生成代码:
      
  186.            </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox1"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"上网"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox1-1"</span></span><span> checked=</span><span class="string"><span style="color:#0000ff;">"checked"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox1-1"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>上网</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox1"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"看书"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox1-2"</span></span><span> checked=</span><span class="string"><span style="color:#0000ff;">"checked"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox1-2"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>看书</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox1"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"爬山"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox1-3"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox1-3"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>爬山</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox1"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"游泳"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox1-4"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox1-4"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>游泳</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox1"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"唱歌"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox1-5"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox1-5"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>唱歌</label>"   </span></li> <li><span>           
  187.         2> .Map 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
  188.         name:checkboxlist的名字
      
  189.         list:checkboxlist要显示的列表
      
  190.         listKey:checkbox 的value的值
      
  191.         listValue:checkbox 的lablel(显示的值)
      
  192.         value:checkboxlist默认被选中的选项,checked=checked
      
  193.         "checkbox2" list="#{1:'上网',2:'看书',3:'爬山',4:'游泳',5:'唱歌'}" listKey="key" listValue="value" value="{1,2,5}" >   
  194.         
      
  195.         以上生成代码:
      
  196.            </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox2"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"1"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox2-1"</span></span><span> checked=</span><span class="string"><span style="color:#0000ff;">"checked"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox2-1"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>上网</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox2"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"2"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox2-2"</span></span><span> checked=</span><span class="string"><span style="color:#0000ff;">"checked"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox2-2"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>看书</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox2"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"3"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox2-3"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox2-3"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>爬山</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox2"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"4"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox2-4"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox2-4"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>游泳</label>   </span></li> <li><span>            <input type=</span><span class="string"><span style="color:#0000ff;">"checkbox"</span></span><span> name=</span><span class="string"><span style="color:#0000ff;">"checkbox2"</span></span><span> value=</span><span class="string"><span style="color:#0000ff;">"5"</span></span><span> id=</span><span class="string"><span style="color:#0000ff;">"checkbox2-5"</span></span><span> checked=</span><span class="string"><span style="color:#0000ff;">"checked"</span></span><span>/>   </span></li> <li><span>            <label </span><span class="keyword"><strong><span style="color:#7f0055;">for</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkbox2-5"</span></span><span> </span><span class="keyword"><strong><span style="color:#7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color:#0000ff;">"checkboxLabel"</span></span><span>>唱歌</label>   </span></li> <li><span>           
  197.         
      
  198.     10.ognl表达式: s:radio 详细
      
  199.         <%   
  200.             Sex sex1 = new Sex(1,"男");    
  201.             Sex sex2 = new Sex(2,"女");   
  202.             List list = new ArrayList();    
  203.             list.add(sex1);   
  204.             list.add(sex2);   
  205.             request.setAttribute("sexs",list);    
  206.         %>   
  207.         这个与checkboxlist差不多;
      
  208.         1>.如果集合为javabean:"sex" list="#request.sexs" listKey="id" listValue="name">
      
  209.         2>.如果集合为list:"sexList" list="{'男','女'}">
      
  210.         3>.如果集合为map:"sexMap" list="#{1:'男',2:'女'}" listKey="key" listValue="value">
      
  211.         
      
  212.     11.ognl表达式: s:select 详细
      
  213.         这个与s:checkboxlist差不多;
      
  214.         1>.如果集合为javabean:"sex" list="#request.sexs" listKey="id" listValue="name">
      
  215.         2>.如果集合为list:"sexList" list="{'男','女'}">
      
  216.         3>.如果集合为map:"sexMap" list="#{1:'男',2:'女'}" listKey="key" listValue="value">
      
  217.     到此主要的ognl标签已经介绍完毕...由于表单标签相对简单不介绍了....   
  218.        
  219.   
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.rao.struts2.bean.Sex"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


	
		My JSP 'OGNL1.jsp' starting page

		
		
		
		
		
		

	

	
		<%
			request.setAttribute("req", "request scope");
			request.getSession().setAttribute("sess", "session scope");
			request.getSession().getServletContext().setAttribute("app",
					"aplication scope");
		%>
	1.通过ognl表达式获取 属性范围中的值
		




2.通过ognl表达式创建list 集合 ,并且遍历出集合中的值



3.通过ognl表达式创建Map 集合 ,并且遍历出集合中的值
->


4.通过ognl表达式 进行逻辑判断
aa 在 集合{'aaa','bbb'}中; aa 不在 集合{'aaa','bbb'}中;
不 在 集合list中; 在 集合list中;

5.通过ognl表达式 的投影功能进行数据筛选
${o }


5.通过ognl表达式 访问某个类的静态方法和值



6.ognl表达式 迭代标签 详细
pink">
索引 奇? 偶? 首? 尾? 当前迭代数量
Y N Y N Y N Y N


7.ognl表达式: if/else if/else 详细
<% request.setAttribute("aa",0); %> 在0-4之间; 在4-8之间; 大于8;

8.ognl表达式: url 详细
<% request.setAttribute("aa","sss"); %> 100
value以字符处理:
value明确指定以ognl表达式处理:

9.ognl表达式: checkboxlist 详细
1> .list 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
name:checkboxlist的名字
list:checkboxlist要显示的列表
value:checkboxlist默认被选中的选项,checked=checked

以上生成代码:
<input type="checkbox" name="checkbox1" value="上网" id="checkbox1-1" checked="checked"/> <label for="checkbox1-1" class="checkboxLabel">上网</label> <input type="checkbox" name="checkbox1" value="看书" id="checkbox1-2" checked="checked"/> <label for="checkbox1-2" class="checkboxLabel">看书</label> <input type="checkbox" name="checkbox1" value="爬山" id="checkbox1-3"/> <label for="checkbox1-3" class="checkboxLabel">爬山</label> <input type="checkbox" name="checkbox1" value="游泳" id="checkbox1-4"/> <label for="checkbox1-4" class="checkboxLabel">游泳</label> <input type="checkbox" name="checkbox1" value="唱歌" id="checkbox1-5"/> <label for="checkbox1-5" class="checkboxLabel">唱歌</label>" 2> .Map 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
name:checkboxlist的名字
list:checkboxlist要显示的列表
listKey:checkbox 的value的值
listValue:checkbox 的lablel(显示的值)
value:checkboxlist默认被选中的选项,checked=checked

以上生成代码:
<input type="checkbox" name="checkbox2" value="1" id="checkbox2-1" checked="checked"/> <label for="checkbox2-1" class="checkboxLabel">上网</label> <input type="checkbox" name="checkbox2" value="2" id="checkbox2-2" checked="checked"/> <label for="checkbox2-2" class="checkboxLabel">看书</label> <input type="checkbox" name="checkbox2" value="3" id="checkbox2-3"/> <label for="checkbox2-3" class="checkboxLabel">爬山</label> <input type="checkbox" name="checkbox2" value="4" id="checkbox2-4"/> <label for="checkbox2-4" class="checkboxLabel">游泳</label> <input type="checkbox" name="checkbox2" value="5" id="checkbox2-5" checked="checked"/> <label for="checkbox2-5" class="checkboxLabel">唱歌</label>
10.ognl表达式: s:radio 详细
<% Sex sex1 = new Sex(1,"男"); Sex sex2 = new Sex(2,"女"); List list = new ArrayList(); list.add(sex1); list.add(sex2); request.setAttribute("sexs",list); %> 这个与checkboxlist差不多;
1>.如果集合为javabean:
2>.如果集合为list:
3>.如果集合为map:

11.ognl表达式: s:select 详细
这个与s:checkboxlist差不多;
1>.如果集合为javabean:
2>.如果集合为list:
3>.如果集合为map:
到此主要的ognl
标签已经介绍完毕...由于表单标签相对简单不介绍了....

 

附上以上代码运行结果图片:见附件

 



 

 

你可能感兴趣的:(OGNL的用法举例。转自ITEYE)