Struts(二十):自定义类型转换器
2017-04-01 23:55
423 查看
[b]如何自定义类型转换器:[/b]
1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型;
2) 如何定义类型转化器?
步骤一:创建自定义类型转化器的类,并继承org.apache.struts2.util..StrutsTypeConverter类;
步骤二:配置类型转化器(包含两种方式:基于字段的配置、基于类型的配置)
官网有类型转化器的写用法向导:http://struts.apache.org/docs/type-conversion.html
备注:从官网向导中我们知道struts2.3.13之后,是支持date类型自动转化的,看到官网上很多说date类型不可以自动转化的就需要看看文档了。
XWork will automatically handle the most common type conversion for you. This includes support for converting to and from Strings for each of the following:
String
boolean / Boolean
char / Character
int / Integer, float / Float, long / Long, double / Double
dates - uses the SHORT format for the Locale associated with the current request
arrays - assuming the individual strings can be coverted to the individual items
collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created
Note that with arrays the type conversion will defer to the type of the array elements and try to convert each item individually. As with any other type conversion, if the conversion can't be performed the standard type conversion error reporting is used to indicate a problem occurred while processing the type conversion.
Enumerations
BigDecimal and BigInteger
[b]基于字段的配置[/b]
1、在字段所在的Model(可能是Action,可能是一个JavaBean)的报下,新建一个ModelClassName-conversion.properties;
2、在ModelClassName-conversion.properties内输入键值对:fieldName=类型转换器的全类名;
3、加载时:第一次使用该转换器时创建实例;
4、类型转换器在使用过程中只实例化一次,是单例的。
基于上一篇文章《Struts(十九):类型转换、类型转换错误消息及显示》中例子上修改程序:
MyAction.java
View Code
在com.dx.models包下创建Member-conversion.properties文件
[b]基于类型的配置[/b]
1、在src下新建一个xwork-conversion.properties文件;
2、在文件xwork-conversion.properties中键入键值对:待转换的类型=类型转换器的全类名;
3、加载时:在当前struts2应用程序被加载时创建实例。
备注:因此如果在web.xml配置了context-param,读取参数时,需要通过get方法中获取,否则如果在constructor中获取context-param参数,将会导致读取不到context-param配置信息。
基于上边方案二修改出解决方案三:
1、把com.dx.models包下的Member-conversion.properties转移到工程根目录(目的使其不起作用),在src下创建文件xwork-conversion.properties
2、编辑文件xwork-conversion.properties内容
1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型;
2) 如何定义类型转化器?
步骤一:创建自定义类型转化器的类,并继承org.apache.struts2.util..StrutsTypeConverter类;
步骤二:配置类型转化器(包含两种方式:基于字段的配置、基于类型的配置)
官网有类型转化器的写用法向导:http://struts.apache.org/docs/type-conversion.html
备注:从官网向导中我们知道struts2.3.13之后,是支持date类型自动转化的,看到官网上很多说date类型不可以自动转化的就需要看看文档了。
Built in Type Conversion Support
Type Conversion is implemented by XWork.XWork will automatically handle the most common type conversion for you. This includes support for converting to and from Strings for each of the following:
String
boolean / Boolean
char / Character
int / Integer, float / Float, long / Long, double / Double
dates - uses the SHORT format for the Locale associated with the current request
arrays - assuming the individual strings can be coverted to the individual items
collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created
Note that with arrays the type conversion will defer to the type of the array elements and try to convert each item individually. As with any other type conversion, if the conversion can't be performed the standard type conversion error reporting is used to indicate a problem occurred while processing the type conversion.
Enumerations
BigDecimal and BigInteger
[b]基于字段的配置[/b]
1、在字段所在的Model(可能是Action,可能是一个JavaBean)的报下,新建一个ModelClassName-conversion.properties;
2、在ModelClassName-conversion.properties内输入键值对:fieldName=类型转换器的全类名;
3、加载时:第一次使用该转换器时创建实例;
4、类型转换器在使用过程中只实例化一次,是单例的。
基于上一篇文章《Struts(十九):类型转换、类型转换错误消息及显示》中例子上修改程序:
MyAction.java
/** * @author Administrator * */ package com.dx.models; import java.awt.Point; import java.util.Date; public class Member { private Integer age; private Date birth; private Point point; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } }
View Code
在com.dx.models包下创建Member-conversion.properties文件
point=com.dx.converters.PointConverter
[b]基于类型的配置[/b]
1、在src下新建一个xwork-conversion.properties文件;
2、在文件xwork-conversion.properties中键入键值对:待转换的类型=类型转换器的全类名;
3、加载时:在当前struts2应用程序被加载时创建实例。
备注:因此如果在web.xml配置了context-param,读取参数时,需要通过get方法中获取,否则如果在constructor中获取context-param参数,将会导致读取不到context-param配置信息。
基于上边方案二修改出解决方案三:
1、把com.dx.models包下的Member-conversion.properties转移到工程根目录(目的使其不起作用),在src下创建文件xwork-conversion.properties
2、编辑文件xwork-conversion.properties内容
java.awt.Point=com.dx.converters.PointConverter
相关文章推荐
- Struts自定义类型转换器
- Struts 2自定义类型转换器
- Struts 2读书笔记-----Struts 2的自定义类型转换器
- 基于Struts 2的自定义类型转换器
- Struts 2中自定义类型转换器 TypeConverter 的应用
- Struts 2读书笔记-----Struts 2的自定义类型转换器
- Struts自定义类型转换器之字符串与复合对象之间的转换
- struts自定义类型转换器DefaultTypeConverter
- 框架学习之Struts2 第三节 请求参数的接收和自定义类型转换器
- 动态ActionForm、struts上传文件、自定义ActionForm的类型转换、Struts对ActionForm的自动搜集流程
- struts2自定义类型转换(2)基于StrutsTypeConverter
- 二、Struts 2的类型转换---1.Struts 2中实现自定义的类型转换
- 自定义类型转换器(转换器的注册方法)
- struts2自定义类型转换(2)基于StrutsTypeConverter
- Struts使用BeanUtils提供的数据类型转换器
- Struts2实现自定义转换器和全局类型转换器
- struts对comments-beanutils包的使用分析以及使用自定义类型转化器的ActionServlet
- Struts2之自定义类型转换器
- 自定义struts2.0中的转换器(converter)
- Struts使用BeanUtils提供的数据类型转换器