Spring MVC


Open video

Thanks to the new extension point included in Spring 3.1, Hdiv installation and configuration for Spring MVC is cleaner and easier than previously.

Add Hdiv Dependencies

Add Hdiv dependencies to the web application project. To do this, add the following dependencies to the pom.xml file.



Replace ${org.hdiv-ee-version} variables with the ones sent by our support team.

There are two alternative ways to configure Hdiv in an application: * Spring XML configuration file * Spring JavaConfig configuration

Choose the best option for your application and follow the corresponding steps.

XML Config

The steps to install Hdiv in an application with Spring MVC are described below. This configuration is made using xml.

Modify the web.xml file to include the Listener and the Hdiv filter:

<!-- Hdiv Init Listener -->
<!-- Hdiv Validator Filter -->
Map the filter to the Servlet of Spring MVC so that Hdiv can process all requests first. It is important that Hdiv's Listener should go after Spring's Listener ContextLoaderListener.

Add the Hdiv configuration file to the rest of Spring configuration files:

<!-- Spring Context Loader -->
  1. Create the Hdiv configuration file. This file is an XML file that follows the format of Spring configuration files. For convention, its name should be hdiv-config.xml, located in the WEB-INF directory and its minimal content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <!-- Hdiv Base Configuration -->
    <hdiv:config excludedExtensions="css,png,gif,js,woff">
    <!-- Own Editable Validation -->
        <hdiv:validationRule url="/.*" />
2. Configuration of Spring MVC.

Hdiv needs changes in the Spring configuration file, for example, spring-web-servlet.xml. Add the Hdiv validator to enable validation of the editable values in the application.

<mvc:annotation-driven validator="hdivEditableValidator" />
Support to multipart requests. For the integration of Hdiv and the file upload, it is necessary to modify the class of the bean multipartResolver. The result is as follows:

<!-- Multipart config with Hdiv support -->
<bean id="multipartResolver" class="org.hdiv.web.multipart.HdivCommonsMultipartResolver">
    <property name="maxUploadSize" value="100000" />
If Servlet 3 standard multipart processing is used:

<bean id="multipartResolver" class="org.hdiv.web.multipart.HdivStandardServletMultipartResolver"/>

If you are using a Spring MVC version prior to 3.1, it is necessary to introduce an additional step, replacing Spring MVC tlds with Hdiv tlds according to the specific version. The following example shows the code for Spring MVC 3.0.4:


JAVA Config

The following is a general tutorial on how to install Hdiv EE in a web application with Java Config. Some familiarity with basic web application concepts is assumed.

In a common Spring MVC Java Config application, a SpringWebInit.java class must be defined. This class usually extends from WebApplicationInitializer.java and two methods have to be modified.

The getRootConfigClasses() method where HdivSecurityConfig.class will be added (this class will be created later)

protected Class<?>[] getRootConfigClasses() {
    return new Class[] { CoreConfig.class, SecurityConfig.class, HdivSecurityConfig.class };
And onStartup() method where Hdiv listener and Hdiv filter will be added.

public void onStartup(ServletContext container) throws ServletException {
    container.addFilter("ValidatorFilter", ValidatorFilter.class)
    container.addListener(new InitListener());

In addition, in order to enable editable validations and multipart with Hdiv, SpringWebMvcConfig.java class, which usually extends from WebMvcConfigurerAdapter.java, has to be modified.

Add these lines to that class:

private Validator hdivEditableValidator ;

public Validator getValidator() {
    return hdivEditableValidator ;

public MultipartResolver multipartResolver() {
    HdivCommonsMultipartResolver resolver = new HdivCommonsMultipartResolver ();
    return resolver;
After this, HdivSecurityConfig.java class has to be created and Hdiv will be configured in this class. Here is an example of this configuration:

package org.hdiv.ee.samples.config;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.hdiv.config.annotation.ExclusionRegistry;
import org.hdiv.config.annotation.ValidationConfigurer;
import org.hdiv.ee.config.annotation.EnableHdivWebSecurity;
import org.hdiv.ee.config.annotation.ExternalStateStorageConfigurer;
import org.hdiv.ee.config.annotation.builders.SecurityConfigBuilder;
import org.hdiv.ee.config.annotation.configuration.HdivWebSecurityConfigurerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class HdivSecurityConfig extends HdivWebSecurityConfigurerAdapter {

    public void configure (SecurityConfigBuilder builder) {

    public void addExclusions (ExclusionRegistry registry) {
        registry.addUrlExclusions("/", "/login", "/j_spring_security_logout").method("GET");

    public void configureEditableValidation (ValidationConfigurer validationConfigurer) {