`
qilixiang012
  • 浏览: 202803 次
文章分类
社区版块
存档分类
最新评论

struts2 自定义类型转换器

 
阅读更多
一、问题的引出
Struts2的类型转换是基于OGNL表达式的,由于请求的参数都是字符串,而JAVA 本身属于强类型的的语言,这样就需要把请求参数字符串转换成其他类型。

Struts2的类型转换器都需要实现一个TypeConverter接口,该接口位于ognl.jar包内,也是就Struts2框架的转换器使用了OGNL技术。该接口定义了一个convertValue()方法,实现该接口的类型转换器实现类都需要重写该方法来进行类型转换。OGNL还提供了一个实现TypeConverter接口的类DefaultTypeConverter,开发者只要继承该类,就可以开发类型转换器的实现类。


二、Struts2自定义类型转换器分为局部类型转换器和全局类型转换器

(1)局部类型转换器
如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用date类型是可以接收到的,但是如果传的是20101112这样类型的字符串,用date类型是获取不到,并且会出现错误的,struts2提供了一种类型转换器供我们使用。

以下为局部类型转换器的开发步骤
a.首先要写一个类来继承DefaultTypeConverter
b.然后覆盖convertValue这个方法,在里面进行数据转型
c.在action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是类名,后面的-conversion.properties是固定的写法,
如:HelloWorldAction-conversion.properties

d.Properties文件里面的内容为:属性名称=类型转换器的全类名(既包名.类名)

如:birthday=com.ljq.type.converter.DateTypeConverter

(2)全局类型转换器
如果业务需求所有的日期都要转换,则可以使用全局类型转换器,只要在src根目录下面放置xwork-conversion.properties文件,并且properties文件中的内容为:
待转换的类型=类型转换器的全类名
如:java.util.Date = com.type.Converter.DateTypeConverter 即可


代码
Action类

package com.ljq.action;

import java.util.Date;

public class HelloWorldAction {
// 日期
private Date birthday;
// 枚举
private Gender gender;

public String execute() {
return "success";
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
System.out.println("birthday="+birthday);
this.birthday = birthday;
}

// 自定义枚举
public enum Gender {
MAN,WOMEN
}

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
System.out.println("gender="+gender);
this.gender = gender;
}

}

日期类型转换器

package com.ljq.type.converter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
* 日期自定义类型转换器
* 
* @author jiqinlin
*
*/
public class DateTypeConverter extends DefaultTypeConverter {

@SuppressWarnings("unchecked")
@Override//其中,context是类型转换环境的上下文,value是需要转换的参数,toType 是转换后的目标类型。
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
if (toType == Date.class) { // 当字符串向Date类型转换时,这时value的来源是从url或者表单提交
String[] params = (String[]) value;
return sdf.parseObject(params[0]);// Request.getParameterValues() ,转换为string数组是因为像复选框之类的组件可以提交的值是一个数组//解析成日期格式(注意:这里的日期一定要是util包下的日期)
} else if (toType == String.class) {//当Date转换成字符串时,这时value的来源是action类,因此value不要强转为数组形式(注意:使用EL表达式向页面输出是该if不会被执行,只有使用OGNL表达式向页面输出是才会进入此if语句)
Date date=(Date)value;
return sdf.format(date);//转换为字符串
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
}

枚举类型转换器

package com.ljq.type.converter;

import java.util.Map;

import com.ljq.action.HelloWorldAction.Gender;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
* 枚举自定义类型转换器
* 
* @author jiqinlin
*
*/
public class GenderTypeConverter extends DefaultTypeConverter{

@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
if(toType==Gender.class){ //当字符串向Gender类型转换时
String[] params=(String[])value;
return Gender.valueOf(params[0]);
}else if (toType==String.class) { //当Gender转换成字符串时
Gender gender=(Gender)value;
return gender.toString();
}
return null;
}
}

配置类型转换器

测试路径
日期
http://localhost:8083/struts2/control/employee/list_execute.do?birthday=20110315 23:34:55
枚举
http://localhost:8083/struts2/control/employee/list_execute.do?gender=WOMEN


局部类型转换器: HelloWorldAction-conversion.properties
birthday=com.ljq.type.converter.DateTypeConverter
gender=com.ljq.type.converter.GenderTypeConverter


全局类型转换器: xwork-conversion.properties
java.util.Date=com.ljq.type.converter.DateTypeConverter

在页面打印日期和枚举的值

birthday=${birthday }
gender=${gender }


参考链接:

http://blog.csdn.net/gzx2080205/article/details/6340056

http://blog.163.com/taodengwen@126/blog/static/87199341201282712417646/

分享到:
评论

相关推荐

    struts2自定义类型转换器

    struts2 自定义类型转换器,实现一个对象转换成一个int 坐标值。

    Struts自定义类型转换器

    对于Struts1和Struts2中都讲到了自定义类型转换器。但是有些人可能对自定义类型转换器的运行原理不理解,这里进行简单介绍一下……

    struts 2.0 自定义类型转换器 注册类型转换器 入门实例 简单实例

    struts 2.0 自定义类型转换器 注册类型转换器 入门实例 简单实例

    自定义类型转换器

    Struts2自定义类型转换器 如果用户登陆后,可以访问Action中的所有方法,如果用户没有登陆,不允许访问Action中的方法。并且提示你没有权限执行该操作!

    struts2自定义类型转换、拦截器实例练习

    myeclipse平台下使用struts2.3框架搭建实例,理解struts2工作原理,了解struts2自定义类型转换,自定义拦截器,掌握struts2的mvc框架应用

    传智播客struts2.1源代码_自定义类型转换器

    传智播客struts2.1源代码_自定义类型转换器

    Struts 自定义转换器

    Struts 自定义转换器Struts 自定义转换器

    Struts的自定义转换器

    Struts的自定义转换器 public class LineConvertor extends StrutsTypeConverter{...}

    struts2中的类型转换器

    对于基本数据类型 strus2可以自动的将其转换成所需要的类型,但是我们自己定义的引用数据类型无法转换,本文详细介绍了strus2的自定义类型转换器

    Struts2学习教程之自定义类型转换器的方法

    类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行...下面这篇文章主要给大家介绍了关于Struts2学习教程之自定义类型转换器的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

    Struts2类型转换与输入校验 .zip

    (1) 编写一个自定义类型转换器,并基于应用程序进行全局配置,实现字符串参数与颜色值之间的转换,并将颜色值应用于视图当中。 (2) 根据实验四的图书管理系统,扩展图书新增功能,要求对新增页面表单中的每个...

    实验4 Struts2的类型转换与OGNL表达式语言.doc

    1、 理解Struts2的类型转换类型 2、 理解局部类型转换器与全局类型转换器的区别 3、 掌握Struts2的自定义类型转换 4、 理解OGNL表达式语言

    自定义struts时间转换器

    自定义的struts时间转换器,轻松实现java中时间的转换。

    一篇文章搞定Struts2的类型转换

    主要介绍了关于Struts2类型转换的相关资料,文中主要介绍了Struts2的类型转换器和自定义类型转换器的实现,有需要的朋友可以参考借鉴,下面来一起看看吧。

    struts2全局转换的问题

    测试源代码 博文链接:https://gyj129129.iteye.com/blog/225216

    自己写的struts2类型转换器

    服务器可以接收到的来自用户的数据只能是字符串或者是字符串数组,而在服务器上的对象中 ,这些数据往往有多种不同的类型,如日期(Date) 整数(int) 浮点数... 要实现 上述的转换 ,Struts2中 提供了 converter。

    struts2 详解文档

    自定义类型转换器 全局类型转换器 访问或添加几个属性 文件上传 多文件上传 自定义拦截器 对Action中所有方法进行输入校验 对Action指定方法进行校验 输入校验的流程 基于XML配置方式实现对action的所有...

    Struts2全解Struts2全解

    1、struts2概述 Struts2 软件下载 Struts2 比较重要的类 操作步骤及框架配置 struts运行机制 2、简单的struts2程序 ...9 struts2类型转换 ........ 10struts2标签库 ........ 11、访问数据库 ........

    Struts2 in action中文版

    5.4.3 配置框架使用自定义转换器 103 5.5 小结 106 第三部分 构建视图——标签和结果 第6章 构建视图——标签 108 6.1 入门 108 6.1.1 ActionContext和OGNL 109 6.1.2 虚拟对象ValueStack 111 6.2 Struts 2标签概要...

Global site tag (gtag.js) - Google Analytics