现在的位置: 首页 > 综合 > 正文

struts2 自定义类型转换器

2017年10月24日 ⁄ 综合 ⁄ 共 3569字 ⁄ 字号 评论关闭

一、问题的引出
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/

抱歉!评论已关闭.