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:

  1. user select language en, number format 1.23 should valid, spring mvc accept format , no valid error triggered. user can change date date format mm/dd/yyyy , no valid error raised, user can post form.
  2. user select language de, number format 1,23 should bevalid, spring mvc accept format , no valid error triggered. user can change date date format dd.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:

  1. 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.
  2. 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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -