'Elegant way to get Locale in Spring Controller [duplicate]
I'm looking for a cleaner way (in Spring 3.2) to get the current locale than explicitly calling LocaleContextHolder.getLocale() at the start of each Controller method. It has to be compatible with Java annotation, as I'm not using the XML config. Here's what 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";
}
}
Solution 1:[1]
As an alternative, you can also autowire the HttpServletRequest
@Autowired
private HttpServletRequest request;
and then use request.getLocale() anywhere in your Controller.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
