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.

<dependency>
    <groupId>org.hdiv.ee</groupId>
    <artifactId>hdiv-ee-config</artifactId>
    <version>${org.hdiv-ee-version}</version>
</dependency>
<dependency>
    <groupId>org.hdiv.ee</groupId>
    <artifactId>hdiv-ee-spring-mvc</artifactId>
    <version>${org.hdiv-ee-version}</version>
</dependency>

Note

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 -->
<listener>
    <listener-class>org.hdiv.listener.InitListener</listener-class>
</listener>
<!-- Hdiv Validator Filter -->
<filter>
    <filter-name>ValidatorFilter</filter-name>
    <filter-class>org.hdiv.filter.ValidatorFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ValidatorFilter</filter-name>
    <servlet-name>spring-web</servlet-name>
</filter-mapping>
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 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dataAccess-config.xml
        /WEB-INF/security-config.xml
        /WEB-INF/hdiv-config.xml
    </param-value>
</context-param>
  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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdiv="http://www.hdiv.org/schema/hdiv-ee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.hdiv.org/schema/hdiv-ee
http://www.hdiv.org/schema/hdiv/hdiv-ee.xsd">
    <!-- Hdiv Base Configuration -->
    <hdiv:config excludedExtensions="css,png,gif,js,woff">
        <hdiv:startPages>/</hdiv:startPages>
    </hdiv:config>
    <!-- Own Editable Validation -->
    <hdiv:editableValidations>
        <hdiv:validationRule url="/.*" />
    </hdiv:editableValidations>
</beans>
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" />
</bean>
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:

<jsp-config>
    <taglib>
        <taglib-uri>http://www.springframework.org/tags/form</taglib-uri>
        <taglib-location>/WEB-INF/tlds/hdiv-spring-form-3_0_4.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>http://www.springframework.org/tags</taglib-uri>
        <taglib-location>/WEB-INF/tlds/hdiv-spring.tld</taglib-location>
    </taglib>
</jsp-config>

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)

@Override
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 {
    super.onStartup(container);
    container.addFilter("ValidatorFilter", ValidatorFilter.class)
        .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,false,"/*");
    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:

@Autowired
@Qualifier("hdivEditableValidator")
private Validator hdivEditableValidator ;

@Override
public Validator getValidator() {
    return hdivEditableValidator ;
}

@Bean
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;

@Configuration
@EnableHdivWebSecurity
public class HdivSecurityConfig extends HdivWebSecurityConfigurerAdapter {

    @Override
    public void configure (SecurityConfigBuilder builder) {
        builder.confidentiality(false)
            .sessionExpired()
            .homePage("/")
            .loginPage("/login");
        builder.reuseExistingPageInAjaxRequest(true);
    }

    @Override
    public void addExclusions (ExclusionRegistry registry) {
        registry.addUrlExclusions("/resources/.*");
        registry.addUrlExclusions("/", "/login", "/j_spring_security_logout").method("GET");
        registry.addUrlExclusions("/j_spring_security_check").method("POST");
    }

    @Override
    public void configureEditableValidation (ValidationConfigurer validationConfigurer) {
        validationConfigurer.addValidation("/.*");
    }
}