---
title: "《研磨struts2》第十一章 类型转换 之 11.2 内建类型转换器 "
author: "研磨struts2"
date: "2012-05-26T09:18:54Z"
tags: []
categories: ["企业架构"]
slug: "struts2-11-2"
draft: false
lastmod: "2012-05-26T09:18:54Z"
id: 1541676
url: "https://rubbing-struts2.iteye.com/blog/1541676"
---

## 11.2  内建类型转换器

Struts2内建的类型转换器能处理绝大多数的需求，只有极特殊情况才需要自定义类型转换器。在这一节中就一起来看看Struts2的内建类型转换器都可以完成哪些工作。

### []()11.2.1简单类型

Struts2已经内置了基本数据类型及其包装类和其他一些常见的用于表示数字/日期类型的类型转换器，包括：

- int/Integer：整数型
- short/Short：短整数型
- long/Long：长整型
- float/Float：浮点型
- double/Double：双精度型
- boolean/Boolean：布尔型
- byte/Byte：字节型
- char/Character：字符型
- BigInteger：大整数型
- BigDecimal：大浮点数型
- Date：日期型

### []()11.2.2枚举类型

枚举类型往往被人们忽略，其实Struts2的Action中也可以直接使用枚举类型，其对应的参数值只需要对应枚举定义时的定义名即可。

       比如有如下的枚举定义，示例代码如下：

 

java代码：

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

1. public enum ColorEnum {
2. red,blue,green;
3. }

在Action引用的时候，只需传入枚举的定义名（red，blue或green）即可，示例代码如下：

 

java代码：

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

01. public class ConverterAction extends ActionSupport {
02. private ColorEnum color;
03. public ColorEnum getColor() {
04. return color;
05. }
06. public void setColor(ColorEnum color) {
07. this.color = color;
08. }
09. public String execute() throws Exception {
10. System.out.println("color="+color);
11. System.out.println("传入的颜色是红色吗？答案是 ："+(color==ColorEnum.red));
12. return SUCCESS;
13. }
14. }

其在struts.xml中的定义为：

 

java代码：

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

1. &lt;package name="helloworld" extends="struts-default"&gt;
2. &lt;action name="converterAction" class="cn.javass.convert.ConverterAction"&gt;
3. &lt;result&gt;/converter/success.jsp&lt;/result&gt;
4. lt;/action&gt;
5. &lt;/package&gt;

为了简单，就不去写页面了，直接通过URL来访问，使用get传参的方式，访问的URL为：“http://localhost:9080/helloworld/converterAction.action?color=red”，运行后，后台输出的结果为：

 

java代码：

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

1. red
2. true

可以看到，使用red作为参数将得到枚举中red的那个枚举值。

### []()11.2.3复合类型

这里提到的复合类型与上一节的简单类型相比，并不是Action的属性的类型变得复杂了，而是操作属性的OGNL变得复杂了，其最终操作的无非还是上面的那些简单类型。

其实，这些东西在第四章的时候都已经提到过了，只不过是在这里再次归纳总结，让大家看到类型转换器的全貌。

**1：JavaBean**

       在Action中定义一个JavaBean属性，那么访问JavaBean中的某一个属性，需要使用Action中JavaBean的属性名.JavaBean中的属性名。

       比如有长方形这个JavaBean：

 

java代码：

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

01. public class Rectangle {
02. private int width;
03. private int height;
04. public int getWidth() {
05. return width;
06. }
07. public void setWidth(int width) {
08. this.width = width;
09. }
10. public int getHeight() {
11. return height;
12. }
13. public void setHeight(int height) {
14. this.height = height;
15. }
16. }

在Action定义一个长方形类型的属性，并提供相应的getter/setter方法，示例代码如下：

 

java代码：

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

01. public class ConverterAction extends ActionSupport {
02. private Rectangle rectangle;
03. public Rectangle getRectangle() {
04. return rectangle;
05. }
06. public void setRectangle(Rectangle rectangle) {
07. this.rectangle = rectangle;
08. }
09. //其它的就省略了
10. }

需要在提交页面上用&lt;s:textfield name=” rectangle.width”/&gt;来引用Action的rectangle属性的width属性。就相当于调用这个Action的get Rectangle().setWidth()方法。

**2：数组和List—简单属性**

       使用数组和List及其相似，在提交页面上并无区别，在Action中也仅是声明的区别。比如在Action中需要得到提交页面提交的两组身高和体重的数据：

 

java代码：

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

