使用Spring MVC设置输入文本的日期格式 [英] Set date format for an input text using Spring MVC

查看:111
本文介绍了使用Spring MVC设置输入文本的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 <的格式使用Spring MVC的文本字段中的code>日期

我正在使用Spring Form标记库和输入标签。

I'm using the Spring Form tag library and the input tag.

我现在得到的是这样的 Mon May 28 11 :09:28 CEST 2012

What I get now is something like this Mon May 28 11:09:28 CEST 2012.

我想在 dd / MM / yyyy <中显示日期/ code> format。

I'd like to show the date in dd/MM/yyyy format.

推荐答案

在yr控制器中注册日期编辑器:

register a date editor in yr controller :

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LocalDate.class, new LocalDateEditor());
}

然后数据编辑器本身可能如下所示:

and then the data editor itself can look like this :

public class LocalDateEditor extends PropertyEditorSupport{

 @Override
 public void setAsText(String text) throws IllegalArgumentException{
   setValue(Joda.getLocalDateFromddMMMyyyy(text));
 }

 @Override
 public String getAsText() throws IllegalArgumentException {
   return Joda.getStringFromLocalDate((LocalDate) getValue());
 }
}

我使用自己的抽象实用工具类(Joda)解析日期,实际上来自 Joda Datetime library 的LocalDates - 推荐作为标准的java日期/日历是一个憎恶,imho。但你应该明白这个想法。此外,您可以注册一个全局编辑器,这样您就不必每个控制器(我不记得如何)。

I am using my own abstract utility class (Joda) for parsing dates, in fact LocalDates from Joda Datetime library - recommended as the standard java date/calendar is an abomination, imho. But you should get the idea. Also, you can register a global editor, so you don't have to do it each controller (I can't remember how).

这篇关于使用Spring MVC设置输入文本的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