- 浏览: 7254295 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
项目中Struts2.1.6不支持基本数据类型的自动转换,项目继而采用Struts2.1.8支持数据类型的基本原始数据类型的自动转换。主要原因在:XWork的版本原来xwork2.1.3 提供为xwork2.16 版本:
下面代码为xwork2.16版本的源码:
* <p/>
* 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:
* <p/>
* <ul>
* <li>String</li>
* <li>boolean / Boolean</li>
* <li>char / Character</li>
* <li>int / Integer, float / Float, long / Long, double / Double</li>
* <li>dates - uses the SHORT format for the Locale associated with the current request</li>
* <li>arrays - assuming the individual strings can be coverted to the individual items</li>
* <li>collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is
* created</li>
* </ul>
* <p/> 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 occured while processing the type conversion.
* <p/>
public class XWorkBasicConverter extends DefaultTypeConverter :
//转换器的重点转换的方面如下:
@Override
public Object convertValue(Map<String, Object> context, Object o, Member member, String s, Object value, Class toType) {
Object result = null;
if (value == null || toType.isAssignableFrom(value.getClass())) {
// no need to convert at all, right?
return value;
}
if (toType == String.class) {
/* the code below has been disabled as it causes sideffects in Struts2 (XW-512)
// if input (value) is a number then use special conversion method (XW-490)
Class inputType = value.getClass();
if (Number.class.isAssignableFrom(inputType)) {
result = doConvertFromNumberToString(context, value, inputType);
if (result != null) {
return result;
}
}*/
// okay use default string conversion
result = doConvertToString(context, value);
} else if (toType == boolean.class) {
result = doConvertToBoolean(value);
} else if (toType == Boolean.class) {
result = doConvertToBoolean(value);
} else if (toType.isArray()) {
result = doConvertToArray(context, o, member, s, value, toType);
} else if (Date.class.isAssignableFrom(toType)) {
result = doConvertToDate(context, value, toType);
} else if (Calendar.class.isAssignableFrom(toType)) {
Date dateResult = (Date) doConvertToDate(context, value, Date.class);
if (dateResult != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateResult);
result = calendar;
}
} else if (Collection.class.isAssignableFrom(toType)) {
result = doConvertToCollection(context, o, member, s, value, toType);
} else if (toType == Character.class) {
result = doConvertToCharacter(value);
} else if (toType == char.class) {
result = doConvertToCharacter(value);
} else if (Number.class.isAssignableFrom(toType) || toType.isPrimitive()) {
result = doConvertToNumber(context, value, toType);
} else if (toType == Class.class) {
result = doConvertToClass(value);
}
if (result == null) {
if (value instanceof Object[]) {
Object[] array = (Object[]) value;
if (array.length >= 1) {
value = array[0];
} else {
value = null;
}
// let's try to convert the first element only
result = convertValue(context, o, member, s, value, toType);
} else if (!"".equals(value)) { // we've already tried the types we know
result = super.convertValue(context, value, toType);
}
if (result == null && value != null && !"".equals(value)) {
throw new XWorkException("Cannot create type " + toType + " from value " + value);
}
}
return result;
}
// 其中的一个数字类型的转换:有此可以看出支持红色的部分的哦:^_^
private Object doConvertToNumber(Map<String, Object> context, Object value, Class toType) {
if (value instanceof String) {
if (toType == BigDecimal.class) {
return new BigDecimal((String) value);
} else if (toType == BigInteger.class) {
return new BigInteger((String) value);
} else if (toType.isPrimitive()) {
Object convertedValue = super.convertValue(context, value, toType);
String stringValue = (String) value;
if (!isInRange((Number)convertedValue, stringValue, toType))
throw new XWorkException("Overflow or underflow casting: \"" + stringValue + "\" into class " + convertedValue.getClass().getName());
return convertedValue;
} else {
String stringValue = (String) value;
if (!toType.isPrimitive() && (stringValue == null || stringValue.length() == 0)) {
return null;
}
NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
ParsePosition parsePos = new ParsePosition(0);
if (isIntegerType(toType)) {
numFormat.setParseIntegerOnly(true);
}
numFormat.setGroupingUsed(true);
Number number = numFormat.parse(stringValue, parsePos);
if (parsePos.getIndex() != stringValue.length()) {
throw new XWorkException("Unparseable number: \"" + stringValue + "\" at position "
+ parsePos.getIndex());
} else {
if (!isInRange(number, stringValue, toType))
throw new XWorkException("Overflow or underflow casting: \"" + stringValue + "\" into class " + number.getClass().getName());
value = super.convertValue(context, number, toType);
}
}
} else if (value instanceof Object[]) {
Object[] objArray = (Object[]) value;
if (objArray.length == 1) {
return doConvertToNumber(context, objArray[0], toType);
}
}
// pass it through DefaultTypeConverter
return super.convertValue(context, value, toType);
}
//时间类型的转换:
private Object doConvertToDate(Map<String, Object> context, Object value, Class toType) {
Date result = null;
if (value instanceof String && value != null && ((String) value).length() > 0) {
String sa = (String) value;
Locale locale = getLocale(context);
DateFormat df = null;
if (java.sql.Time.class == toType) {
df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
} else if (java.sql.Timestamp.class == toType) {
Date check = null;
//支持的格式
SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM,
locale);
SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT,
locale);
SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
locale);
SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt};
for (SimpleDateFormat fmt : fmts) {
try {
check = fmt.parse(sa);
df = fmt;
if (check != null) {
break;
}
} catch (ParseException ignore) {
}
}
} else if (java.util.Date.class == toType) {
Date check = null;
DateFormat[] dfs = getDateFormats(locale);
for (DateFormat df1 : dfs) {
try {
check = df1.parse(sa);
df = df1;
if (check != null) {
break;
}
}
catch (ParseException ignore) {
}
}
}
//final fallback for dates without time
if (df == null) {
df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
}
try {
df.setLenient(false); // let's use strict parsing (XW-341)
result = df.parse(sa);
if (!(Date.class == toType)) {
try {
Constructor constructor = toType.getConstructor(new Class[]{long.class});
return constructor.newInstance(new Object[]{Long.valueOf(result.getTime())});
} catch (Exception e) {
throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
}
}
} catch (ParseException e) {
throw new XWorkException("Could not parse date", e);
}
} else if (Date.class.isAssignableFrom(value.getClass())) {
result = (Date) value;
}
return result;
}
//Struts2.0可以格式化的几种类型:
private DateFormat[] getDateFormats(Locale locale) {
DateFormat dt1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
DateFormat dt2 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
DateFormat dt3 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
DateFormat d1 = DateFormat.getDateInstance(DateFormat.SHORT, locale);
DateFormat d2 = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
DateFormat d3 = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
DateFormat[] dfs = {dt1, dt2, dt3, rfc3399, d1, d2, d3}; //added RFC 3339 date format (XW-473)
return dfs;
}
评论
默认是“yyyy-MM-dd'T'HH:mm:ss”,我想改成“yyyy-MM-dd'T'HH:mm”?
第二种方式就是
一。采用自定义转换器继承自DefaultTypeConverter ,实现相应的转换方法即可。备注此处你需要实现的是将时间Date转换为自定义的格式的字符串。
二.在全巨额的xwork-conversion.properties文件配置。
数据类型=转换器类(包含包名)
例如:
java.util.Date=com.easyway.dev.common.DataTypeConverter
默认是“yyyy-MM-dd'T'HH:mm:ss”,我想改成“yyyy-MM-dd'T'HH:mm”?
现在一个struts的国家化文件配置需要的格式化成的格式
一、资源文件的配置(applicationResource_zh_CN.properties)
format.number = {0,number,###,###.##}
format.discount = {0,number,###.#######%}
format.time = {0,time}
format.number = {0,number,#0.0##}
format.percent = {0,number,##0.00'%'}
format.money = {0,number,\u00A4##0.00}
format.date = {0,date,yyyy-MM-dd }
二在struts.xml配置国家化文件的名称:
<constant name="struts.action.extension" value="do"/>
<!--指定国际化文件的名称-->
<constant name="struts.custom.i18n.resources" value="applicationResource"></constant>
三:jsp页面取值:
数字和时间一样
页面格式化输出数据时候,只需要:
<s:text name="format.number">
<s:param name="value" value="a.somefield"/>
</s:text>
默认是“yyyy-MM-dd'T'HH:mm:ss”,我想改成“yyyy-MM-dd'T'HH:mm”?
发表评论
-
整合Struts2+JasperReport Web报表应用示例
2013-04-22 13:56 2250整合Struts2+JasperReport Web报表应用 ... -
Struts2+JFreeChart制作图标
2011-11-26 13:52 2047前言 关于Struts2入门以及提高等在这里就不介绍了 ... -
Struts2.0中获取项目的上下文的两种方式
2010-01-19 13:42 3242Struts2.0中获取项目的上下文的两种方式 方 ... -
Struts2.0中通过OGNL访问常量必须注意的一个配置
2010-01-19 13:34 2593在项目的开始阶段,由于没有学习Struts2.0中OGNL的 ... -
Struts2.0 中值栈的实现以及解析OGNL以及值堆栈的原理
2010-01-14 13:19 4629OGNL的值栈实现的堆栈动态OGNL的表达式。何时设置 ... -
关于Struts2.0 标签中采用%{}%的处理原理
2010-01-14 13:08 2661项目中采用struts2.0 ... -
struts 2配置文件解说
2010-01-13 13:09 1370struts 2框架有两个核心配置文件: struts.xml ... -
struts2.0的ognl表达式 研究
2010-01-13 13:05 2612OGNL —— 完美的催化剂 为了解决数据从 ... -
Struts2和OGNL
2010-01-13 10:57 2015OGNL是XWork引入的一个非常有效的数据处 ... -
Struts2参数传递
2010-01-13 10:45 2397本篇主要通过实例来讲述Struts2中各种各样的参数传递。这个 ... -
Struts2.0的不同版本针对枚举转换器应用
2010-01-13 10:39 2808Struts2.0 的Apache项目中从Strut ... -
Struts2.0加載配置順序
2009-12-28 13:33 2475Struts2.0默認的加载顺序: Constants ... -
Struts2.0的IOC方式注入SessionAware, CookiesAware
2009-12-10 19:59 3958在Action实现相应的 private Map<S ... -
Struts2工作原理和框架扩展点
2009-12-08 18:49 3832上图来源于Struts2官方站点,是Struts ... -
Struts2.0 和JQuery實現AjaX的調用功能
2009-11-27 12:47 2323开发过程忽略: 重点注意点解释以下: (1)将Ac ... -
在Struts2.0中批量操作(update Or Add)的實現和注意點
2009-11-15 16:41 2999在項目中,需要針對一批数据进行或添加或着修改操作。使用S ... -
在Struts2.0 中将Enum对象与组件的绑定
2009-08-19 13:05 4120在项目使用一个下拉框绑定或单选或复选框的绑定的应用: ... -
struts2 标签针对字符串截取的处理
2009-06-18 13:16 6154struts2 标签截取字符串的功能 <td> ... -
Struts2中 用OGNL表达式
2009-06-18 12:40 3438具体记录如下:<s:set ... -
struts2的学习(复杂的遍历标签)
2009-06-18 12:36 2355例如我有个此结构的map ...
相关推荐
05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...
Struts2.0视频教程,struts2.0中文教程,Struts2.0视频教程,struts2.0中文教程,
struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0
struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架
转换器(Converter)——Struts 2.0中的魔术师 在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD ...
struts2.0的数据校验struts2.0的数据校验struts2.0的数据校验struts2.0的数据校验struts2.0的数据校验struts2.0的数据校验
sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0
05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...
struts2.0的基本jar包,用于开发struts2.0的项目
struts2.0jar包 struts2.0包 struts2.0源文件
JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...
此为Struts2.0最新Jar包,方便各位用于Struts2.0的开发.
struts 2.0 详细配置 struts 2.0 详细配置 struts 2.0 详细配置
Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程Struts2.0中文教程
清晰的介绍了Struts 2.0框架的工作流程,Action线程安全,程序入口,配置文件。
为Struts 2.0做好准备 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放...
struts2.0 入门案例、简单的struts2.0入门案例 2.0配置包,基础