Introduction: This article shows how to
create a simple struts application. I have demonstrated this article with a
simple calculation example developed using NetBeans 6.0.1. Other versions of
NetBeans can also be used to do the same.
The following are the steps required to create the application:
Step 1 : Create a Web application by selecting the Web Application
project from the Web category and click Next.
Step 2 : Type the project name and project location and keep all other
options default as shown in the following figure.
Step 3 : Select the Struts 1.2.9 checkbox as shown in the following
figure. Also select the Add Struts TLDs checkbox and click Finish.
Step 4 : From the Projects window open the struts-config.xml file.
Step 5 : Edit the form-beans and action-mappings as follows:
A form bean is a class that specifies the data of our application. In the form
bean class, we have to declare private properties and accessor (get) and mutator
(set) methods for each property.
In the form bean element, we specify the name and type attributes for our form
bean. The name attribute specifies the name of our form bean and the type
attribute specifies the fully qualified name (package.class) of our form bean.
The action element specifies the attributes of our action class like path, type,
name and input. The forward element specifies the success path for our
application.
All other elements in the struts-config.xml file need not be changed.
Step 6 : Add a new Java Class to the project giving it the class name and
package name as specified in the form-bean element and click Finish. This class
represents our form bean.
Step 7 : Write the following code in the CalcForm class:
package calc;
import
org.apache.struts.action.ActionError;
import
org.apache.struts.action.ActionErrors;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionMapping;
public
class
CalcForm
extends
ActionForm
{
private
int
n1,n2,addition,subtraction;
public
void
setN1(int
n1)
{
this.n1=n1;
}
public
int
getN1()
{
return
n1;
}
public
void
setN2(int
n2)
{
this.n2=n2;
}
public
int
getN2()
{
return
n2;
}
public
void
setAddition(int
addition)
{
this.addition=addition;
}
public
int
getAddition()
{
return
addition;
}
public
void
setSubtraction(int
subtraction)
{
this.subtraction=subtraction;
}
public
int
getSubtraction()
{
return
subtraction;
}
public
ActionErrors validate(ActionMapping mapping,HttpServletRequest
request)
{
ActionErrors errors=new
ActionErrors();
if(n1==0)
{
errors.add("error.n1",new
ActionError("error.n1"));
}
if(n2==0)
{
errors.add("error.n2",new
ActionError("error.n2"));
}
return
errors;
}
}
The form bean class contains the definitions for the private properties and the
corresponding set and get methods. The validate method is used to handle errors
in the program.
Step 8 : From the projects window edit the ApplicationResource.properties
file.
Step 9 : Add the following code in the file:
These errors will be displayed when data is not entered while executing the
application.
Step 10 : Now from the Projects window add a new Java Class as specified
in the action-mappings element as follows and click Finish:
Step 11 : Write the following code in the class:
package calc;
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
MyAction
extends
Action
{
public
ActionForward execute(ActionMapping mapping,ActionForm
form,HttpServletRequest request,HttpServletResponse response)
{
CalcForm calcForm=(CalcForm)form;
int
n1=calcForm.getN1();
int
n2=calcForm.getN2();
calcForm.setAddition(n1+n2);
calcForm.setSubtraction(n1-n2);
return
mapping.findForward("success");
}
}
This program represents the action controller. It retrieves data from the form
bean, performs the calculation and stores the results in the form bean.
Step 12 : From the Projects window, edit the index.jsp file under the Web
Pages folder.
Step 13 : Add the following code to the index.jsp file:
The taglib directive is used to include the struts html library. The prefix
attribute specifies the prefix to be used and the uri attribute specifies the
html library.
We create the user interface using the html library.
Step 14 : Execute the application. Following are the output windows:
If the input is not specified, you get error as follows: