KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > ServletForwardingController


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.mvc;
18
19 import javax.servlet.RequestDispatcher JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.springframework.beans.factory.BeanNameAware;
25 import org.springframework.web.servlet.ModelAndView;
26 import org.springframework.web.util.WebUtils;
27
28 /**
29  * Spring Controller implementation that forwards to a named servlet,
30  * i.e. the "servlet-name" in web.xml rather than a URL path mapping.
31  * A target servlet doesn't even need a "servlet-mapping" in web.xml
32  * in the first place: A "servlet" declaration is sufficient.
33  *
34  * <p>Useful to invoke an existing servlet via Spring's dispatching infrastructure,
35  * for example to apply Spring HandlerInterceptors to its requests. This will work
36  * even in a Servlet 2.2 container that does not support Servlet filters.
37  *
38  * <p>In particular, the main intent of this controller is to allow for applying
39  * Spring's OpenSessionInViewInterceptor or OpenPersistenceManagerInViewInterceptor
40  * to servlets in a Servlet 2.2 container. The specified "servlet-name" will
41  * simply refer to a custom servlet definition in web.xml in such a scenario.
42  * You then need to map "/myservlet" (or whatever path you choose for your servlet)
43  * onto this controller, which will in turn forward to your servlet.
44  *
45  * <p>In a Servlet 2.3 container, when not using Spring's own web MVC framework,
46  * it is recommended to use classic servlet mapping in combination with a filter,
47  * for example Spring's OpenSessionInViewFilter or OpenPersistenceManagerInViewFilter.
48  *
49  * <p><b>Example:</b> web.xml, mapping all "/myservlet" requests to a Spring dispatcher.
50  * Also defines a custom "myServlet", but <i>without</i> servlet mapping.
51  *
52  * <pre>
53  * &lt;servlet&gt;
54  * &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt;
55  * &lt;servlet-class&gt;mypackage.TestServlet&lt;/servlet-class&gt;
56  * &lt;/servlet&gt;
57  *
58  * &lt;servlet&gt;
59  * &lt;servlet-name&gt;myDispatcher&lt;/servlet-name&gt;
60  * &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
61  * &lt;/servlet&gt;
62  *
63  * &lt;servlet-mapping&gt;
64  * &lt;servlet-name&gt;myDispatcher&lt;/servlet-name&gt;
65  * &lt;url-pattern&gt;/myservlet&lt;/url-pattern&gt;
66  * &lt;/servlet-mapping&gt;</pre>
67  *
68  * <b>Example:</b> myDispatcher-servlet.xml, in turn forwarding "/myservlet" to your
69  * servlet (identified by servlet name). All such requests will go through the
70  * configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor).
71  * From the servlet point of view, everything will work as usual.
72  *
73  * <pre>
74  * &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
75  * &lt;property name="interceptors"&gt;
76  * &lt;list&gt;
77  * &lt;ref bean="openSessionInViewInterceptor"/&gt;
78  * &lt;/list&gt;
79  * &lt;/property&gt;
80  * &lt;property name="mappings"&gt;
81  * &lt;props&gt;
82  * &lt;prop key="/myservlet"&gt;myServletForwardingController&lt;/prop&gt;
83  * &lt;/props&gt;
84  * &lt;/property&gt;
85  * &lt;/bean&gt;
86  *
87  * &lt;bean id="myServletForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController"&gt;
88  * &lt;property name="servletName"&gt;&lt;value&gt;myServlet&lt;/value&gt;&lt;/property&gt;
89  * &lt;/bean&gt;</pre>
90  *
91  * @author Juergen Hoeller
92  * @since 1.1.1
93  * @see ServletWrappingController
94  * @see org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor
95  * @see org.springframework.orm.hibernate.support.OpenSessionInViewFilter
96  * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor
97  * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
98  */

99 public class ServletForwardingController extends AbstractController implements BeanNameAware {
100
101     private String JavaDoc servletName;
102
103     private String JavaDoc beanName;
104     
105
106     /**
107      * Set the name of the servlet to forward to,
108      * i.e. the "servlet-name" of the target servlet in web.xml.
109      * <p>Default is the bean name of this controller.
110      */

111     public void setServletName(String JavaDoc servletName) {
112         this.servletName = servletName;
113     }
114
115     public void setBeanName(String JavaDoc name) {
116         this.beanName = name;
117         if (this.servletName == null) {
118             this.servletName = name;
119         }
120     }
121
122
123     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
124             throws Exception JavaDoc {
125
126         RequestDispatcher JavaDoc rd = getServletContext().getNamedDispatcher(this.servletName);
127         if (rd == null) {
128             throw new ServletException JavaDoc("No servlet with name '" + this.servletName + "' defined in web.xml");
129         }
130         // If already included, include again, else forward.
131
if (useInclude(request, response)) {
132             rd.include(request, response);
133             if (logger.isDebugEnabled()) {
134                 logger.debug("Included servlet [" + this.servletName +
135                         "] in ServletForwardingController '" + this.beanName + "'");
136             }
137         }
138         else {
139             rd.forward(request, response);
140             if (logger.isDebugEnabled()) {
141                 logger.debug("Forwarded to servlet [" + this.servletName +
142                         "] in ServletForwardingController '" + this.beanName + "'");
143             }
144         }
145         return null;
146     }
147
148     /**
149      * Determine whether to use RequestDispatcher's <code>include</code> or
150      * <code>forward</code> method.
151      * <p>Performs a check whether an include URI attribute is found in the request,
152      * indicating an include request, and whether the response has already been committed.
153      * In both cases, an include will be performed, as a forward is not possible anymore.
154      * @param request current HTTP request
155      * @param response current HTTP response
156      * @return <code>true</code> for include, <code>false</code> for forward
157      * @see javax.servlet.RequestDispatcher#forward
158      * @see javax.servlet.RequestDispatcher#include
159      * @see javax.servlet.ServletResponse#isCommitted
160      * @see org.springframework.web.util.WebUtils#isIncludeRequest
161      */

162     protected boolean useInclude(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
163         return (WebUtils.isIncludeRequest(request) || response.isCommitted());
164     }
165
166 }
167
Popular Tags