---
title: "《研磨struts2》第十一章 类型转换 之 11.4 类型转换器引用方式 "
author: "研磨struts2"
date: "2012-05-28T07:52:31Z"
tags: []
categories: ["企业架构"]
slug: "struts2-11-4"
draft: false
lastmod: "2012-05-28T07:52:31Z"
id: 1542396
url: "https://rubbing-struts2.iteye.com/blog/1542396"
---

## 11.4  类型转换器引用方式

### []()11.4.1全局级类型转换器引用

在上一节中，使用的就是全局级类型转换器引用。简单回顾一下，在src下建立一个xwork-conversion.properties文件，这个文件中用“全类名=这个类对应的类型转换器全类名”，来建立类和类型转换器的关系。示例：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4101.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4101.html)[打印](http://sishuok.com/forum/blogPost/list/4101.html)

1. cn.javass.convert.Rectangle = cn.javass.convert.RectangleConverter

这样注册后，整个项目的Rectangle类都会使用RectangleConverter自定义类型转换器来处理。

### []()11.4.2类级类型转换器引用

还有更灵活的做法，可以在Action类上注册它的某个属性使用哪个类型转换器。

维持RectangleConverter自定义类型转换器的写法不变，现在删掉xwork-conversion.properties文件，以保证只有类级类型转换器起作用。

       在使用了RectangleConverter的Action同包下建立一个Action名-conversion.properties的文件，里面用“属性名=属性引用的类型转换器”的形式，来指定属性和它引用的类型转换器，对于上面的示例来说，在ConverterAction-conversion.properties只需要一行：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4101.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4101.html)[打印](http://sishuok.com/forum/blogPost/list/4101.html)

1. rectangle=cn.javass.convert.RectangleConverter

再次测试运行一下，可以看到，仍然会引用我们自定义的类型转换器。

       当然，在使用类级类型转换器引用的时候，只对这个文件名指定的Action有效，对其他Action无效。

另外一个重要的点就是：类级类型转换器的引用会覆盖全局级类型转换器的引用。

 

私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列

转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4101.html](http://sishuok.com/forum/blogPost/list/0/4101.html)】

欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容