OGNL遍历list、map的常用三种方法

 1 package com.mylife.po;
 2 
 3 public class User {
 4     
 5     private String uname;
 6     private String pwd;
 7     public String getUname() {
 8         return uname;
 9     }
10     public void setUname(String uname) {
11         this.uname = uname;
12     }
13     public String getPwd() {
14         return pwd;
15     }
16     public void setPwd(String pwd) {
17         this.pwd = pwd;
18     }
22 }

 以上是po类。

以下是jsp页面的具体ognl操作。

 

  1 <%@page import="com.mylife.po.User"%>
  2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  3 <%@ taglib prefix="s" uri="/struts-tags" %>
  4 <%
  5 String path = request.getContextPath();
  6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7 %>
  8 
  9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 10 <html>
 11   <head>
 12     <base href="<%=basePath%>">
 13     
 14     <title>My JSP 'tags.jsp' starting page</title>
 15     
 16     <meta http-equiv="pragma" content="no-cache">
 17     <meta http-equiv="cache-control" content="no-cache">
 18     <meta http-equiv="expires" content="0">    
 19     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 20     <meta http-equiv="description" content="This is my page">
 21     <!--
 22     <link rel="stylesheet" type="text/css" href="styles.css">
 23     -->
 24 
 25 
 26 <%
 27     String id =request.getParameter("id");
 28     request.setAttribute("id", id);
 29     
 30     User u1=new User();
 31     u1.setUname("cherry_1");
 32     u1.setPwd("33");
 33     User u2=new User();
 34     u2.setUname("cherry_2");
 35     u2.setPwd("23");
 36     User u3=new User();
 37     u3.setUname("cherry_3");
 38     u3.setPwd("43");
 39     
 40     List list=new ArrayList();
 41     list.add(u1);
 42     list.add(u2);
 43     list.add(u3);
 44     request.setAttribute("list", list);
 45     
 46     Map map = new HashMap();
 47     map.put("u1", u1);
 48     map.put("u2", u2);
 49     map.put("u3", u3);
 50     request.setAttribute("map", map);
 51     
 52 %>
 53   </head>
 54   
 55   <body>
 56  
 57   <h1>测试</h1>
 58    <s:if test="#request['id'] >= 18">年龄达标</s:if>
 59    <s:else>年龄不合格</s:else>
 60    
 61    <hr size="4" color="blue">
 62       <h2 style="color: red"> 迭代list-1:</h2>
 63    <s:iterator value="#request['list']" var="u">
 64            <s:property value="#u.uname"/>
 65    </s:iterator>
 66    
 67    <hr size="4" color="blue">
 68    <h2 style="color: red">迭代list-2:</h2>
 69    <s:iterator value="#request['list']">
 70            <s:property value="pwd"/>
 71    </s:iterator>
 72    
 73    <hr color="blue" size="5">
 74    <h2 style="color: red">迭代list-3:</h2>
 75    <s:iterator begin="0" end="#request['list'].size()-1" var="i">
 76            <s:property value="#request['list'].get(#i).uname"/>
 77    </s:iterator>
 78    
 79    <hr color="blue" size="5">
 80    <h2 style="color: red"> 迭代map-1:</h2>
 81    <s:iterator value="#request['map']">
 82        <s:property value="key"/>---->
 83        <s:property value="value.uname"/>,
 84        <s:property value="value.pwd"/><br>
 85    </s:iterator>
 86    
 87    <hr color="blue" size="5">
 88    <h2 style="color: red"> 迭代map-2:</h2>
 89    <s:iterator value="#request['map'].keySet()" var="i">
 90            <s:property value="#i"/>-->>>
 91            <s:property value="#request['map'].get(#i).uname"/>,
 92            <s:property value="#request['map'].get(#i).pwd"/><br>    
 93    </s:iterator>
 94    
 95     <hr color="blue" size="5">
 96    <h2 style="color: red"> 迭代map-3:</h2>
 97    <s:iterator value="#request['map'].entrySet()" var="e">
 98            <s:property value="#e.getKey()"/>-->>
 99            <s:property value="#e.getValue().uname"/>,
100            <s:property value="#e.getValue().pwd"/><br>
101    </s:iterator>
102    
103   </body>
104 </html>

 

posted @ 2015-01-21 16:03  sunshine052697  阅读(1052)  评论(1编辑  收藏  举报