spring-framework / org.springframework.web.servlet.mvc

Package org.springframework.web.servlet.mvc

Types

LastModified

interface LastModified

Supports last-modified HTTP requests to facilitate content caching. Same contract as for the Servlet API's getLastModified method.

Delegated to by a org.springframework.web.servlet.HandlerAdapter#getLastModified implementation. By default, any Controller or HttpRequestHandler within Spring's default framework can implement this interface to enable last-modified checking.

Note: Alternative handler implementation approaches have different last-modified handling styles. For example, Spring 2.5's annotated controller approach (using @RequestMapping) provides last-modified support through the org.springframework.web.context.request.WebRequest#checkNotModified method, allowing for last-modified checking within the main handler method.

ServletForwardingController

open class ServletForwardingController : AbstractController, BeanNameAware

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>

ServletWrappingController

open class ServletWrappingController : AbstractController, BeanNameAware, InitializingBean, DisposableBean

Spring Controller implementation that wraps a servlet instance which it manages internally. Such a wrapped servlet is not known outside of this controller; its entire lifecycle is covered here (in contrast to ServletForwardingController).

Useful to invoke an existing servlet via Spring's dispatching infrastructure, for example to apply Spring HandlerInterceptors to its requests.

Note that Struts has a special requirement in that it parses web.xml to find its servlet mapping. Therefore, you need to specify the DispatcherServlet's servlet name as "servletName" on this controller, so that Struts finds the DispatcherServlet's mapping (thinking that it refers to the ActionServlet).

Example: a DispatcherServlet XML context, forwarding "*.do" to the Struts ActionServlet wrapped by a ServletWrappingController. All such requests will go through the configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor). From the Struts 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="*.do">strutsWrappingController</prop> </props> </property> </bean> <bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController"> <property name="servletClass"> <value>org.apache.struts.action.ActionServlet</value> </property> <property name="servletName"> <value>action</value> </property> <property name="initParameters"> <props> <prop key="config">/WEB-INF/struts-config.xml</prop> </props> </property> </bean>

UrlFilenameViewController

open class UrlFilenameViewController : AbstractUrlViewController

Simple Controller implementation that transforms the virtual path of a URL into a view name and returns that view.

Can optionally prepend a prefix and/or append a suffix to build the viewname from the URL filename.

Find some examples below:

  1. "/index" -> "index"
  2. "/index.html" -> "index"
  3. "/index.html" + prefix "pre_" and suffix "_suf" -> "pre_index_suf"
  4. "/products/view.html" -> "products/view"

Thanks to David Barri for suggesting prefix/suffix support!

WebContentInterceptor

open class WebContentInterceptor : WebContentGenerator, HandlerInterceptor

Handler interceptor that checks the request and prepares the response. Checks for supported methods and a required session, and applies the specified org.springframework.http.CacheControl builder. See superclass bean properties for configuration options.

All the settings supported by this interceptor can also be set on AbstractController. This interceptor is mainly intended for applying checks and preparations to a set of controllers mapped by a HandlerMapping.