« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 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 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9707788 建立时间:2004年12月20日 |

| |
[j2ee]spring技术手册阅读笔记(三) 使用CustomEditorConfigurer 原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2006/9/15 15:57:12 |
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> |
|
回复:spring技术手册阅读笔记(三) 使用CustomEditorConfigurer 原创空间, 软件技术, 电脑与网络
kruce(游客)发表评论于2006/9/25 15:45:07 |
|
» 1 »
|