本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

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>


阅读全文(5468) | 回复(1) | 编辑 | 精华
 


回复:spring技术手册阅读笔记(三) 使用CustomEditorConfigurer
原创空间,  软件技术,  电脑与网络

kruce(游客)发表评论于2006/9/25 15:45:07

加油。。


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144769025 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号