处理流程
在上述流程图中,控制层Action可以通过拦截器对其属性自动赋值,但是多个数据类型有时候并不一定能够成功转换。
自定义类型转换器
常见的类型转化器主要是针对日期类型的,对于自定义类型转换器,只需要继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter或者org.apache.struts2.util.StrutsTypeConverter(StrutsTypeConverter 继承与DefaultTypeConve)。 主要有两种方式进行转换:
1、全局范围的类型转换器
在源目录(src/main/java)下新建xwork-conversion.properties文件。其中属性文件的key值为需要转换的类的全名,value值为进行类型转换的类的全名。如下:
#全局类型转换器:类全名=类型转换器全名
#java.util.Date=com.struts.typeconverter.DefaultDateTypeConverter
java.util.Date=com.struts.typeconverter.StrutsDateTypeConverter
其中在本类中定义了两个类型转换器,一个是继承DefaultTypeConverter,一个是继承StrutsTypeConverter。这两个类的实现方式不同并且前台获取的方式也不一致。参见以下代码:
继承StrutsTypeConverter:
package com.struts.typeconverter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**
* @TODO 类型转换器
* @author Administrator
* @date 2015年6月26日 下午10:40:49
* @version 1.0
*/
public class DefaultDateTypeConverter extends DefaultTypeConverter{
private static final String[] DATE_FORMAT = {"yyyy-MM-dd","yyyy/MM/dd","yyyyMMdd"};
/**
*@param context
*@param value
*@param toType
*/
@Override
public Object convertValue(Map<String, Object> context, Object value,
@SuppressWarnings("rawtypes") Class toType) {
Object formatValue = null;
for(String format:DATE_FORMAT){
if(toType == Date.class){
try{
String source = ((String[])value)[0];
formatValue = new SimpleDateFormat(format).parse(source);
return formatValue;
}catch(Exception e){
}
}else if(toType == String.class){
try{
Date source = ((Date[])value)[0];
formatValue = new SimpleDateFormat(format).format(source);
return formatValue;
}catch(Exception e){
}
}
}
return formatValue;
}
}
继承StrutsTypeConverter:
package com.struts.typeconverter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
* @TODO TODO
* @author Administrator
* @date 2015年6月26日 下午11:40:12
* @version 1.0
*/
public class StrutsDateTypeConverter extends StrutsTypeConverter{
private static final String[] DATE_FORMAT = {"yyyy-MM-dd","yyyy/MM/dd","yyyyMMdd"};
/**
* 对前台传递的日期参数进行转换
*/
@SuppressWarnings("rawtypes")
@Override
public Object convertFromString(Map paramMap, String[] paramArrayOfString,
Class paramClass) {
Object formatValue = null;
for(String format:DATE_FORMAT){
if(paramClass == Date.class){
try{
formatValue = new SimpleDateFormat(format).parse(paramArrayOfString[0]);
return formatValue;
}catch(Exception e){
}
}else if(paramClass == String.class){
try{
formatValue = new SimpleDateFormat(format).format(paramArrayOfString[0]);
return formatValue;
}catch(Exception e){
}
}
}
return formatValue;
}
/**
* 在前台使用struts标签<s:property/>时显示使用
*/
@SuppressWarnings("rawtypes")
@Override
public String convertToString(Map paramMap, Object paramObject) {
return new SimpleDateFormat("yyyy-MM-dd").format(paramObject);
}
}
使用struts2.1.16,如果需要使用EL表达式,则需要在使用的页面添加以下代码:
<%@page isELIgnored="false" %>
前台页面获取属性代码如下:
<s:property value="login.username"/>登录成功!<br/>
出生日期:<s:date name="login.birthday" format="yyyy/MM/dd" /><br/>
<!-- 继承StrutsTypeConverter 可以使用此标签获取日期值,继承DefaultTypeConverter则无法获取属性值 -->
<s:property value="login.birthday" /><br/>
EL表达式:${login.birthday }
2、特定类的类型转换器
做法:在特定类的相同目录下创建名为ClassName-conversion.properties的属性文件,内容为:特定类的属性名=类型转换器类全名,在Login的同级目录下新建配置为文件:Login-conversion.properties,内容如下:
#适用于Login类属性birthday的转换器
birthday=com.struts.typeconverter.DefaultDateTypeConverter
#birthday=com.struts.typeconverter.StrutsDateTypeConverter
局部类型转换器同样能达到全局类型转换器的作用。