How can I send resourcebundle messages through controllers in spring mvc?

Most spring tutorials and examples show how to get a message from a resource file and how to show it in your view (jsp), but not how you should handle these messages in your controller and between views.

Here is an example of how I am doing this now when I have a view / controller that handles forgotten passwords. When the password is sent, I am redirected back to the login screen with the message that "your password has been sent ..."

@RequestMapping(value="/forgottenpassword") public String forgottenpassword(@RequestParam String email) { ....something something if(email != null){ return "redirect:/login?forgottenpassword=ok"; } } @RequestMapping(value="/login") public String login(HttpServletRequest request) { if(request.getParameter("forgottenpassword") != null && request.getParameter("forgottenpassword").equals("ok")) { data.put("ok_forgottenpassword", "forgottenpassword.ok"); } return "login"; } 

Finally, I show the message in my opinion, in this case the freemarker template

 <#if (ok_forgottenpassword?exists)> <div class="alert alert-success"><@spring.message "${ok_forgottenpassword}" /></div> </#if> 

Is this the best way to do this as part of spring? It's just with one type of message, but what if I need 5?

0
java spring spring-mvc resourcebundle
source share
3 answers

This method is actullay, which is called a flash message and is implemented in Spring 3.1 as shown in this answer: fooobar.com/questions/853643 / ...

0
source share

Just create a simple bean and click on the data. In this bean you can get all the messages that you want to download from the resource. (By the way, do you really need a resource package? It does some fancy tricks that you absolutely don't need if you don't need i18n. A simple properties file will suffice in almost every other case.)

0
source share

Add errors to the list in your controller, for example

 List<String> errorsList = new ArrayList<String>(); errorsList.add("error.invalid.username"); errorsList.add("error.invalid.password"); errorsList.add("error.invalid.passwordResetLinkSent"); ..... 

Then on the jsp page iterate all the errors to display as

  <c:if test="${!empty errorsList}"> <ul> <c:forEach var="error" items="${errorsList}"> <li><spring:message message="${error}"></spring:message></li> </c:forEach> </ul> </c:if> 
0
source share

All Articles