Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 5
A lot of changes have been made to the Struts configuration file. Perhaps the most obvious is its new name: in Struts 2, the file is now referred to as struts.xml instead of struts-config.xml. Listing 3 illustrates a sample Struts 1 configuration file.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="formDataForm"
type="com.demo.form.FormDataForm">
</form-bean>
</form-beans>
<!-- Action Mapping Definitions -->
<action-mappings>
<action path="/formData"
name="formDataForm"
type="com.demo.action.FormDataAction"
scope="request"
validate="false">
<forward name="success" path="/jspUserProfile.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationMessageResources" null="false"/>
</struts-config>
The corresponding configuration file for Struts 2 is simpler: it has fewer elements and the elements have fewer attributes.
There is no concept of FormBeans in Struts 2. The properties can now be defined in the Action class directly. Listing 4 illustrates the the Struts 2 struts.xml configuration file.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
<include file="struts-default.xml"/>
<package name="form-default" extends="struts-default">
<action name="FormData" class=" com.demo.action.FormData">
<result>/jspUserProfile.jsp</result>
</action>
</package>
</struts>
The configuration file has been changed in many ways. The DTD must be Struts 2-compliant. The root element in the configuration
file is <struts>, and it must include a file called struts-default.xml to inherit the default behavior. Because there is no support for FormBeans, <form-beans> and its related tags are removed. <package> tags replace the <action-mappings> tags. The <action> tag attributes have changed. The <forward> element is replaced by the <result> element. All these changes have been introduced to maintain consistency across the framework so that it is on par with other
frameworks such as Spring and iBATIS. These changes have been introduced to take advantage of the many new features in Version
2 of the framework. For example, the configuration file utilizes the advantage of intelligent defaults, where the result name
defaults to "success."
The ActionForm classes no longer exist in the Struts 2 framework. These classes have become obsolete, and whatever had been written in the
ActionForm class is now part of the Action class itself.
The Action class has become simpler in the Struts 2 framework. Take a look at Listing 5, which illustrates a Struts 1 Action class.
package com.demo.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class FormDataAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
request.setAttribute("FORMDATA", form);
return (mapping.findForward("sucess"));
}
}
The Action class in Struts 2 generally extends from ActionSupport and may optionally implement the Action interface. The advantage here is that any POJO can be used as an Action. Also, the Action class in Struts 2 is not a singleton; an Action class is initiated for each request. Hence, it need not be thread safe and can have member variables. Listing 6 illustrates
the Action class in Struts 2. It corresponds to the Struts 1 code in Listing 5, and it also incorporates the functionality that in Struts
1 would be encapsulated in an ActionForm class.
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class FormData extends ActionSupport {
public String execute() throws Exception {
// Code having the processing logic
...........
...........
...........
return (SUCCESS);
}
private String strFirstName="";
private String strLastName="";
public void setFirstName(String strFirstName) {
this.strFirstName = strFirstName;
}
public String getFirstName() {
return this.strFirstName;
}
public void setLastName(String strLastName) {
this.strLastName = strLastName;
}
public String getLastName() {
return this.strLastName;
}
}
The bean tags supported in Struts 1 have been completely replaced by <s /> tags. The new tags provide a number of improvements over Struts 1 tags. Struts 2 framework tags can be generally broken into
two categories: generic tags and UI tags.
Generic tags are used to handle the execution flow when the pages are rendered, and for data extraction as well. These are further broken
down into control tags, which are used for flow control functions such as if, else, append, iterate and merge, and data tags, which are used for data manipulation, internationalization, localization, beans, bean properties, and so on. Generic tags
generally output tag content directly.
UI tags are mainly designed to use the data retrieved from the data tags. These tags are generally used to display the data on an HTML page. They are further broken down into form tags, which are used to specify form elements, and non-form tags, which are used to specify an error in an action, an error in a particular field, or advanced UI elements like trees or tables. UI tags use templates and themes, and their output is usually a combined display.
Listing 7 demonstrates a simple example using Struts 1 bean tags.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>This is a demo!</title>
</head>
<body>
<h2><bean:write name="DemoForm" property="message" /></h2>
</body>
</html>
In Listing 8, the same example has been transformed to use Struts 2 tags.
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>This is a demo!</title>
</head>
<body>
<h2><s:property value="message"/></h2>
</body>
</html>
In this example, to transform the code, the value for the @taglib directive has been replaced with Struts tags. The advantage here is that the Struts tags automatically find the property;
thus, there is no need to specify the ActionForm.
This is a fairly simple example, and details of each tag is beyond the scope of this article. Please refer to Apache Struts documentation (see Resources) for more details on Struts tags.
The Struts 2 framework features a simple architecture that is not hard to extend. It is smarter and easier to use than Struts
1 because it is POJO based. It also boasts a number of advantages over the previous release. The Struts 2 framework comes
with rich set of features, such as enhanced Actions, enhanced tag support, support for Ajax, and support for annotations, all of which can be really helpful to the larger developer
community. However, Struts 1 cannot be made obsolete immediately, as there are plenty of developers still using it.
Migrating from Struts 1 to Struts 2 is not very difficult, but it involves some effort. The easiest approach to migration would be migrating one page at a time. Even though this is a simple idea, there are still many changes to make. We recommend that you use Struts 2 for new projects and continue to use Struts 1 for existing applications already written for that framework. However, both versions of the framework can co-exist within a single application.
This article hasn't covered everything related to Struts 2. However, it hopefully has offered an overall idea of the new features and reasons you might want to migrate to or adopt the Struts 2 framework.
The authors would like to acknowledge Jagdish Bhandarkar for reviewing this article.
S. Sangeetha works as a technical architect at the E-Commerce Research Labs at Infosys Technologies Ltd. She has over eight years of experience in design and development of Java and JEE applications. She has co-authored a book on J2EE architecture and also has written articles for JavaWorld and java.net.
S. V. Subrahmanya, popularly known as SVS, is vice president at Infosys Technologies and the founder of the E-Commerce Research Labs. He has coauthored books on Web services, JEE architecture, and enterprise IT architecture. He has published over 15 papers at international conferences and has written articles for JavaWorld and java.net. He has also filed two patents at USPTO.
Archived Discussions (Read only)