01. public class ConverterAction extends ActionSupport {
02. private int heights\[];
03. private List&lt;Integer&gt; weights;
04. public int\[] getHeights() {
05. return heights;
06. }
07. public void setHeights(int\[] heights) {
08. this.heights = heights;
09. }
10. public List&lt;Integer&gt; getWeights() {
11. return weights;
12. }
13. public void setWeights(List&lt;Integer&gt; weights) {
14. this.weights = weights;
15. }
16. //其它的就省略了
17. }

对应的提交页面上需要添加：

 

java代码：

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

1. &lt;s:textfield name="heights"/&gt;
2. &lt;s:textfield name="heights"/&gt;
3. &lt;s:textfield name="weights\[0]"/&gt;
4. &lt;s:textfield name="weights\[1]"/&gt;

对应的提交页面上在&lt;s:textfield&gt;的name属性上写不写对应的索引都可以。Action都会正确的获取到用户的输入。

**3：数组和List—JavaBean**

       如果数组或List里面放的是JavaBean，大家不可以掉以轻心，在提交页面上的写法就不那么随便了。

       仍然使用上面长方形的JavaBean(里面有width和height两个属性)来做说明，在Action中需要接收一个长方形类型的数组，示例代码如下：

 

java代码：

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

01. public class ConverterAction extends ActionSupport {
02. private Rectangle\[] rectangles;
03. public Rectangle\[] getRectangles() {
04. return rectangles;
05. }
06. public void setRectangles(Rectangle\[] rectangles) {
07. this.rectangles = rectangles;
08. }
09. //其它的就省略了
10. }

这个时候，如果在提交页面上不用索引：

 

java代码：

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

1. &lt;s:textfield name="rectangles.height"/&gt;
2. &lt;s:textfield name="rectangles.weight"/&gt;
3. &lt;s:textfield name="rectangles.height"/&gt;
4. &lt;s:textfield name="rectangles.weight"/&gt;

在Action中得到的就会是四个对象，Struts2并不会把第一个rectangles.height和第一个rectangles.weight凑成一对，放到一个对象里，而是把四个属性都分别放到自己的对象里。

       所以，这时候，必须写索引，示例代码如下：

 

java代码：

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

1. &lt;s:textfield name="rectangles\[0].height"/&gt;
2. &lt;s:textfield name="rectangles\[0].weight"/&gt;
3. &lt;s:textfield name="rectangles\[1].height"/&gt;
4. &lt;s:textfield name="rectangles\[1].weight"/&gt;

Action中才能正确的接到两个长方形的对象。

**4：Map**

       Map与数组及其类似，同样是用来存放多个“单体”数据，只不过数组用索引来区分不同的单体，而Map用Key来区分所有的单体。可以用“Map名\[‘Key值’]”这样的OGNL来引用Map中的值。

       在Action中使用Map来存储多个长方形，多个长方形以Key区分：

 

java代码：

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

01. public class ConverterAction extends ActionSupport {
02. private Map&lt;String, Rectangle&gt; map;
03. public Map&lt;String, Rectangle&gt; getMap() {
04. return map;
05. }
06. public void setMap(Map&lt;String, Rectangle&gt; map) {
07. this.map = map;
08. }
09. //其它的就省略了
10. }

在提交页面上可以这么写：

 

java代码：

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

1. &lt;s:textfield name="map\[‘first’].height"/&gt;
2. &lt;s:textfield name="map\[‘first’].weight"/&gt;
3. &lt;s:textfield name="map\[‘second’].height"/&gt;
4. &lt;s:textfield name="map\[‘second’].weight"/&gt;

Action的map中。

**5：小节**

       这里看到了很多的复合类型：JavaBean、数组或List、Map，这些复合类型之间还可以结合使用。在前面就已经见到了JavaBean数组(Rectangle\[] rectangles)；Map里面有JavaBean(Map&lt;String, Rectangle&gt;)。反过来JavaBean里面有List或Map，甚至嵌套更多层都是可以的。

虽然看起来会很复杂，但只要牢牢掌握它们各自的访问方式，然后根据情况自由组合就可以了。基本的访问方式为：

- JavaBean：用“.”来访问自己的属性
- 数组或List：用“\[索引]”来访问自己的第几个元素
- Map：用“\[‘Key’]”来访问自己的键为Key的元素。
  
   
  
  私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列
  
  转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4096.html](http://sishuok.com/forum/blogPost/list/0/4096.html)】
  
  欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容