Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring/JSP, where should formatting be performed?

I'm using Spring, but this question applies to all JSP-controller type designs.

The JSP page references data (using tags) which is populated by the corresponding controller. My question is, where is the appropriate place to perform formatting, in JSP or the controller?

So far I've been preparing the data by formatting it in my controller.

public class ViewPersonController extends org.springframework.web.servlet.mvc.AbstractController
{
    private static final Format MY_DATE_FORMAT = new SimpleDateFormat(...);
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    {
        Person person = get person from backing service layer or database
        Map properties = new HashMap();

        // No formatting required, name is a String
        properties.put("name", person.getName());

        // getBirthDate() returns Date and is formatted by a Format
        properties.put("birthDate", MY_DATE_FORMAT.format(person.getBirthDate()));

        // latitude and longitude are separate fields in Person, but in the UI it's one field
        properties.put("location", person.getLatitude() + ", " + person.getLongitude());

        return new ModelAndView("viewPerson", "person", properties);
    }
}

The JSP file would look something like:

Name = <c:out value="${person. name}" /><br>
Birth Date = <c:out value="${person. birthDate}" /><br>
Location = <c:out value="${person. location}" /><br>

I know that JSP does have some provisions for formatting,

<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<fmt:formatDate type="date" value="${person. birthDate}" />

But this only works with Java's java.util.Format. What if I need more complex or computed values. In such a case putting the code in the JSP would be cumbersome (and ugly).

I'm curious if this is following the spirit Spring/JSP/MVC. In other words, is the controller part of the view? Where is the preferred place to perform view related formatting? Should my controller just be returning the object (Person) instead of a Map of formatted values?

like image 771
Steve Kuo Avatar asked Dec 12 '08 00:12

Steve Kuo


People also ask

Where do I put JSP in spring boot?

As per convention, we place our JSP files in the ${project. basedir}/main/webapp/WEB-INF/jsp/ directory.

What is Formatter in spring?

Spring Formatters come into picture to format the data according to the display where it is rendered. Examples may include formatting date/timestamp values according to locales etc.

Which format will support JSP in spring boot Microservices?

Default embedded servlet container for Spring Boot Starter Web is tomcat. To enable support for JSP's, we would need to add a dependency on tomcat-embed-jasper.

What is JSP in Spring Framework?

One of the view technologies you can use with the Spring Framework is Java Server Pages (JSPs). To help you implement views using Java Server Pages the Spring Framework provides you with some tags for evaluating errors, setting themes and outputting internationalized messages.


3 Answers

JSPs typically do not have a lot (or any?) code in them, so your options would be

  • controller
  • tag libraries

I would say that a tag library would probably be what you want for most cases, because typically the view is the code that cares about things like formatting.

If standard tag libraries don't get you there, they are not hard to create, so you can roll your own.

like image 73
davetron5000 Avatar answered Sep 30 '22 22:09

davetron5000


I typically do formatting, etc. in the bean or a view "helper". This has several advantages including the following:

  1. Easier to test
  2. Flexibility to change your view technologies without worrying about porting or rewriting what you've done in custom tablibs.
  3. Cleaner and easier to maintain controller and view code.
like image 21
digitalsanctum Avatar answered Oct 01 '22 00:10

digitalsanctum


I prefer to consider formatting part of the display layer, thus done in the JSP. I used Velocity most recently, but same idea with JSP: controller returns a data model, and the view is responsible for rendering that data into a visible representation. Plenty of JSP tag libraries out there for common needs.

You mention complex or computed values. Those sound like elements of the results data model to me, so should be done in the controller, even if they can in principle be determined by other data, such as sum, max and other aggregate values. By formatting in the view I mean basic things like date and number formats, line splitting, alignment. Of course the exact line between data and formatted representation depends on the application, but I think you get the idea.

like image 30
Dov Wasserman Avatar answered Sep 30 '22 23:09

Dov Wasserman