1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.view;
  17. import java.util.Enumeration;
  18. import java.util.Map;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.HttpSession;
  22. /**
  23. * AbstractTemplateView provides template based view technologies such as
  24. * Velocity and FreeMarker with the ability to use request and session
  25. * attributes in their model.
  26. *
  27. * <p>JSP/JSTL and other view technologies automatically have access to the
  28. * HttpServletRequest object and thereby the request/session attributes
  29. * for the current user.
  30. *
  31. * @author Darren Davison
  32. * @author Juergen Hoeller
  33. * @since May 17, 2004
  34. * @version $Id: AbstractTemplateView.java,v 1.2 2004/05/21 19:33:38 jhoeller Exp $
  35. */
  36. public abstract class AbstractTemplateView extends AbstractUrlBasedView {
  37. private boolean exposeRequestAttributes = false;
  38. private boolean exposeSessionAttributes = false;
  39. /**
  40. * Set whether all request attributes should be added to the
  41. * model prior to merging with the template.
  42. */
  43. public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
  44. this.exposeRequestAttributes = exposeRequestAttributes;
  45. }
  46. /**
  47. * Set whether all HttpSession attributes should be added to the
  48. * model prior to merging with the template.
  49. */
  50. public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
  51. this.exposeSessionAttributes = exposeSessionAttributes;
  52. }
  53. protected final void renderMergedOutputModel(Map model, HttpServletRequest request,
  54. HttpServletResponse response) throws Exception {
  55. if (this.exposeRequestAttributes) {
  56. for (Enumeration enum = request.getAttributeNames(); enum.hasMoreElements();) {
  57. String attribute = (String) enum.nextElement();
  58. Object attributeValue = request.getAttribute(attribute);
  59. if (logger.isDebugEnabled()) {
  60. logger.debug("Exposing request attribute '" + attribute + "' with value [" +
  61. attributeValue + "] to model");
  62. }
  63. model.put(attribute, attributeValue);
  64. }
  65. }
  66. if (this.exposeSessionAttributes) {
  67. HttpSession session = request.getSession(false);
  68. if (session != null) {
  69. for (Enumeration enum = session.getAttributeNames(); enum.hasMoreElements();) {
  70. String attribute = (String) enum.nextElement();
  71. Object attributeValue = session.getAttribute(attribute);
  72. if (logger.isDebugEnabled()) {
  73. logger.debug("Exposing session attribute '" + attribute + "' with value [" +
  74. attributeValue + "] to model");
  75. }
  76. model.put(attribute, attributeValue);
  77. }
  78. }
  79. }
  80. renderMergedTemplateModel(model, request, response);
  81. }
  82. /**
  83. * Subclasses must implement this method to actually render the view.
  84. * @param model combined output Map, with request attributes and
  85. * session attributes merged into it if required
  86. * @param request current HTTP request
  87. * @param response current HTTP response
  88. * @throws Exception if rendering failed
  89. */
  90. protected abstract void renderMergedTemplateModel(Map model, HttpServletRequest request,
  91. HttpServletResponse response) throws Exception;
  92. }