/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.servlet.mvc; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.BeanNameAware; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.util.WebUtils; /** * Spring Controller implementation that forwards to a named servlet, * i.e. the "servlet-name" in web.xml rather than a URL path mapping. * A target servlet doesn't even need a "servlet-mapping" in web.xml * in the first place: A "servlet" declaration is sufficient. * * <p>Useful to invoke an existing servlet via Spring's dispatching infrastructure, * for example to apply Spring HandlerInterceptors to its requests. This will work * even in a minimal Servlet container that does not support Servlet filters. * * <p><b>Example:</b> web.xml, mapping all "/myservlet" requests to a Spring dispatcher. * Also defines a custom "myServlet", but <i>without</i> servlet mapping. * * <pre> * <servlet> * <servlet-name>myServlet</servlet-name> * <servlet-class>mypackage.TestServlet</servlet-class> * </servlet> * * <servlet> * <servlet-name>myDispatcher</servlet-name> * <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> * </servlet> * * <servlet-mapping> * <servlet-name>myDispatcher</servlet-name> * <url-pattern>/myservlet</url-pattern> * </servlet-mapping></pre> * * <b>Example:</b> myDispatcher-servlet.xml, in turn forwarding "/myservlet" to your * servlet (identified by servlet name). All such requests will go through the * configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor). * From the servlet point of view, everything will work as usual. * * <pre> * <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> * <property name="interceptors"> * <list> * <ref bean="openSessionInViewInterceptor"/> * </list> * </property> * <property name="mappings"> * <props> * <prop key="/myservlet">myServletForwardingController</prop> * </props> * </property> * </bean> * * <bean id="myServletForwardingController" class="org.springframework.web.servlet.mvc.ServletForwardingController"> * <property name="servletName"><value>myServlet</value></property> * </bean></pre> * * @author Juergen Hoeller * @since 1.1.1 * @see ServletWrappingController * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter */ public class ServletForwardingController extends AbstractController implements BeanNameAware { private String servletName; private String beanName; /** * Set the name of the servlet to forward to, * i.e. the "servlet-name" of the target servlet in web.xml. * <p>Default is the bean name of this controller. */ public void setServletName(String servletName) { this.servletName = servletName; } public void setBeanName(String name) { this.beanName = name; if (this.servletName == null) { this.servletName = name; } } protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { RequestDispatcher rd = getServletContext().getNamedDispatcher(this.servletName); if (rd == null) { throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml"); } // If already included, include again, else forward. if (useInclude(request, response)) { rd.include(request, response); if (logger.isDebugEnabled()) { logger.debug("Included servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'"); } } else { rd.forward(request, response); if (logger.isDebugEnabled()) { logger.debug("Forwarded to servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'"); } } return null; } /** * Determine whether to use RequestDispatcher's <code>include</code> or * <code>forward</code> method. * <p>Performs a check whether an include URI attribute is found in the request, * indicating an include request, and whether the response has already been committed. * In both cases, an include will be performed, as a forward is not possible anymore. * @param request current HTTP request * @param response current HTTP response * @return <code>true</code> for include, <code>false</code> for forward * @see javax.servlet.RequestDispatcher#forward * @see javax.servlet.RequestDispatcher#include * @see javax.servlet.ServletResponse#isCommitted * @see org.springframework.web.util.WebUtils#isIncludeRequest */ protected boolean useInclude(HttpServletRequest request, HttpServletResponse response) { return (WebUtils.isIncludeRequest(request) || response.isCommitted()); } }