Showing posts with label Property. Show all posts
Showing posts with label Property. Show all posts

Sunday, December 19, 2010

spring的属性编辑器CustomDateEditor及日期对象转化

在spring mvc的Controller中,属性在通过依赖注入(DI)时,普通数据类型都能够辨识。但诸如Date之类,就需要自定义属性编辑器解决。否则报如下错误:
org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [java.lang.String] to required type
[java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
这表示spring无法找到合适的转换策略,需要自己写一个转换器,在spring中称之为属性编辑器。
spring中的属性编辑器可以将字符串转换为相应的对象,然后注入到其它对象中去。
编写自己的属性编辑器的步骤很简单,属性编辑器类需要从java.beans.PropertyEditorSupport类继承,在这个类中有一个setAsText方法,这个方法有一个String类型的参数,通过这个方法,可以将String类型的参数值转换成其他类型的属性。在这个方法中我们还需要使用一个setValue方法,就来指定转换后的对象实例。
spring 中有个 CustomDateEditor 的类就是继承 PropertyEditorSupport 的一个属性编辑器,在Controller中添加一个@InitBinder的Annotation到某个方法上,在方法中指明日期字符串的格式,就可以将符合此格式的字符串转化为日期对象,代码如下:


/**
* <pre>
* HTML forms work with string values only, when your Authority is a complex bean. You need to configure a PropertyEditor to perform conversion between Authority and String:
*
* @InitBinder
* public void initBinder(WebDataBinder b) {
* b.registerCustomEditor(Authority.class, new AuthorityEditor());
* }
*
* private class AuthorityEditor extends PropertyEditorSupport {
* @Override
* public void setAsText(String text) throws IllegalArgumentException {
* // 另外一个例子是根据字符串,从数据库中查找返回对象
* setValue(authorityService.findById(Long.valueOf(text)));
* }
*
* @Override
* public String getAsText() {
* return ((Authority) getValue()).getId();
* }
* }
* </pre>
*
* 这个方法用来将页面表单上传的Date字符串转化成java的Date对象
*
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}


Reference: how to pass a date into bean property

Monday, July 28, 2008

PropertyAccess/ClassPropertyAdapter/PropertyAdapter of Tapestry5

看了三个接口说明,简要记下三者的关系:
1、PropertyAccess中的get/set方法是用于读取/设置某对象instance中的某属性property,比较明了,另外还有个方法getAdapter是用于获取此对象class或者实例instance的ClassPropertyAdapter对象。
2、而在得到ClassPropertyAdapter后,也可以与PropertyAccess中的get/set方法一样操作一个instance中的property,另外有个方法getPropertyAdapter则根据参数propertyName获取PropertyAdapter对象。
3、在得到PropertyAdapter对象之后,也同前二个接口一样可以传入instance实例,对此实例的propertyName进行get/set操作。其中有个方法getClassAdapter则是反过来用于获取ClassPropertyAdapter对象,以操作此对象的其他属性。

Wednesday, March 19, 2008

Javascript on property href of link

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>JavaScript Test</title>
</head>

<body>
<a href="http://www.test.com/Javascript/">test a href when it be clicked will load another web page and then location to its original destination url.</a><br />
<a href="http://www.test.com/Javascript/" onclick="return false;">test a href which property value of onclick return false</a><br />
<a href="javascript:void(0);">test a href with value:"javascript: void(0);"</a>
<script type="text/javascript">
var a = document.getElementsByTagName('a')[0];
var h = a.href;
a.href = "#"; // a.href = "javascript: void(0);"
//a.target = "_self";
a.onclick = function(){
var img = new Image(1,1);
img.src = "http://www.google.com/images/firefox/sprite.png" + (new Date).getTime();
f = function(){setTimeout("window.location = h", 1000);};
img.onload=f;
img.onerror=f;
return false;
};
</script>
</body>
</html>