http://www.oschina.net/code/snippet_1864608_37795
使用commons-beanutils的BeanUtils类封装request到对象时注册日期转换器
代码片段(5)[全屏查看所有代码]
1. [代码]Mapping.java(FormBeanUtil升级为Mapping,脱离request)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/**
* Mapping.java
* com.platform.framework.utils
* Copyright (c) 2014.
*/
package
com.jfly.core.view;
import
java.io.Serializable;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Map;
import
org.apache.commons.beanutils.BeanUtils;
import
org.apache.commons.beanutils.ConvertUtils;
/**
* 对象转换
* @author yangjian1004
* @Date Aug 7, 2014
*/
public
class
Mapping {
public
static
Mapping m =
new
Mapping();
static
{
// 在封装之前 注册转换器
ConvertUtils.register(
new
DateTimeConverter(), java.util.Date.
class
);
}
/**
* 将Map对象转换成指定对象
* @param clazz,指定的转换后的对象。
* @param map,被转换信息的map。
* @return clazz参数指定的对象。
* @since 1.0.0
*/
public
<E
extends
Serializable> E convertObjectFromMap(Class<E> clazz, Map<String, Object> fromMap) {
if
(clazz ==
null
|| fromMap ==
null
)
return
null
;
E obj =
null
;
try
{
obj = clazz.newInstance();
BeanUtils.populate(obj, fromMap);
}
catch
(Exception e) {
e.printStackTrace();
}
return
obj;
}
/**
* 将对象转换成指定的对象。
* @param clazz,指定转换成的对象。
* @param obj,被转换的对象。
* @return clazz参数指定转换成的对象。
* @since 1.0.0
*/
public
<E
extends
Serializable> E convertObjectFromObject(Class<E> clazz, Object obj) {
if
(clazz ==
null
|| obj ==
null
)
return
null
;
E dest =
null
;
try
{
dest = clazz.newInstance();
BeanUtils.copyProperties(dest, obj);
}
catch
(Exception e) {
e.printStackTrace();
}
return
dest;
}
/**
* 将原始数据的List转换成指定对象的List。
* @param clazz,指定转换成的对象。
* @param objList,原始数据List集合。
* @return List list内每个对象元素为clazz参数指定的对象。
* @since 1.0.0
*/
public
<E
extends
Serializable> List<E> convertObjectListFromObjectList(Class<E> clazz, List<?> objList) {
List<E> ret =
new
ArrayList<E>();
if
(clazz ==
null
|| objList ==
null
|| objList.size() ==
0
)
return
ret;
for
(Object obj : objList) {
E e = convertObjectFromObject(clazz, obj);
ret.add(e);
}
return
ret;
}
public
static
void
main(String[] args) {
/*
Hotel hotel = new Hotel();
hotel.setAbridgeName("黄山");
hotel.setDs(new Date());
hotel.setHotelId(12l);
hotel.setHotelName("黄山酒店");
Mapping mapping = new Mapping();
HotelDto hotelDto = mapping.convertObjectFromObject(HotelDto.class, hotel);
Debug.println(hotelDto.getDs(), hotelDto.getHotelName(), hotelDto.getHotelId(), hotel.getDs());
Debug.println("==================");
List<Hotel> list = new ArrayList<Hotel>();
list.add(hotel);
List<HotelDto> dtoList = mapping.convertObjectListFromObjectList(HotelDto.class, list);
Debug.println(dtoList.get(0).getDs());*/
}
}
|
2. [代码]FormBeanUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/**
* FormBeanUtil.java
* com.jfly.core.view
* Copyright (c) 2014.
*/
package
com.jfly.core.view;
import
java.io.Serializable;
import
java.util.Map;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.commons.beanutils.BeanUtils;
import
org.apache.commons.beanutils.ConvertUtils;
/**
* 请求信息封装到对象
* @author yangjian1004
* @Date Aug 5, 2014
*/
public
class
FormBeanUtil {
static
{
// 在封装之前 注册转换器
ConvertUtils.register(
new
DateTimeConverter(), java.util.Date.
class
);
}
/**
* 请求信息封装到对象
*
* @param request
* 请求信息
* @param clazz
* 封装对象
*/
@SuppressWarnings
(
"unchecked"
)
public
static
<E
extends
Serializable> E get(HttpServletRequest request, Class<E> clazz) {
if
(request ==
null
)
throw
new
IllegalArgumentException(
"FormBeanUtil.get中的request为空"
);
E obj =
null
;
try
{
obj = clazz.newInstance();
Map<String, String[]> parameterMap = request.getParameterMap();
BeanUtils.populate(obj, parameterMap);
}
catch
(Exception e) {
e.printStackTrace();
}
return
obj;
}
}
|
3. [代码]DateTimeConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
/**
* DateTimeConverter.java
* com.jfly.core.view
* Copyright (c) 2014.
*/
package
com.jfly.core.view;
import
java.text.DateFormat;
import
java.text.DateFormatSymbols;
import
java.text.SimpleDateFormat;
import
java.util.Locale;
import
org.apache.commons.beanutils.Converter;
/**
* 日期转换
* <p>
* format [yyyy-MM-dd]
* format [yyyy-MM-dd HH:mm:ss]
* format [yyyy-MM-dd HH:mm:ss.SSS]
* @author yangjian1004
* @Date Aug 4, 2014
*/
@SuppressWarnings
({
"rawtypes"
})
public
class
DateTimeConverter
implements
Converter {
private
static
final
String DATE =
"yyyy-MM-dd"
;
private
static
final
String DATETIME =
"yyyy-MM-dd HH:mm:ss"
;
private
static
final
String TIMESTAMP =
"yyyy-MM-dd HH:mm:ss.SSS"
;
@Override
public
Object convert(Class type, Object value) {
return
toDate(type, value);
}
public
static
Object toDate(Class type, Object value) {
if
(value ==
null
||
""
.equals(value))
return
null
;
if
(value
instanceof
String) {
String dateValue = value.toString().trim();
int
length = dateValue.length();
if
(type.equals(java.util.Date.
class
)) {
try
{
DateFormat formatter =
null
;
if
(length <=
10
) {
formatter =
new
SimpleDateFormat(DATE,
new
DateFormatSymbols(Locale.CHINA));
return
formatter.parse(dateValue);
}
if
(length <=
19
) {
formatter =
new
SimpleDateFormat(DATETIME,
new
DateFormatSymbols(Locale.CHINA));
return
formatter.parse(dateValue);
}
if
(length <=
23
) {
formatter =
new
SimpleDateFormat(TIMESTAMP,
new
DateFormatSymbols(Locale.CHINA));
return
formatter.parse(dateValue);
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
return
value;
}
}
|
4. [代码]User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
com.demo.admin.user.model;
import
java.io.Serializable;
import
java.util.Date;
public
class
User
implements
Serializable{
private
static
final
long
serialVersionUID = -4844339718314271237L;
private
String userId;
private
String userName;
private
String userCode;
private
Date birthday;
private
String password;
public
String getUserId() {
return
userId;
}
public
void
setUserId(String userId) {
this
.userId = userId;
}
public
String getUserName() {
return
userName;
}
public
void
setUserName(String userName) {
this
.userName = userName;
}
public
String getUserCode() {
return
userCode;
}
public
void
setUserCode(String userCode) {
this
.userCode = userCode;
}
public
Date getBirthday() {
return
birthday;
}
public
void
setBirthday(Date birthday) {
this
.birthday = birthday;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
}
|
5. [代码]调用片段
1
|
User user = FormBeanUtil.get(request, User.
class
);
|