This article covers a Bloomreach Experience Manager version 13. There's anupdated version available that covers our most recent release.
Spring MVC Bridge
Introduction
Goal
Integrate Hippo's delivery tier (HST) with Spring Web MVC.
Use Case
You may have an existing Spring Web MVC application that you want to integrate into your Hippo project. Hippo's delivery tier (a.k.a. HST) enables such integrations through the HST Spring MVC Bridge.
Spring Web MVC Bridge in HST
HST Spring MVC Bridge provides a simple integration with an existing Spring MVC application by using request dispatching.
To see a demo,build andrun the demo inHippo Test Suite project. This project is available in our source code repository:Hippo Test Suite.
Next, click on theContact-SpringMVC link on the left menu of the example website (http://localhost:8080/site/contact-springmvc). You will see a page containing a simple form and when you submit invalid information, e.g.,wicky as email, the page will show validation errors which are generated by the Spring Web MVC Framework. If you then submit valid information, it will show a success view which is defined in the Spring Web MVC configuration.
In summary:
You need to use org.hippoecm.hst.component.support.SimpleDispatcherHstComponent for a HST component class with dispatching URI parameters.
You need to configure your own Spring MVC Framework based application which should be dispatched by the HST component.
You need to configure and use org.hippoecm.hst.component.support.spring.mvc.HstDispatcherServlet instead of the default org.springframework.web.servlet.DispatcherServlet that is provided by the Spring MVC Framework.
You can make use of all features provided by Spring Web MVC Framework such as validating, form controller, etc.!
HstDispatcherServlet Configuration
In your web application, your have probably configured Spring MVC FrameworkDispatcherServlet. In order to use the HST Spring MVC Bridge solution you must replace the default org.springframework.web.servlet.DispatcherServlet with org.hippoecm.hst.component.support.spring.mvc.HstDispatcherServlet.
<!-- SNIP --> <!-- NOTE: Use HstDispatcherServlet instead of the default DispatcherServlet! --> <servlet> <servlet-name>HstDispatcherServlet</servlet-name> <servlet-class>org.hippoecm.hst.component.support.spring.mvc.HstDispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> </servlet> <!-- SNIP --> <servlet-mapping> <servlet-name>HstDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- SNIP -->
The HST Container redirects the request to a new GET URL after processiong an action phase following thePRG pattern.
Therefore,HstDispatcherServlet should extend the defaultDispatcherServlet to do the following:
- After completing the action phase, it stores theModelAndView object intoHttpSession attributes temporarily.
- Before doing render phase, it restores theModelAndView object from theHttpSession attributes if available.
TheHstDispatcherServlet overrides the methodrender(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) to accomplish those.
SimpleDispatcherHstComponent Configuration
You can configure a Spring MVC Bridge component as follows (example configuration taken from Contact-SpringMVC example in Hippo Test Suite):
Component: /hst:hst/hst:configurations/democommon/hst:components/bodycontactspringmvcformpage/content
/hst:hst/hst:configurations/democommon/hst:components/bodycontactspringmvcformpage: /content: jcr:primaryType: hst:component hst:componentclassname: org.hippoecm.hst.component.support.SimpleDispatcherHstComponent hst:parameternames: [action-path] hst:parametervalues: [/spring/contactspringmvc.do] hst:template: contactspringmvc
Template:/hst:hst/hst:configurations/democommon/hst:templates/contactspringmvc
/hst:hst/hst:configurations/democommon/hst:templates: /contactspringmvc: jcr:primaryType: hst:template hst:renderpath: /spring/contactspringmvc.do
In the example above the bridge component will use thecontactspringmvc template in the RENDER phase to render.
But, in the ACTION phase (i.e., submitting a form via an HST Action URL), the bridge component dispatches the request to the/spring/contactspringmvc.do path (as configured by theaction-path parameter) to process the action submission.
After processing the ACTION phase, theHstDispatcherServlet will store theModelAndViewobject returned from the Spring controller and the HST Container will redirect to the original page, using thecontactspringmvctemplate to do the rendering.
Please note that this bridge component (SimpleDispatcherHstComponent) can have additional parameters as follows:
| Name | Description |
| dispatch-path | The default dispatch path, to which the container dispatches on each invocation. |
| action-path | The dispatch path for doAction() invocation of the component. If this is not configured, then 'dispatch-path' would be used instead. |
| before-render-path | The dispatch path for doBeforeRender() invocation of the component. If this is not configured, then 'dispatch-path' would be used instead. |
| render-path | The dispatch path for rendering phase. If this is not configured, then 'dispatch-path' would be used instead. |
| before-resource-path | The dispatch path for doBeforeServeResource() invocation of the component. If this is not configured, then 'dispatch-path' would be used instead. |
| resource-path | The dispatch path for resource serving phase. If this is not configured, then 'dispatch-path' would be used instead. |
The Internals
The internal interaction between the bridge component (SimpleDispatcherHstComponent) and the Spring MVC application can be explained with the following diagram (you can compare it with thediagram for the defaultDispatcherServlet).

If there's an ACTION phase request coming to the component, then it should pass from the step 1 to the step 6. For RENDER phase request, the remaining steps will be processed:
- HST Container process a FORMPOST request and invokes the page containing the bridge component inACTION phase.
- SimpleDispatcherHstComponent dispatches the request to Spring MVC application by the configured URI (e.g, "action-path" HST component parameter), which should map to aController in the Spring MVC application context.
- HstDispatcherServlet invokes a Controller configured in the Spring MVC application context.
- If theController in theACTION phase returns aModelAndView, thenHstDispatcherServlet stores it intoHttpSession for the specific HST Component window (the component window reference name is used for the session attribute key).
- SimpleDispatcherHstComponent finishes theACTION phase processing.
- HST Container redirects to a RENDER URL after processing FORMPOST request.
- HST Container process a GETRENDER request and invokes the page containing the bridge component in RENDER phase.
- SimpleDispatcherHstComponent dispatches the request to Spring MVC application by the configured URI (e.g, in either HST template configuration, "render-path" or "dispatch-path" HST component parameter), which should map to aController in the Spring MVC application context.
- HstDispatcherServlet checks if there exists aModelAndView stored inHttpSession for the specific HST Component window (by using the key based on the component window reference name). If exists, it retrieves theModelAndView attribute and removes it fromHttpSession. And, it invokes a Controller configured in the Spring MVC application context to process the RENDER phase request.
- The Controller may return a newModelAndView, and then HstDispatcherServletrenders theView Template with the model.