org.springframework.web.servlet.mvc
public class: ServletForwardingController [javadoc |
source]
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
org.springframework.web.servlet.mvc.AbstractController
org.springframework.web.servlet.mvc.ServletForwardingController
All Implemented Interfaces:
BeanNameAware, Controller, ServletContextAware, ApplicationContextAware
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>
| Methods from org.springframework.web.servlet.support.WebContentGenerator: |
|---|
|
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseCacheControlNoStore, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseCacheControlNoStore, setUseExpiresHeader |
| Method from org.springframework.web.servlet.mvc.ServletForwardingController Detail: |
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;
}
|
public void setBeanName(String name) {
this.beanName = name;
if (this.servletName == null) {
this.servletName = name;
}
}
|
public void setServletName(String servletName) {
this.servletName = servletName;
}
|
protected boolean useInclude(HttpServletRequest request,
HttpServletResponse response) {
return (WebUtils.isIncludeRequest(request) || response.isCommitted());
}
Determine whether to use RequestDispatcher's include or
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. |