001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet;
018:
019: import java.io.IOException;
020: import java.util.Map;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServlet;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.springframework.web.util.NestedServletException;
028:
029: /**
030: * ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.
031: *
032: * <p>For usage with Portlets, this Servlet is necessary to force the portlet container
033: * to convert the PortletRequest to a ServletRequest, which it has to do when
034: * including a resource via the PortletRequestDispatcher. This allows for reuse
035: * of the entire Servlet-based View support even in a Portlet environment.
036: *
037: * <p>The actual mapping of the bridge servlet is configurable in the DispatcherPortlet,
038: * via a "viewRendererUrl" property. The default is "/WEB-INF/servlet/view", which is
039: * just available for internal resource dispatching.
040: *
041: * @author William G. Thompson, Jr.
042: * @author John A. Lewis
043: * @author Juergen Hoeller
044: * @since 2.0
045: */
046: public class ViewRendererServlet extends HttpServlet {
047:
048: /**
049: * Request attribute to hold current web application context.
050: * Otherwise only the global web app context is obtainable by tags etc.
051: * @see org.springframework.web.servlet.support.RequestContextUtils#getWebApplicationContext
052: */
053: public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE;
054:
055: /** Name of request attribute that holds the View object */
056: public static final String VIEW_ATTRIBUTE = ViewRendererServlet.class
057: .getName()
058: + ".VIEW";
059:
060: /** Name of request attribute that holds the model Map */
061: public static final String MODEL_ATTRIBUTE = ViewRendererServlet.class
062: .getName()
063: + ".MODEL";
064:
065: protected final void doGet(HttpServletRequest request,
066: HttpServletResponse response) throws ServletException,
067: IOException {
068:
069: processRequest(request, response);
070: }
071:
072: protected final void doPost(HttpServletRequest request,
073: HttpServletResponse response) throws ServletException,
074: IOException {
075:
076: processRequest(request, response);
077: }
078:
079: /**
080: * Process this request, handling exceptions.
081: * The actually event handling is performed by the abstract
082: * <code>renderView()</code> template method.
083: * @see #renderView
084: */
085: protected final void processRequest(HttpServletRequest request,
086: HttpServletResponse response) throws ServletException,
087: IOException {
088:
089: try {
090: renderView(request, response);
091: } catch (ServletException ex) {
092: throw ex;
093: } catch (IOException ex) {
094: throw ex;
095: } catch (Exception ex) {
096: throw new NestedServletException("View rendering failed",
097: ex);
098: }
099: }
100:
101: /**
102: * Retrieve the View instance and model Map to render
103: * and trigger actual rendering.
104: * @param request current HTTP request
105: * @param response current HTTP response
106: * @throws Exception in case of any kind of processing failure
107: * @see org.springframework.web.servlet.View#render
108: */
109: protected void renderView(HttpServletRequest request,
110: HttpServletResponse response) throws Exception {
111: View view = (View) request.getAttribute(VIEW_ATTRIBUTE);
112: if (view == null) {
113: throw new ServletException(
114: "Could not complete render request: View is null");
115: }
116: Map model = (Map) request.getAttribute(MODEL_ATTRIBUTE);
117: view.render(model, request, response);
118: }
119:
120: }
|