views:

231

answers:

2

What is the best way to organize status messages ("Your data has been successfully saved/added/deleted") on the Spring MVC-based site using annotation controller?

So, the issue is in the way of sending the message from POST-method in contoller.

A: 

You should keep it simple and use the specific HTTP 1.1 Status codes. So for a successful call you would return a 200 OK. And on the client side if you want to show a specific message to the user when the controller returns a 200, then you show it there.

BrennaSoft
But if I want to display different messages depends on context, how will I know what message should show if I have only 200 OK message?
linpulee
The point is that you only return a 200 when status is ok. If something is not found return 404, if server error return 500, if unauthorized return 403.
BrennaSoft
A: 

If you mean that the page is reloaded after some POST, you can include a flag in you view (JSP or velocity or whatever you use). E.g. something like this

<c:if test="${not empty resultMessage}">
 <spring:message code="${resultMessage}" />
</c:if>

And your message bundle should contain a message for that code.

If you do an AJAX POST to submit some data (i.e. page is not reloaded and you need to show a message) you could

1) make you JS files dynamic (JSP or Velocity, again) and insert <spring:message> tags there (I don't really like this option)

or

2) follow the advice from this link and use @ResponseBody to return a status object to your JS. Inside the object you mark as @ResponseBody you can put both status and messages. E.g. using your message bundles as in this case.

Leonid