比较来自世界各地的卖家的域名和 IT 服务价格

使用输入文本的日期格式设置使用 Spring MVC

如何安装格式
https://docs.oracle.com/javase ... .html
在一个文本框中 Spring MVC?

我使用表单标记库 Spring 和标签
input

.

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

.

我想以格式显示日期
dd/MM/yyyy

.
已邀请:

郭文康

赞同来自:

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


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


然后数据编辑器本身可能看起来像 :


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/ 事实上,解析日期 LocalDates 的
http://joda-time.sourceforge.net/
Datetime - 推荐为标准日期 java/日历是一种憎恶,恕我直言。 但你必须了解这个想法。 此外,您还可以注册全局编辑器,因此您无需每个控制器执行此操作。 /我不记得了/.

龙天

赞同来自:

准备好! 我刚刚将此方法添加到我的控制器类中:


@InitBinder
protected void initBinder/WebDataBinder binder/ {
SimpleDateFormat dateFormat = new SimpleDateFormat/"dd/MM/yyyy"/;
binder.registerCustomEditor/Date.class, new CustomDateEditor/
dateFormat, false//;
}

莫问

赞同来自:

如果要格式化所有日期,而无需在每个控制器中重复相同的代码,则可以创建

全球的 InitBinder

在课堂上。

注解 @ControllerAdvice.

脚步

1.

创建一个类 DateEditor, 例如,这将格式化您的日期:


public class DateEditor extends PropertyEditorSupport {

public void setAsText/String value/ {
try {
setValue/new SimpleDateFormat/"dd/MM/yyyy"/.parse/value//;
} catch/ParseException e/ {
setValue/null/;
}
}

public String getAsText// {
String s = "";
if /getValue// != null/ {
s = new SimpleDateFormat/"dd/MM/yyyy"/.format//Date/ getValue///;
}
return s;
}


2.

用注释创建一个类 @ControllerAdvice /我打电话给它 GlobalBindingInitializer/:


@ControllerAdvice
public class GlobalBindingInitializer {

/* Initialize a global InitBinder for dates instead of cloning its code in every Controller */

@InitBinder
public void binder/WebDataBinder binder/ {
binder.registerCustomEditor/Date.class, new DateEditor///;
}
}


3.

在配置文件中 Spring MVC /例如, webmvc-config.xml/ 添加允许的字符串 Spring 扫描您创建类的包 GlobalBindingInitializer. 例如,如果您创建了 GlobalBindingInitializer 在包装中 org.example.common:


<context:component-scan base-package="org.example.common"></context:component-scan>


precheno!

来源

:

http://keenformatics.blogspot. ... .html
/我的博客/

http://static.springsource.org ... .html
http://static.springsource.org ... .html
个人经验

要回复问题请先登录注册