---
title: "Struts2内建类型转换器数据器使用简介"
author: "esffor"
date: "2008-02-27T13:39:18Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2008-02-27T13:39:18Z"
id: 168177
url: "https://esffor.iteye.com/blog/168177"
---

struts2 内建了类型转换器，可以方便的将我们在页面上输入的字符串数据转换成JavaBean中的Boolean,Float,Integer,Double,Long等数据类型，在无法转换成功时，还可以给出错误提示，非常方便

首先简历JavaBean:

 

![](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 Integer password;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](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 Integer 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(Integer 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/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)

 

编写Action,需要继承ActionSupport,在action中有两个变量，一个是User类，一个是birth属性

我们定义birth属性和user类中的password属性都必须是Integer类型

 

![](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.ActionSupport;  
![](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 extends ActionSupport...{  
![](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)private User user;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)private Integer birth;  
![](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 Integer getBirth() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return birth;  
![](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 setBirth(Integer birth) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    this.birth = birth;  
![](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)    System.out.println(this.getUser().getUsername());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    System.out.println(this.getUser().getPassword());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          return SUCCESS;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.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 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 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)

 

web.xml

 

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

 

struts.xml (WEB-INF/classes下)

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="GBK" ?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE struts PUBLIC  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;struts&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;package name="struts" extends="struts-default"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;action name="Login" class="HelloWorld.LoginAction"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;result name="input"&gt;/helloworld/index.jsp&lt;/result&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;result name="success"&gt;/helloworld/welcome.jsp&lt;/result&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)         &lt;result name="error"&gt;/helloworld/error.jsp&lt;/result&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;/action&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/package&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/struts&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

struts.properties(WEB-INF/classes下)

struts.custom.i18n.resources=messageResource

 

信息录入页面：

&lt;%@ page language="java" contentType="text/html; charset=gb2312"  
    pageEncoding="gb2312"%&gt;  
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;title&gt;&lt;/title&gt;  
  &lt;/head&gt;  
  &lt;body&gt;       
   &lt;div style="color:red"&gt;  
    &lt;s:fielderror /&gt;  
&lt;/div&gt;  
     &lt;s:form action="Login"&gt;  
       &lt;s:textfield name="user.username" key="username"&gt;&lt;/s:textfield&gt;

       &lt;s:textfield name="user.password" key="password"&gt;&lt;/s:textfield&gt;  
 &lt;s:textfield name="birth" key="password"&gt;&lt;/s:textfield&gt;  
       &lt;s:submit value="login"&gt;&lt;/s:submit&gt;  
    &lt;/s:form&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  &lt;/body&gt;  
&lt;/html&gt;

信息展示页面：

&lt;%@ page language="java" contentType="text/html; charset=gb2312"  
    pageEncoding="gb2312"%&gt;  
&lt;%@ page isELIgnored="false" %&gt;  
     
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;  
&lt;%@ taglib prefix="c" uri="[http://java.sun.com/jsp/jstl/core](http://java.sun.com/jsp/jstl/core)" %&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;title&gt;&lt;/title&gt;  
  &lt;/head&gt;  
  &lt;body&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
   &lt;body&gt;  
        转换成功!&lt;br&gt;  
  用户1的用户名为：&lt;s:property value="user.username"/&gt;&lt;br&gt;  
  用户1的密码为：&lt;s:property value="user.password"/&gt;&lt;br&gt;  
    
    &lt;/body&gt;  
&lt;/html&gt;

 

 

我们在资源文件中加入：

xwork.default.invalid.fieldvalue={0}类型转换错误，运行index.jsp.,在三个输入框中都输入admin,则会出现以下提示：

user.password字段类型转换失败  
birth字段类型转换失败

 

在资源文件中定义的是全局转换错误提示的内容，如果想对具体Action中的字段进行提示，则编写ActionName.properties,本文中对应的就是LoginAction.properties,放在和Action同一个目录下，内容格式如下：invalid.fieldvalue.birth （其中birth为action中属性名）

本文LoginAction.properties内容为：invalid.fieldvalue.birth=生日必须为数字

再次运行index.jsp.结果如下：

user.password字段类型转换失败  
生日必须位数字