Is it wrong to use the view code in the controller?

In MVC (like JSP and Spring), is it bad practice to view related code in a controller?

In my case, the controller does some work and then passes the results to the view (JSP). In the case of a status message, I can pass the entire text of the message to the view or pass the key and let the JSP match it with the message text.

Example:

The message generated in the 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}

Generated message

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 the JSP code are simpler, but the corresponding logic is visible in the controller. In the second case, all the viewing logic is in the JSP, but the code is not so simple.

Am I breaking the MVC paradigm by generating message text in the controller? What is common practice for this scenario?

+5
3

MVC ​​Rich Client (, Swing). -MVC .

, , , , . , spring MessageSource . - , :

case1=Case 1 status message
case2=Case 2 status message

JSP :

<spring:message message="${status}"/>

:

+3

:-) Spring message, .

+6

, . , , , - , . , .

, , , , , , Excel.

JSP ( HTML), Excel ( ).

, view agnostic, .

This does not apply to logic, for example, which decides whether selected text should be bold or not. This is specific viewing logic. HTML uses sylesheets to render bold text, while another view engine uses a different view. In this case, the best place to store this logic would be the presentation layer (JSP for HTML representations, etc.).

0
source

All Articles