I want to localize exception messages from POJO classes using Spring. I have a Spring MVC application through which I can add books. If the name of the added book is null, the implementation class throws an exception. I want to localize this.
I know that I can use localeResolvers in JSP pages, and I already did it. Can I use this to collect localized error messages in POJO? If so, how can I insert a language language editor (Cookie or Session) or the locale that was set in Cookie / Session into the POJO class?
addBook method browser exception
public void addBook(IBook book) throws Exception {
if (book.getTitle() == null || book.getTitle() == "") {
throw new Exception("Title is null");
}
I want to throw new Exception("Title is null"); be something like
String msg = rBundle.getMessage(propKey)
throw new Exception(msg);
rBundle - , , propKey
,
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@RequestParam("siteLanguage") String siteLanguage,
@ModelAttribute("book") Book book, BindingResult result,
SessionStatus status, HttpServletRequest arg0) {
logger.debug("Adding a Book");
Locale loc = RequestContextUtils.getLocale(arg0);
if (result.hasErrors()) {
return "error.htm";
} else {
try {
Author author = new Author("Gabriel Garcia Marquez");
book.setAuthor(author);
library.addBook(book);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:home.htm";
}
}
? java ResourceBundle .