All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.servlet.mvc.ServletForwardingController Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2017 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
 *
 *      https://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.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
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.
 *
 * 

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. * *

Example: web.xml, mapping all "/myservlet" requests to a Spring dispatcher. * Also defines a custom "myServlet", but without servlet mapping. * *

 * <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>
* * Example: 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. * *
 * <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>
* * @author Juergen Hoeller * @since 1.1.1 * @see ServletWrappingController * @see org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor * @see org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter */ public class ServletForwardingController extends AbstractController implements BeanNameAware { @Nullable private String servletName; @Nullable private String beanName; public ServletForwardingController() { super(false); } /** * Set the name of the servlet to forward to, * i.e. the "servlet-name" of the target servlet in web.xml. *

Default is the bean name of this controller. */ public void setServletName(String servletName) { this.servletName = servletName; } @Override public void setBeanName(String name) { this.beanName = name; if (this.servletName == null) { this.servletName = name; } } @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ServletContext servletContext = getServletContext(); Assert.state(servletContext != null, "No ServletContext"); RequestDispatcher rd = servletContext.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.isTraceEnabled()) { logger.trace("Included servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'"); } } else { rd.forward(request, response); if (logger.isTraceEnabled()) { logger.trace("Forwarded to servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'"); } } return null; } /** * Determine whether to use RequestDispatcher's {@code include} or * {@code forward} method. *

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} for include, {@code false} 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()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy