code_junkie

In Spring/JSP, where should formatting be performed?

Nov 12th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. public class ViewPersonController extends org.springframework.web.servlet.mvc.AbstractController
  2. {
  3. private static final Format MY_DATE_FORMAT = new SimpleDateFormat(...);
  4. protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
  5. {
  6. Person person = get person from backing service layer or database
  7. Map properties = new HashMap();
  8.  
  9. // No formatting required, name is a String
  10. properties.put("name", person.getName());
  11.  
  12. // getBirthDate() returns Date and is formatted by a Format
  13. properties.put("birthDate", MY_DATE_FORMAT.format(person.getBirthDate()));
  14.  
  15. // latitude and longitude are separate fields in Person, but in the UI it's one field
  16. properties.put("location", person.getLatitude() + ", " + person.getLongitude());
  17.  
  18. return new ModelAndView("viewPerson", "person", properties);
  19. }
  20. }
  21.  
  22. Name = <c:out value="${person. name}" /><br>
  23. Birth Date = <c:out value="${person. birthDate}" /><br>
  24. Location = <c:out value="${person. location}" /><br>
  25.  
  26. <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
  27. <fmt:formatDate type="date" value="${person. birthDate}" />
  28.  
  29. public class Person {
  30. public String getLocation() {
  31. return this.latitude.concat(", ").concat(this.longitude);
  32. }
  33. }
Add Comment
Please, Sign In to add comment