Home >> Struts
In this writing I shall be showing all the steps I have followed
in order to make a Struts1 based Action class from a web application,
those cane be used in Struts2 Environment.
Let us setup a simple web application where I have designed a
very simple form with a single textfield to accept user input.
This user inputted value goes to a Action class, and then the
Action Forward to another JSP file happens. Simple isn't it!!
For this I have to create a Action class as follows:
action.ExampleAction.java
package action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
public class ExampleAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ExampleForm exampleForm = (ExampleForm) form;
System.out.println("Value of Name:"+exampleForm.getName());
return mapping.findForward("success");
}
}
|
So the action form is ExampleForm as follows:
action.ExampleForm.java
package action;
import org.apache.struts.action.ActionForm;
import java.util.Date;
public class ExampleForm extends ActionForm
{
private String name;
private int age;
private Date dob;
public void setName(String argName) {
name = argName;
}
public String getName() {
return name;
}
public void setAge(int argAge) {
age = argAge;
}
public int getAge() {
return age;
}
public void setDob(Date argDob) {
dob = argDob;
}
public Date getDob() {
return dob;
}
}
|
I remember what i said earlier, there will be a single input textfield
then why are these three fields in Action form. Out of these three
fields I shall be using field "name" only, and the rest two fields
are there as place holder for some further examples on validations.
First screen/page of this example is index.jsp file.
As this JSP file is Struts1 based, so you may see Struts1 related
tag libraries are used here in this file as follows:
index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="index.title.label"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<table>
<html:form method="post" action="example">
<tr><td><bean:message key="index.name.label"/></td>
<td><html:text property="name"/></td></tr>
<tr><td><html:submit property="butSubmit" value="Submit"/></td>
<td><html:button property="butReset" value="Reset"/></td></tr>
</html:form>
</table>
</body>
</html:html>
|
As you can see from the above JSP file we have a action mapping
as "example" is to be defined in struts-config.xml file.
And the action form as "exampleForm" :
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- Action Form definition goes here -->
<form-beans>
<form-bean name="exampleForm" type="action.ExampleForm"/>
</form-beans>
<!-- global exception handling if any -->
<global-exceptions>
</global-exceptions>
<!-- global forward if any -->
<global-forwards>
</global-forwards>
<!-- Action definition goes here -->
<action-mappings>
<action path="/example"
type="action.ExampleAction"
name="exampleForm"
scope="request">
<forward name="success" path="/preview.jsp"/>
</action>
</action-mappings>
<!-- Tiles related request processor (not required in this example) -->
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<!-- MessageResources.properties file for all messages used in UI JSP files. -->
<message-resources parameter="MessageResources" />
<!-- TilesPlugin definitions goes here -->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- Validator Plugin definitions goes here -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
|
This configuration file has the next page as preview.jsp , when the
action forward is "success".
After setting up all the required components for this example web application,
such as all JAR files, web.xml file with configuration for Actionservlet,
tag library files, validation-rules and validation XML files etc.
when this index JSP file is accessed from the Browser, it shows the input
textfield to be entered by user and the on click of submit button, this
value from the textfield goes to the ExampleAction action class, and it
prints out on server console as
"Value of Name: <<Value as entered in text field on screen>>"
This shows that existing Struts 1.x example is all setup properly and functional.
Now is the time to move on to explore Struts1 Plugin with Struts2 framework.
As we might be knowing that we can reuse only the example Action and ActionForm
class (by the time i have written this example)and not all the other files,
such as Struts1 Tag Libraries in JSP files, and related configuration.
If any version of Struts1 Plugin has support for reusing
existing JSP files with Struts 1.x Tag Libraries, then please make us
aware by writing comment on this page.
So we have to move towards creating a web application with Struts2 Framework,
but reuse existing Action and ActionForm from Struts 1.x example as already seen
here.
So we have to write appropriate Struts2 related configuration file, and use
Struts 2 specific Tag Libraries in JSP files.
And we have to define related configuration file for the prepare and excute
filter that is comming with truts2 filter, as shown below:
web.xml
....
....
<filter>
<filter-name>sample-filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sample-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
....
....
|
It is required to provide appropriate Struts2 specific JAR files, including
dependencies. In this example following are the JAR files I have used
to make a single web application using both Strus 1.x version and Struts2
but same set of Action and Action Form.
One may or may not require all these JARs, but for this example I have tested
using following set of JAR files in LIB folder of the web application.

One can observe that corresponding Plugin JAR file "struts2-struts1-plugin-2.1.8.1.jar"
is required to make this example work.
For this example, of course after so many trial and error and fact finding
I could able to put together following Struts Configuration XML file, that
is working for this example.
struts.xml (to be placed in WEB-INF/classes folder only)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="sample-example" namespace="/sample1"
extends="struts1-default">
<interceptors>
<interceptor name="exampleForm"
class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor">
<param name="className">action.ExampleForm</param>
<param name="name">exampleForm</param>
<param name="scope">request</param>
</interceptor>
<interceptor-stack name="sample-example">
<interceptor-ref name="staticParams"/>
<interceptor-ref name="exampleForm"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="sample-example"/>
<action name="example1" class="org.apache.struts2.s1.Struts1Action">
<param name="className">action.ExampleAction</param>
<result name="success">/sample-preview.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
|
In order to start with the struts.xml file, we have the Struts 1.x
Action form is defined as parameter with the name as className
for the scope model driven interceptor.
And the org.apache.struts2.s1.Struts1Action acts like a wrapper for the
Struts1.x Action class (for this example it is action.ExampleAction),
which is defined as param with name className.
In order to create a UI screen, we may have to create another JSP
file using Struts2 based Tag Library, in which appropriate namespace
and action mapping should be referred, as shown below:
sample.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="sample.css" />
</head>
<body>
<struts2:actionerror cssClass="ss"/>
<struts2:form method="post" action="sample1/example1">
<table>
<tr><td><struts2:textfield key="name" label="Name "/></td></tr>
<tr><td colspan="2"><struts2:submit value="Register"/></td></tr>
</table>
</struts2:form>
</body>
</html>
|
Here "sample1" is the namespace and "example1" is the corresponding action
mapping in struts.xml file.
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/Struts-1-Struts2-plugin.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com