---
title: "Struts2的Collection和Map支持"
author: "esffor"
date: "2008-02-25T17:17:59Z"
tags: []
slug: "struts2-collection-map"
draft: false
lastmod: "2008-02-25T17:17:59Z"
id: 168178
url: "https://esffor.iteye.com/blog/168178"
---

struts2提供了从页面直接封装到List的功能，实现起来有两个办法

1.使用泛型定义集合中的对象类型  
2.配合配置文件定义集合中的对象类型

User.java

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package HelloWorld;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class User ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private String password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private String\[] books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      public String\[] getBooks() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public void setBooks(String\[] books) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.books = books;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public String getUsername() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public void setUsername(String username) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.username = username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public String getPassword() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public void setPassword(String password) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.password = password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 LoginAction.java （不使用泛型，使用配置文件定义集合中对象的类型）

 

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package HelloWorld;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.List;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.Action;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import com.opensymphony.xwork2.ActionContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class LoginAction implements Action...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)private String tip;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)private List users;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public List getUsers() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return users;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setUsers(List users) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.users = users;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String execute() throws Exception...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //用第一个user做逻辑判断  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      if(((User)this.getUsers().get(0)).getUsername().equals("admin")&&((User)this.getUsers().get(0)).getPassword().equals("1234"))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          ActionContext.getContext().getSession().put("user", ((User)this.getUsers().get(0)).getUsername());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          BookService bs=new BookService();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          ((User)this.getUsers().get(0)).setBooks(bs.getBooks());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this.setTip("welcome welcome");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return SUCCESS;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      }else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return ERROR;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getTip() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return tip;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setTip(String tip) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.tip = tip;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

LoginAction\_conversion.properties (放在和LoginAction同目录)

Element\_users=HelloWorld.User 

表示集合中的Element类型是User,如果是Map类型，则还需要定义Key\_users=\*\*\**

 

index.jsp

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page language="java" contentType="text/html; charset=gb2312"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    pageEncoding="gb2312"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ taglib prefix="s" uri="/struts-tags" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;head&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;title&gt;&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;body&gt;        
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;s:form action="Login"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:textfield name="users\[0].username" key="username"&gt;&lt;/s:textfield&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:textfield name="users\[0].password" key="password"&gt;&lt;/s:textfield&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:textfield name="users\[1].username" key="username"&gt;&lt;/s:textfield&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:textfield name="users\[1].password" key="password"&gt;&lt;/s:textfield&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;s:submit value="login"&gt;&lt;/s:submit&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;/s:form&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

如果使用泛型定义List users，则可以省略LoginAction-conversion.properties文件

private List&lt;List&gt; users