Struts Application in NetBeans

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.
 
struts application 
 
Step 2: Type the project name and project location and keep all other options default as shown in the following figure.
 
struts in netbeans 
 
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.
 
netbeans 
 
Step 4: From the Projects window open the struts-config.xml file.
 
netbeans struts 
 
Step 5: Edit the form-beans and action-mappings as follows:
 
struts 
 
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.
 
netbeans application 
 
Step 7: Write the following code in the CalcForm class: 
  1. package calc;  
  2.   
  3. import org.apache.struts.action.ActionError;  
  4. import org.apache.struts.action.ActionErrors;  
  5. import org.apache.struts.action.ActionForm;  
  6. import org.apache.struts.action.ActionMapping;  
  7.    
  8. public class CalcForm extends ActionForm  
  9. {  
  10.     private int n1,n2,addition,subtraction;  
  11.     public void setN1(int n1)  
  12.     {  
  13.         this.n1=n1;  
  14.     }  
  15.     public int getN1()  
  16.     {  
  17.         return n1;  
  18.     }  
  19.     public void setN2(int n2)  
  20.     {  
  21.         this.n2=n2;  
  22.     }  
  23.     public int getN2()  
  24.     {  
  25.         return n2;  
  26.     }  
  27.     public void setAddition(int addition)  
  28.     {  
  29.         this.addition=addition;  
  30.     }  
  31.     public int getAddition()  
  32.     {  
  33.         return addition;  
  34.     }  
  35.     public void setSubtraction(int subtraction)  
  36.     {  
  37.         this.subtraction=subtraction;  
  38.     }  
  39.     public int getSubtraction()  
  40.     {  
  41.         return subtraction;  
  42.     }  
  43.     public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)  
  44.     {  
  45.         ActionErrors errors=new ActionErrors();  
  46.         if(n1==0)  
  47.         {  
  48.             errors.add("error.n1",new ActionError("error.n1"));  
  49.         }  
  50.         if(n2==0)  
  51.         {  
  52.             errors.add("error.n2",new ActionError("error.n2"));  
  53.         }  
  54.         return errors;  
  55.     }  
  56. }  
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.
 
struts in java 
 
Step 9: Add the following code in the file:
 
struts8.gif 
 
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:
 
netbeans in java 
 
Step 11: Write the following code in the class:
  1. package calc;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import org.apache.struts.action.Action;  
  6. import org.apache.struts.action.ActionForm;  
  7. import org.apache.struts.action.ActionForward;  
  8. import org.apache.struts.action.ActionMapping;  
  9.    
  10. public class MyAction extends Action  
  11. {  
  12.       public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)  
  13.       {  
  14.             CalcForm calcForm=(CalcForm)form;  
  15.             int n1=calcForm.getN1();  
  16.             int n2=calcForm.getN2();  
  17.             calcForm.setAddition(n1+n2);  
  18.             calcForm.setSubtraction(n1-n2);  
  19.             return mapping.findForward("success");            
  20.       }  
  21. }  
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.
 
java netbeans 
 
Step 13: Add the following code to the index.jsp file:
 
java application 
 
The taglib directive is used to include the struts HTML library. The prefix the 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:
 
 
netbeans application in java 
 
If the input is not specified, you get error as follows: 
 
netbeans application