1 16 17 package org.springframework.web.servlet; 18 19 import java.io.IOException ; 20 import java.util.Map ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.springframework.web.util.NestedServletException; 28 29 46 public class ViewRendererServlet extends HttpServlet { 47 48 53 public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE; 54 55 56 public static final String VIEW_ATTRIBUTE = ViewRendererServlet.class.getName() + ".VIEW"; 57 58 59 public static final String MODEL_ATTRIBUTE = ViewRendererServlet.class.getName() + ".MODEL"; 60 61 62 protected final void doGet(HttpServletRequest request, HttpServletResponse response) 63 throws ServletException , IOException { 64 65 processRequest(request, response); 66 } 67 68 protected final void doPost(HttpServletRequest request, HttpServletResponse response) 69 throws ServletException , IOException { 70 71 processRequest(request, response); 72 } 73 74 80 protected final void processRequest(HttpServletRequest request, HttpServletResponse response) 81 throws ServletException , IOException { 82 83 try { 84 renderView(request, response); 85 } 86 catch (ServletException ex) { 87 throw ex; 88 } 89 catch (IOException ex) { 90 throw ex; 91 } 92 catch (Exception ex) { 93 throw new NestedServletException("View rendering failed", ex); 94 } 95 } 96 97 105 protected void renderView(HttpServletRequest request, HttpServletResponse response) throws Exception { 106 View view = (View) request.getAttribute(VIEW_ATTRIBUTE); 107 if (view == null) { 108 throw new ServletException ("Could not complete render request: View is null"); 109 } 110 Map model = (Map ) request.getAttribute(MODEL_ATTRIBUTE); 111 view.render(model, request, response); 112 } 113 114 } 115 | Popular Tags |