Home >> Struts
In this example we shall how to reuse Action and Action Form from Struts 1.x
based web application.
Looking back to the already posted example on using Struts 1.x action and action form
In order to use the validation along with the action and actionform.
First we shall see how user input validation is carried out in case of
a web application that is using Struts 1.x as MVC Architecture.
We have the Action form and Action class from an existing example are 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");
}
}
|
And Action Form as
action.ActionForm.java
package action;
import org.apache.struts.validator.ValidatorForm;
import java.util.Date;
public class ExampleForm extends ValidatorForm
{
private String name;
public ExampleForm() {
}
public void setName(String argName) {
name = argName;
}
public String getName() {
return name;
}
}
|
One quick question:
Can I re-use these two class files for another web application
that is going to use
Struts2 as MVC Framework
Or if an already existing web application using Struts 1.x
MVC Framework to be upgraded to use
Struts2 at some
point of time in the evolving of any web application.
One thing to remember here is that we may not be able to re-use
existing JSP and UI tier files as Tag Library for Struts2 is
different from Struts 1.x Tag Library.
So let us create an example web application by put together various
parts like root folder, lib, classes folders with various JAR file
in lib folder, and struts.xml configuration file in
"ROOT_FOLDER/WEB-INF/classes" folder, following is the folder structure
<<ROOT_FOLDER>>/WEB-INF/web.xml
<<ROOT_FOLDER>>/WEB-INF/lib/antlr.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-beanutils.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-digester.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-fileupload-1.2.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-fileupload.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-io-1.3.2.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-logging-1.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-logging.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-validator-1.3.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/freemarker-2.3.13.jar
<<ROOT_FOLDER>>/WEB-INF/lib/jakarta-oro.jar
<<ROOT_FOLDER>>/WEB-INF/lib/ognl-2.6.11.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts2-core-2.1.6.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts2-struts1-plugin-2.1.8.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts.jar
<<ROOT_FOLDER>>/WEB-INF/lib/xwork-2.1.2.jar
(* these are the files I have used for upgrading
an existing Struts 1.x based web application to Struts2 based MVC Architecture.
Please don't consider this as standard way of upgrading to Struts2
You may have to look at many aspects of any application before upgrade)
You might have noted that there is struts.jar file also present along
with Struts2 core and Struts2 Plugin related JAR file.
This is to make existing Action and ActionForm to work or compile properly
as we are using some of the Java Class Files those are using some of the
class files from struts.jar file.
<<ROOT_FOLDER>>/WEB-INF/classes/struts.xml
<<ROOT_FOLDER>>/WEB-INF/classes/action/ExampleAction.java
<<ROOT_FOLDER>>/WEB-INF/classes/action/ExampleForm.java
<<ROOT_FOLDER>>/WEB-INF/validation.xml
<<ROOT_FOLDER>>/WEB-INF/validation-rules.xml
|
In order to provide ways for including Validation from the Action Form
we have to provide appropriate Interceptor like "ActionFormValidationInterceptor"
This interceptor requires pathnames as param with value as the validation
and validation-rules XML configuration files.
Let us step through the struts.xml file as follows:
struts.xml
<?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>
<!-- Defining struts 1.x base action form with scope -->
<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>
<!-- Example Form definition for the validation with
validator framework -->
<interceptor name="exampleValidation" class="org.apache.struts2.s1.ActionFormValidationInterceptor">
<param name="pathnames">
/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml
</param>
</interceptor>
<!-- Defining required interceptor stack with valious interceptor references -->
<interceptor-stack name="sample-example">
<interceptor-ref name="staticParams"/>
<interceptor-ref name="exampleForm"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="exampleValidation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="sample-example"/>
<!-- Example Action wrapper class from Struts2 plugin -->
<action name="example1"
class="org.apache.struts2.s1.Struts1Action">
<param name="className">action.ExampleAction</param>
<param name="validate">true</param>
<result name="success">/sample-preview.jsp</result>
<result name="input">/sample.jsp</result>
</action>
</package>
</struts>
|
Defining Web Application Archive configuration file is having configuration
for both Struts 1.x ActionServlet and Struts2 "StrutsPrepareAndExecuteFilter"
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>
Struts 1 with Struts 2 and Plugin
</display-name>
<!-- Struts 1.x Actionservlet Definition -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Struts 1.x Tag Library -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>
/WEB-INF/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>
/WEB-INF/struts-html.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>
/WEB-INF/struts-logic.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>
/WEB-INF/struts-nested.tld
</taglib-location>
</taglib>
<!-- Tiles related Tag Library Definition -->
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>
/WEB-INF/struts-tiles.tld
</taglib-location>
</taglib>
<!-- Struts2 Filter for Prepare and Execute Appropriately -->
<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>
</web-app>
|
Now let us look at the validation and validation-rules XML configuration
files,
validation.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<global>
</global>
<formset>
<form name="exampleForm">
<field property="name" depends="required">
<arg0 key="exampleForm.name.text"/>
</field>
</form>
</formset>
</form-validation>
|
Now as per the validation entry shown above, name firld in action form
should be a mandatory field to be entered from UI.
When this example is executed using Tomcat Web server and and from browser
the sample.jsp file is accessed, UI as shown below is seen on screen.

And after clicking submit button, following is the error message

OOPs..... why is this errors.required is showing on screen, i think
it is from the Struts 1.x action error default key, that is getting
shwon on screen, then where should i define the messages so that
Struts2 Plugin will read read appropriate error message.
I can recall from my memory, is that while using Struts 1.x
based web application I defined appropriate error message in the
/WEB-INF/classes/MessageResources.properties file.
I tried putting the same resource bundle properties file in this
example, but no success.
If anyone faced this issue/problem, please write your suggestions
or recomendations below form/section.
Thanks in advance.
If anything missed out , please let me know at
techienjoy at yahoo . com