---
title: "Struts2实现自定义转换器和全局类型转换器"
author: "jandroid"
date: "2011-10-31T15:32:00Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2011-11-04T13:09:38Z"
id: 1235226
url: "https://jandroid.iteye.com/blog/1235226"
---

在action中有个字段是birthday是date类型，当我们传值时必须写成1999-01-01它才会自动类型转换，但在有些时候有这样的需求要写成19990101这样默认就不会进行自动类型转换，这样我们可以写一个自定义类型转换器，实现转换需求。

Struts2有两种类型转换器：

**局部：对某个action转换类型起作用**

首先要自定义类型转换器：

进行注册：

创建HelloWorldAction-conversion.properties文件HelloWorldAction是要注册的action后边是规定写法

文件内容是Action中哪个属性要进行类型转换并只向的内容转换器：

birthday=cn.test.type.converter.DateTypeConverter

**全局：对所有action转换类型起作用**

在src下创建xwork-conversion.properties文件

内容为：java.util.Date=cn.test.type.converter.DateTypeConverter