spring技术手册阅读笔记(三) 使用CustomEditorConfigurer
2006-09-25 15:03
323 查看
CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型,更方便的使用PropertyEditorSupport.PropertyEditorSupport实现PropertyEditor,必须重新定义setAsText.
举个将字符串转为date的例子.
import java.util.Date;
public final class ValueRelationalOperand {
private Date _date;
public Date getValue() {
return _date;
}
public void setValue(Date date) {
_date = date;
}
}
配置文件
<bean id="configBean"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg index="0">
<bean class="java.text.SimpleDateFormat">
<constructor-arg>
<value>yyyy/MM/dd</value>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg index="1">
<value>true</value>
</constructor-arg>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="rightDateOperand" class="onlyfun.caterpillar.ValueRelationalOperand">
<property name="value" value="2001/15/01"/>
</bean>
测试例子
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public final class Main {
public static void main(final String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"D://workspace//springdemo//src//beans-config.xml");
ValueRelationalOperand op = (ValueRelationalOperand) ctx
.getBean("rightDateOperand");
System.out.println(op.getValue());
}
}
这只是用于ApplicationContext中,如果用于spring MVC中,要麻烦一些,必须注册property editor ,需要重载initBinder,然后调用binder.registerCustomEditor(Date.class, yourPropertyEditor),
代码:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class,ValueRelationalOperand);
}
页面绑定
<spring:bind path="dateCommand.startDate">
<input type="text" id="startDateField" name="${status.expression}" value="<c:out value="${status.value}"/>"/>
<span class="fieldError">${status.errorMessage}</span>
</spring:bind>
举个将字符串转为date的例子.
import java.util.Date;
public final class ValueRelationalOperand {
private Date _date;
public Date getValue() {
return _date;
}
public void setValue(Date date) {
_date = date;
}
}
配置文件
<bean id="configBean"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg index="0">
<bean class="java.text.SimpleDateFormat">
<constructor-arg>
<value>yyyy/MM/dd</value>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg index="1">
<value>true</value>
</constructor-arg>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="rightDateOperand" class="onlyfun.caterpillar.ValueRelationalOperand">
<property name="value" value="2001/15/01"/>
</bean>
测试例子
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public final class Main {
public static void main(final String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"D://workspace//springdemo//src//beans-config.xml");
ValueRelationalOperand op = (ValueRelationalOperand) ctx
.getBean("rightDateOperand");
System.out.println(op.getValue());
}
}
这只是用于ApplicationContext中,如果用于spring MVC中,要麻烦一些,必须注册property editor ,需要重载initBinder,然后调用binder.registerCustomEditor(Date.class, yourPropertyEditor),
代码:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class,ValueRelationalOperand);
}
页面绑定
<spring:bind path="dateCommand.startDate">
<input type="text" id="startDateField" name="${status.expression}" value="<c:out value="${status.value}"/>"/>
<span class="fieldError">${status.errorMessage}</span>
</spring:bind>
相关文章推荐
- 细品RibbonX(11):使用Custom UI Editor自定义功能区
- Spring学习笔记-CustomEditorConfigurer
- Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器
- 细品RibbonX(11):使用Custom UI Editor自定义功能区
- spring中自定义属性编辑器CustomEditorConfigurer
- Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器
- 插手容器启动之CustomEditorConfigurer
- CustomEditorConfigurer,bean的属性编辑器
- spring中自定义属性编辑器CustomEditorConfigurer
- Error creating bean with name 'org.apache.cxf.binding.soap.customEditorConfigurer'
- spring中自定义属性编辑器CustomEditorConfigurer
- Mina 配置中的 CustomEditorConfigurer
- spring中自定义属性编辑器CustomEditorConfigurer
- CTE(Custom Terrain Editor)使用说明
- Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器
- spring读取资源文件时自定义数据类型转换---CustomEditorConfigurer
- spring中自定义属性编辑器CustomEditorConfigurer
- Unity编辑器开发,使用CustomEditor增加脚本的中文说明
- Spring 4 CustomEditorConfigurer Example--转
- spring中自定义属性编辑器CustomEditorConfigurer