OGNL遍历list和map 以及访问四个范围的属性值
来源:互联网 发布:高中历史辅助教程 淘宝 编辑:程序博客网 时间:2024/04/29 20:11
假定在struts2 的action的属性域中定义2个如下的List和Map变量和一个String对象:
private List<String> testlist=new ArrayList<String>(); private Map<String,String> testmap=new HashMap<String,String>(); private String testname="hello,i am in valuestack"; //直接初始化了
并同时提供getter和setter方法.
在 action中的execute方法中有如下的赋值操作:
/* * OGNL测试代码 **/HttpServletRequest request=ServletActionContext.getRequest(); //对应requestHttpSession session=request.getSession();//对应sessionServletContext context=ServletActionContext.getServletContext(); // 对应application//构造普通的字符串放到三个环境中request.setAttribute("request_name", "来自request中的name");context.setAttribute("application_name","来自application中的name");session.setAttribute("session_name", "来自session中的name");//构造一个list放到request中List<String> request_names=new ArrayList<String>();request_names.add("reqname1");request_names.add("reqname2");request_names.add("reqname3");request.setAttribute("req_names", request_names); //构造一个map放到rquest中Map<String,String> request_maps=new HashMap<String,String>(); request_maps.put("request_map1","request_value1"); request_maps.put("request_map2","request_value2"); request_maps.put("request_map3","request_value3"); request.setAttribute("req_maps", request_maps); //构造list放到session中List<String> session_names=new ArrayList<String>();session_names.add("sessionname1");session_names.add("sessionname2");session_names.add("sessionname3");session.setAttribute("session_names", session_names); //放到request中//构造一个map放到session中Map<String,String> session_maps=new HashMap<String,String>();session_maps.put("session_map1","session_value1");session_maps.put("session_map2","session_value2");session_maps.put("session_map3","session_value3"); session.setAttribute("session_maps", session_maps); //赋值到ActionContext值栈中的对象 //ActionContext中的testlist testlist.add("vs144444"); testlist.add("vs24444"); testlist.add("vs34444"); testlist.add("vs44444444"); //ActionContext中的testmap testmap.put("mp1", "vsmap1"); testmap.put("mp2", "vsmap2"); testmap.put("mp3", "vsmap3"); testmap.put("mp4", "vsmap5555"); //ActionContext中的score testscore.put("语文",95); testscore.put("数学",75); testscore.put("英语",55); testscore.put("生物",65); testscore.put("物理",85); testscore.put("化学",44);
在该action跳转后的视图jsp页面上使用struts2标签和OGNL表达式取以上的各个范围中的值的代码为:
<hr>显示来自各个范围中的值<br>actionContext中的testname:<s:property value="testname" /> <br>request中的request_name:<s:property value="#request.request_name" /><br>application中的session_name:<s:property value="#application.application_name" /><br>session中的session_name:<s:property value="#session.session_name" /><br>遍历request中的list:request_names<p><s:iterator value="#request.req_names"><s:property /><br> </s:iterator>遍历request中的map:request_maps<p><s:iterator value="#request.req_maps"><s:property value="key"/>=<s:property value="value" /><br></s:iterator>遍历session中的list:session_names<p><s:iterator value="#session.session_names"><s:property /> <br></s:iterator>遍历session中的map:session_maps<p><s:iterator value="#session.session_maps"><s:property value="key"/>=<s:property value="value" /><br></s:iterator>遍历ActionContext中的testlist=========================<p><s:iterator value="testlist" ><s:property /><br></s:iterator>遍历actionContext中的testmap<p><s:iterator value="testmap"><s:property value="key"/>=<s:property value="value" /><br></s:iterator> 直接遍历list<p><s:iterator value="{'第一年','第二年','第三年'}" ><s:property /><br></s:iterator>直接遍历map <p><s:iterator value="#{'luo1':'罗先桥1','luo2':'罗先桥2','luo3':'罗先桥3'}"><s:property value="key" />:<s:property value="value"/><br></s:iterator>遍历actionContext中的testscore这个map<p><s:iterator value="testscore"><s:property value="key"/>:<s:if test="value<60">不及格<br></s:if><s:elseif test="value>=60 && value<75">及格<br></s:elseif><s:elseif test="value>=75 && value<85">良好<br></s:elseif><s:else>优秀<br></s:else></s:iterator>
显示的结果为:
显示来自各个范围中的值actionContext中的testname:hello,i am in valuestackrequest中的request_name:来自request中的nameapplication中的session_name:来自application中的namesession中的session_name:来自session中的name遍历request中的list:request_namesreqname1reqname2reqname3遍历request中的map:request_mapsrequest_map1=request_value1request_map2=request_value2request_map3=request_value3遍历session中的list:session_namessessionname1sessionname2sessionname3遍历session中的map:session_mapssession_map1=session_value1session_map2=session_value2session_map3=session_value3遍历ActionContext中的testlist=========================vs144444vs24444vs34444vs44444444遍历actionContext中的testmapmp1=vsmap1mp2=vsmap2mp4=vsmap5555mp3=vsmap3直接遍历list第一年第二年第三年直接遍历mapluo1:罗先桥1luo2:罗先桥2luo3:罗先桥3遍历actionContext中的testscore这个map物理: 优秀语文: 优秀英语: 不及格数学: 良好化学: 不及格生物: 及格
说明几个地方:
1. struts2总是将action的实例放置在栈顶,因为action在值栈中,而值栈又是OGNL的根,所以引用action中的属性可以省略#
例如我们访问action中的属性testname:
actionContext中的testname:<s:property value="testname" /> <br>
2. 访问request,application,session以及请求参数parameters的值,需要使用#request.xxxx 这样的访问方式访问
3. 使用="{'第一年','第二年','第三年'}" 直接构造一个list ,使用=#{'luo1':'罗先桥1','luo2':'罗先桥2','luo3':'罗先桥3'} 直接构造了一个map
- OGNL遍历list和map 以及访问四个范围的属性值
- OGNL遍历list和map 以及访问四个范围的属性值
- OGNL遍历list和map 以及访问四个范围的属性值
- Ognl访问数组、List和Map
- Struts2的OGNL遍历数组、List、简单的Map
- Struts2的OGNL遍历数组、List、简单的Map
- 为何使用EL表达式能够访问valueStack中对象的属性、采用OGNL表达式创建List/Map集合对象
- List 和 Map 的遍历
- 数组、List以及Map的遍历
- list,map,set的遍历以及排序
- (6)OGNL--1:访问值栈中的普通属性、对象属性、普通方法、静态方法、自写set、map、list
- 遍历Map和List
- Struts2中使用OGNL表达式语言访问静态方法和静态属性以及我遇到的问题和解决方法
- Mybatis 中遍历map 参数中的 list 和 array 属性
- 模板中 List 和 Map 的遍历
- List 和Map的遍历方式
- List和Map的遍历操作
- List、Set和Map的遍历方法
- 黑马程序员 java基础<五>--IO流(3)
- 正则表达式:去掉数字(整数、小数)前面多余的零
- OpenGL ES1.1.12 第二章 2.10.3
- 拿数据说话,教你侦破“人人猎头”的谎言
- Python, Shell 遍历文件
- OGNL遍历list和map 以及访问四个范围的属性值
- 分享个java线程安全,自增主键id的类AtomicLong
- Android播放声音SoundPool、MediaPlayer、AudioTrack
- 常用的android弹出对话框
- Effective C++学记之07 为多态基类声明virtual析构函数
- Java API online
- 最快线程间数据交换算法,有效避免锁竞争 -- TwoQueues
- java.util包中的线程运用
- C#在WinCE平台上的相关技巧