---
title: "Struts2数据转换实例之数组转换"
author: "esffor"
date: "2008-02-25T14:40:31Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2008-02-25T14:40:31Z"
id: 168181
url: "https://esffor.iteye.com/blog/168181"
---

MVC的设计模式，存在从用户WEB页面输入和实际JavaBean属性之间的自动类型转换问题

功能需求：页面上输入“用户名，密码”，将前半部分自动映射到User类的username属性中，将后半部分自动映射到User类的password属性中

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 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 User user;  
![](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 User getUser() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return user;  
![](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 setUser(User user) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.user = user;  
![](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/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      if(this.getUser().getUsername().equals("admin")&&this.getUser().getPassword().equals("1234"))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          ActionContext.getContext().getSession().put("user", this.getUser().getUsername());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          BookService bs=new BookService();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          this.getUser().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的类型转换器，实现从String到User的双向转换

 

![](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.Map;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import ognl.DefaultTypeConverter;  
![](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 UserConverter extends DefaultTypeConverter ...{  
![](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)          public Object convertValue(Map context, Object value, Class toType)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if (toType == User.class )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    String\[] params = (String\[])value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    User user = new User();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    String\[] userValues = params\[0].split(",");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    user.setUsername(userValues\[0]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    user.setPassword(userValues\[1]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    return user;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                else if (toType == String.class )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    User user = (User) value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    return "&lt;" + user.getUsername() + "," + user.getPassword() + "&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return null ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

转换器配置文件（命名规则为ActionName-convrsion.proipertoes,如果是全局的，命名为xwork.conversion.properties）,局部配置文件放在和Action同一个目录下，如果是全局的配置文件，则放到WEB-INF/classes下

user=HelloWorld.UserConverter  （Action中的user对象使用UserConverter转换器）

 

web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.5"  
 xmlns="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)  
 [http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd)"&gt;  
 &lt;filter&gt;  
   &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
   &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;  
 &lt;/filter&gt;  
 &lt;filter-mapping&gt;  
   &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
   &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
 &lt;/filter-mapping&gt;  
&lt;/web-app&gt;

 

struts.xml(WEB-INF/classes下)

&lt;?xml version="1.0" encoding="GBK" ?&gt;  
&lt;!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
        "[http://struts.apache.org/dtds/struts-2.0.dtd](http://struts.apache.org/dtds/struts-2.0.dtd)"&gt;

&lt;struts&gt;  
 &lt;package name="struts" extends="struts-default"&gt;  
    &lt;action name="Login" class="HelloWorld.LoginAction"&gt;  
      &lt;result name="input"&gt;/helloworld/index.jsp&lt;/result&gt;  
      &lt;result name="success"&gt;/helloworld/welcome.jsp&lt;/result&gt;  
       
      &lt;result name="error"&gt;/helloworld/error.jsp&lt;/result&gt;  
    &lt;/action&gt;  
 &lt;/package&gt;  
&lt;/struts&gt;

 

struts.properties (WEB-INF/classes下)

struts.custom.i18n.resources=messageResource

 

messageResource.properties

pass.required = \\u5BC6\\u7801\\u5FC5\\u987B\\u8F93\\u5165  
password = \\u7528\\u6237\\u5BC6\\u7801  
user.required = \\u7528\\u6237\\u540D\\u5FC5\\u987B\\u8F93\\u5165  
username = \\u7528\\u6237\\u59D3\\u540D