KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > ViewRendererServlet


1 /*
2  * Copyright 2002-2006 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
17 package org.springframework.web.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.springframework.web.util.NestedServletException;
28
29 /**
30  * ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.
31  *
32  * <p>For usage with Portlets, this Servlet is necessary to force the portlet container
33  * to convert the PortletRequest to a ServletRequest, which it has to do when
34  * including a resource via the PortletRequestDispatcher. This allows for reuse
35  * of the entire Servlet-based View support even in a Portlet environment.
36  *
37  * <p>The actual mapping of the bridge servlet is configurable in the DispatcherPortlet,
38  * via a "viewRendererUrl" property. The default is "/WEB-INF/servlet/view", which is
39  * just available for internal resource dispatching.
40  *
41  * @author William G. Thompson, Jr.
42  * @author John A. Lewis
43  * @author Juergen Hoeller
44  * @since 2.0
45  */

46 public class ViewRendererServlet extends HttpServlet JavaDoc {
47
48     /**
49      * Request attribute to hold current web application context.
50      * Otherwise only the global web app context is obtainable by tags etc.
51      * @see org.springframework.web.servlet.support.RequestContextUtils#getWebApplicationContext
52      */

53     public static final String JavaDoc WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE;
54
55     /** Name of request attribute that holds the View object */
56     public static final String JavaDoc VIEW_ATTRIBUTE = ViewRendererServlet.class.getName() + ".VIEW";
57
58     /** Name of request attribute that holds the model Map */
59     public static final String JavaDoc MODEL_ATTRIBUTE = ViewRendererServlet.class.getName() + ".MODEL";
60
61
62     protected final void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
63             throws ServletException JavaDoc, IOException JavaDoc {
64
65         processRequest(request, response);
66     }
67
68     protected final void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69             throws ServletException JavaDoc, IOException JavaDoc {
70
71         processRequest(request, response);
72     }
73
74     /**
75      * Process this request, handling exceptions.
76      * The actually event handling is performed by the abstract
77      * <code>renderView()</code> template method.
78      * @see #renderView
79      */

80     protected final void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
81             throws ServletException JavaDoc, IOException JavaDoc {
82
83         try {
84             renderView(request, response);
85         }
86         catch (ServletException JavaDoc ex) {
87             throw ex;
88         }
89         catch (IOException JavaDoc ex) {
90             throw ex;
91         }
92         catch (Exception JavaDoc ex) {
93             throw new NestedServletException("View rendering failed", ex);
94         }
95     }
96
97     /**
98      * Retrieve the View instance and model Map to render
99      * and trigger actual rendering.
100      * @param request current HTTP request
101      * @param response current HTTP response
102      * @throws Exception in case of any kind of processing failure
103      * @see org.springframework.web.servlet.View#render
104      */

105     protected void renderView(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
106         View view = (View) request.getAttribute(VIEW_ATTRIBUTE);
107         if (view == null) {
108             throw new ServletException JavaDoc("Could not complete render request: View is null");
109         }
110         Map JavaDoc model = (Map JavaDoc) request.getAttribute(MODEL_ATTRIBUTE);
111         view.render(model, request, response);
112     }
113
114 }
115
Popular Tags