How to configure & create sample struts 2 project in eclipse? A Step by Step guide.

In this article, let us see step by step guide to configure struts 2 in eclipse and create a sample web application.
The requirements to start a strut2 project are

1. Struts 2 Jars
2. Java requirements : Java 1.5 or above , Servlet API 2.4 or above , JSP API 2.0 or above
3. Latest Eclipse with Apache Tomcat server

First let us see how to configure struts 2 for your new web application

1. Add struts2 jars to java build path. Download struts 2 full distribution (struts-2.3.16.3-all.zip) and extract the same in a folder that has many jars in the lib folder.  Jars required for your project may vary depending upon your project requirement.

Minimum jars required for any struts 2  web project creation.
1. struts2-core-2.3.16.3.jar
2. commons-logging-1.1.3.jar
3. commons-logging-api-1.1.jar
4. commons-lang3-3.1.jar
5. freemarker-2.3.19.jar
6. ognl-3.0.6.jar
7. commons-fileupload-1.3.1.jar
8. commons-io-2.2.jar
9. javassist-3.11.0.GA.jar
10. xwork-core-2.3.16.3

2. Other than jars, minimum files required are to start with struts2 project.
1. Web.xml
2. struts.xml

i) Configure web.xml for defining filter class of your struts2 web application. In struts2, all the requests made to the web application is routed through a Filter class to handle. If you are using latest version of struts2 i.e 2.3.16 or above, use StrutsPrepareAndExecuteFilter class. or use FilterDispatcher which is depreciated.

ii) Create mapping with struts.xml. Any struts2 web project will have model , action class (controller) and view page (MVC). Mapping between actions and views are configured in struts.xml file. Don’t forget to copy the struts.xml in /WEB-INF/Classes folder OR  you can put it  in src folder which will be placed in classes folder at run time.

You can see the directory and files  structure of the sample project below.sample project structure

Now let us create sample project in struts2. (In our example Struts2Sample that contains Index.jsp , GetEmployeeCode.jsp and Display.jsp which accepts code, name, designation, email and mobile.)

1. Copy all the above jars to lib folder under /Struts2Sample/WebContent/WEB-INF/ in your Struts2Sample project and refresh lib folder in eclipse. or the same can be done by Right clicking on the project -> properties -> java buid path -> Add External jars.

2. Copy the below web.xml file to the /Struts2Sample/WebContent/WEB-INF/web.xml

Web.xml

 

3. Create a model class to store Employee details

 

4. Create Action Class

Note : We have created two methods (execute & getEmployeeDetail) in one action. @SkipValidation indicates that the validate method is skipped for the method execute.

5. Create DAO class for retrieving data based on employee code and capture in Employee model. This is optional.

 

6. Create view pages (i.e jsp pages)

1. Index.jsp
2. GetEmployeeCode.jsp
3. Display.jsp

Each jsp will have the taglib directive at the top of the it <%@ taglib prefix=”s” uri=”/struts-tags” %>.
Index.jsp should look like:

 

code of GetEmployeeCode.jsp

 

code of Display.jsp

 

Finally struts.xml

Now sample strut2 project is completed. Yuo can run the project by invoking http://localhost:8080/Struts2Sample/Index.jsp

index page input pagevalidationdisplay

Leave a Reply