Difference between Struts 2.3.x and 2.5.x


If you planning to migrate your current Struts 2.3.x project to the newer version of Struts 2.5.x, you must have to keep these points in your mind before proceed.

The newer version of Apache Struts have been modified few things in core as well as UI.

Dependencies

The very first step is update the JAR dependencies of the project. To get the JAR of Struts 2.5.x, you can get it from multiple ways.

If you are a Maven user, you can update the following dependency in you pom.xml

pom.xml
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.13</version>
</dependency>

For the Gradle user, add the following script into the build.gradle

build.gradle
compile group: 'org.apache.struts', name: 'struts2-core', version: '2.5.13'

And you can also download it form the Apache Struts website. To get the latest JAR click here.

StrutsPrepareAndExecuteFilter

Inside the web.xml filter of Struts 2.5.x has been changed. Basically the package name was renamed and drop the ng from package. New class name look like org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter. You can compare the both filters below.

Struts 2.3.x web.xml.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

Struts 2.5.x web.xml.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

Document Type Definition (DTD)

Struts DTD was updated to 2.5 version.

In struts.xml replace 2.3 DTD version:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

with 2.5:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

A document type definition (DTD) is a set of markup declarations that define a document type for an SGML-family markup language (SGML, XML, HTML).

A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes.
A DTD can be declared inline inside an XML document, or as an external reference. – Wikipedia

HTML 5

All the core themes are now HTML 5 compliant which means using a required attribute in your tags will produce a proper browser’s validation.

Tags Attributes

Struts 2.5.x UI tags attribute id was replaced with var attribute in the following tags.

  • <s:action>
  • <s:append>
  • <s:bean>
  • <s:date>
  • <s:generator>
  • <s:iterator>
  • <s:merge>
  • <s:number>
  • <s:set>
  • <s:sort>
  • <s:subset>
  • <s:text>
  • <s:url>

If you have something like that in your code:

<s:url id="url" action="login">

change it to:

<s:url var="url" action="login">

The <s:set> tag name attribute is replaced with var attribute.

From:

<s:set id="str1" value="'string1 value'" />
<s:set name="str2" value="'string2 value'" />

to:

<s:set var="str1" value="'string1 value'" />
<s:set var="str2" value="'string2 value'" />

Also escape attribute was renamed to escapeHtml attribute.

From:

<s:property escape="true" var="someProperty"/>

to:

<s:property escapeHtml="true" var="someProperty"/>

Div Tag

The <s:div> tag was dropped.

Replace <s:div> with plain HTML <div> tag.

Tiles

Depending on from which version of struts you upgrade and whether you used tiles-plugin or tiles3-plugin you may need to do different steps.

Struts 2.5 just provides a tiles-plugin which uses Tiles3. So support for Tiles2 has been dropped as well as the name tiles3-plugin.

Now the only maven dependency looks like this:

pom.xml
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-tiles-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>

You may need to update DTD in your tiles.xml files to Tiles3:

tiles.xml
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

A Listener in web.xml is required. It is not necessary to configure paths to tiles.xml files here as they are picked up automatically.

web.xml
<listener>
  <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

Package Names Changed

Some classes were moved to different packages, see the list below for more details:

New:

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

org.apache.struts2.dispatcher.filter.StrutsExecuteFilter

org.apache.struts2.dispatcher.filter.StrutsPrepareFilter

org.apache.struts2.dispatcher.listener.StrutsListener

org.apache.struts2.result.ServletRedirectResult

com.opensymphony.xwork2.interceptor.ValidationAware

Old:

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter

org.apache.struts2.dispatcher.ng.listener.StrutsListener

org.apache.struts2.dispatcher.ServletRedirectResult

com.opensymphony.xwork2.ValidationAware


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.