Struts 2

Installation and configuration

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

  1. Modify the web.xml file to include the InitListener and ValidatorFilter:

    <!-- 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>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    Set the ValidatorFilter before Struts2 filter so that Hdiv can process all requests in the first place. It is important that Hdiv's Listener should go after Spring's Listener ContextLoaderListener.

    Hdiv requires the use of Spring Framework in the application.

    If the application doesn't use the Spring Framework, add the dependency and Spring's ContextLoaderListener in web.xmlfile.

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            ...Spring configuration files...
        </param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  2. Add the required dependency libraries to your 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-struts-{your-struts-2-version}</artifactId>
        <version>${org.hdiv-struts-2-version}</version>
    </dependency>
    

    Note

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

    The specific hdiv-struts-2.x.x.x version depends on the Struts version the project use. For example, for Struts 2.3.16.3 use hdiv-struts-2.3.16.3.

  3. Add the Hdiv configuration file hdiv-config.xml 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>
    
  4. 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>
    
  5. Configuration of Struts 2.

    5.1. Activate Hdiv plugin using hdiv-default as parent configuration of Struts 2 instead of struts-default. Modify every Struts configuration file to match this requirement.

    Example struts.xml file:

    <package name="default" extends="hdiv-default">
    ...
    </package>
    

    5.2. Include hdiv-struts-applicationContext.xml file in your Spring application context dependencies.

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            ...
            classpath*:org/hdiv/struts2/config/hdiv-struts-applicationContext.xml,
            /WEB-INF/hdiv-config.xml
        </param-value>
    </context-param>
    

    This file is included in the hdiv-struts-2.x.x.x.jar file, so you don’t need to copy it into your project.

    5.3. If your application has a struts.properties file, comment this line:

    struts.multipart.parser=jakarta
    

    The same for struts.xml

    <constant name="struts.multipart.parser" value="jakarta" />
    

    5.4. In order to have a correct Hdiv and Struts 2 integration it is important to check that all link and forms are created using Struts 2 tags, <s:url> for urls and form tags (<s:form>, <s:select>, <s:text>...) for forms.

    In addition to Struts 2, if JSTL tags are used to create url or redirect follow the steps on JSTL installation.