In MVC (such as JSP and Spring), is it bad practice to view related code in the controller?
In my case, the controller does some work and then hands off the results to the view (JSP). In the case of a status message, I can pass the entire message text to the view, or pass a key and let the JSP map it to the message text.
Example:
Message generated in controller
Spring Controller:
protected ModelAndView onSubmit(...) {
Map map = new HashMap();
// Controller processing
if (...)
map.put("status", "Case 1 status message");
else
map.put("status", "Case 2 status message");
return new ModelAndView("viewPage", map);
}
JSP:
{$status}
Message generated in view
Spring Controller:
protected ModelAndView onSubmit(...) {
Map map = new HashMap();
// Controller processing
if (...)
map.put("status", "case1");
else
map.put("status", "case2");
return new ModelAndView("viewPage", map);
}
JSP:
<c:choose>
<c:when test="{$status eq 'case1'}">Case 1 status message</c:when>
<c:when test="{$status eq 'case2'}">Case 2 status message</c:when>
</c:choose>
In the first case, the controller and JSP code is simpler, but there's view related logic in the controller. In the second case, all view logic is in the JSP, but the code isn't as simple.
Am I violating MVC paradigm by generating message text in the controller? What is the common practice for this scenario?
The common practice is to use resource bundles :-) You can configure them as message sources in your Spring context and use message tag to retrieve them.
Combining UI and Controller code in MVC is common practice in Rich Clients (in Swing for example). even in web MVC it is sometimes implemented, for very simple responses.
In your case however what you are doing is not recommended. Usually you put the texts of your application in a resource bundle using spring's MessageSource mechanism, and refer to it only using codes. The resource bundle is a simple properties file, in your case it will look like this:
case1=Case 1 status message
case2=Case 2 status message
In the JSP you refer to it using the tag in the following manner:
<spring:message message="${status}"/>
Resource bundles have two advantages:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With