Struts迭代器(iterator)遍历List常用的4种例子



https://www.cnblogs.com/jessefeng/p/3295891.html

【摘要】本文主要介绍及演示了Struts迭代器(iterator)遍历List常用的4种例子,基于MyEclipse开发环境,重点关注前后端代码的实现,给出后端java代码、前段struts标签代码,主要有如下4个例子:

1. 遍历List

2. 遍历List>
3. 遍历UserEntity
4. 遍历List

例子的详细代码请参考附件1,给出两个4个例子的运行截图(如下图1):
 
                   图1:遍历Listd的4个例子运行结果
关键词:Struts标签,iterator迭代器,List,List>

 

1、遍历List  

(1)后台TestAction.java 部分代码: 



1         // TODO 给list赋值
2         if (list==null) {
3             list=new ArrayList();
4             list.add("刘备");
5             list.add("关羽");
6             list.add("张飞");
7             list.add("赵云");
8         }


(2)前台jsp+s标签部分源代码:



 1                     <%--List遍历--%>
 2                    

 3                         cellspacing="1">
 4                        
 7                        
 8                             <%--
 9                         说明:
10                              
11                             --%>
12                            
13                                

18                            
19                        
20                    

 5                             List<String>遍历
 6                        

14                                    
15                                        
16                                    

17                                


注:遍历List运行结果参考上图1(左上)

2、遍历List>

(1)后台TestAction.java 部分代码: 



 1         // TODO 给listList赋值(二维列表List)
 2         if (listList==null) {
 3             listList=new ArrayList>();
 4             List li=new ArrayList();
 5             li.add("姓名");
 6             li.add("势力");
 7             li.add("职务");
 8             listList.add(li);
 9             /*List为引用类型(以地址方式保存一维List对象),
10             添加不同List到二维列表需再实例化    */       
11             li=new ArrayList();
12             li.add("刘备");
13             li.add("蜀");
14             li.add("老大");
15             listList.add(li);
16             li=new ArrayList();
17             li.add("关羽");
18             li.add("蜀");
19             li.add("老2");
20             listList.add(li);
21             li=new ArrayList();
22             li.add("张飞");
23             li.add("蜀");
24             li.add("老3");
25             listList.add(li);
26             li=new ArrayList();
27             li.add("赵云");
28             li.add("蜀");
29             li.add("大将");
30             listList.add(li);


(2)前台jsp+s标签部分源代码:



 1     <%--List>遍历--%>
 2                    

 3                         cellspacing="1">
 4                        
 7                        
 8                            

 9                                
10                                    

13                                
14                            
15                        
16                    

 5                             List<List<String>>遍历如下:
 6                        

11                                        
12                                    


注:遍历List>运行结果参考上图1(右上)

3、遍历UserEntity

(1)后台TestAction.java 部分代码: 


1 // TODO 给UserEntity赋值
2             if (userEntity==null) {
3                 userEntity=new UserEntity("诸葛亮","男",37);
4             }

(2)前台jsp+s标签部分源代码:



 1                     <%--UserEntity遍历如下:--%>
 2                    

 3                         cellspacing="1">
 4                        
 7                        
 8                             <%----%>
 9                            
12                            
15                            
18                        
19                    

 5                             UserEntity遍历如下:
 6                        

10                                
11                            

13                                
14                            

16                                
17                            


注:遍历UserEntity运行结果参考上图1(左下)

4、遍历List

(1)后台TestAction.java 部分代码: 



 1 // TODO 给List赋值
 2     if (users==null) {
 3         users=new ArrayList ();
 4         UserEntity user=new UserEntity("张飞","男",18);
 5         users.add(user);
 6         user=new UserEntity("李斯","男",19);
 7         users.add(user);
 8         user=new UserEntity("王武","男",20);
 9         users.add(user);
10         user=new UserEntity("小乔","女",21);
11         users.add(user);
12     }


(2)前台jsp+s标签部分源代码:



 1 <%-- List遍历如下:--%>
 2                    

 3                         cellspacing="1">
 4                        
 7                        
 8                            
11                            
14                            
17                        
18                        
19                             <%--
20        
21         users:对应后台Action类的List users;
22         --%>
23                            

24                                 <%----%>
25                                
28                                
31                                
34                            
35                        
36                    

 5                             List<UserEntity>遍历如下:
 6                        

 9                                 姓名
10                            

12                                 性别
13                            

15                                 年龄
16                            

26                                    
27                                

29                                    
30                                

32                                    
33                                


注:遍历List运行结果参考上图1(右下)

5、用法比较

(1)比较List和UserEntity

  它们遍历显示效果类似,但是细心的朋友会发现,它们主要有以下区别:
  区别1:List的遍历需要iterator迭代器如,且不需要value属性直接用获取列表元素;
  区别2:UserEntity刚相反,不需要iterator迭代器,但需要value属性来获取列表元素。

(2)比较List>和List

  它们遍历显示效果也类似,它们也有以下区别:
  区别1:List>由于列表没有属性需要借助迭代器的属性status="st",
  如:



 
   
       
         
       


  区别2:List则简单多,借用迭代器iterator的id、value属性,再用获取

  如:



  
      
        
      
      
        
      
      
        
      
  

你可能感兴趣的:(java)