java - How to configure locale based date format support in spring -
does had problem:
i need configure spring recognize locale based date , number format, i.e. following user behavior should valid:
- user select language
en
, number format1.23
should valid, spring mvc accept format , no valid error triggered. user can change date date formatmm/dd/yyyy
, no valid error raised, user can post form. - user select language
de
, number format1,23
should bevalid, spring mvc accept format , no valid error triggered. user can change date date formatdd.mm.yyyy
, no valid error triggerd. user can post form.
i'v tried use @datetimeformat(pattern="#{messagesource['date_format']}")
,(i have date_format defined in messages_(locale).properties) seems spring doesn't support yet, see jira issue
does has similar problem , got solution.
does write own converter, , register in org.springframework.format.support.formattingconversionservicefactorybean
? need kind of request based converter
since nobody answers question, post 1 of solution solve problem, others:
- i had request scoped bean, resolves locale using:
requestcontextutils.getlocale(request);
request can autowired request scoped class(notice, works field injection, not construction or setter). in class locale based date/number format. in controller (we have abstractcontroller). have code this:
@initbinder public void initbinder(webdatabinder binder) { binder.registercustomeditor(date.class, new localizeddateeditor(formathelper)); }
formathelper
request scoped bean. localizeddateeditor
looks this:
public class localizeddateeditor extends customdateeditor { public localizeddateeditor(formathelper formathelper) { super(new simpledateformat(formathelper.getdefaultdateformat()), true); } }
it tell spring use dateformat.
that's all.
Comments
Post a Comment