java - Elegant way to get Locale in Spring Controller -
i'm looking cleaner way (in spring 3.2) current locale explicitly calling localecontextholder.getlocale() @ start of each controller method. has compatible java annotation, i'm not using xml config. here's i'm doing currently.
@controller public class wifecontroller { @autowired private messagesource msgsrc; @requestmapping(value = "/wife/mood") public string readwife(model model, @requestparam("whatimdoing") string iam) { locale loc = localecontextholder.getlocale(); if(iam.equals("playingxbox")) { model.addattribute( "statustitle", msgsrc.getmessage("mood.angry", null, loc) ); model.addattribute( "statusdetail", msgsrc.getmessage("mood.angry.xboxdiatribe", null, loc) ); } return "moodresult"; } }
in spring 3.2 reference docs, section 17.3.3, supported method argument types:
the following supported method arguments:
request or response objects (servlet api). choose specific request or response type, example servletrequest or httpservletrequest.
(...)
java.util.locale current request locale, determined specific locale resolver available, in effect, configured localeresolver in servlet environment.
so you'd need receive instance of locale
argument in every method:
@requestmapping(value = "/wife/mood") public string readwife(model model, @requestparam("whatimdoing") string iam, locale loc) { if (iam.equals("playingxbox")) { model.addattribute("statustitle", msgsrc.getmessage("mood.angry", null, loc)); model.addattribute("statusdetail", msgsrc.getmessage("mood.angry.xboxdiatribe", null, loc)); } return "moodresult"; }
Comments
Post a Comment