Struts 2 Example

In this tutorial you will learn about the Struts 2 Example and its application with practical example.

Struts2 Example

In previous articles, We have learnt about struts2. In the struts2 architecture we have learnt that if we click on hyperlink or submit form action in Struts2 Web Application so the input is collected by the Controller and then Controller sends it to Java class which is called Actions. After the execution of Action the resources are selected to render the response. JSP, PDF , an Excel spreadsheet and a Java applet window are the resources which has been selected by the action. JSP is generally used as resources. Detailed about Action will be in Next Article.
Let’s understand struts2 by an example. Please follow the steps. This project is created in Netbeans IDE.

Table Of Contents

Step 1:-

Create new project on click on File and then New Project and then select JavaWeb->Web Application like below image.


Step 2:- In this step enter your project name and then click on Next. Check below image.


Step 3:- This step is used for selecting “Server and Settings”. I am using “GlassFish Server”, you can also use “apache tomcat server”. Check below image.


Step 4:- This is the step where we are going to select our framework , I hope you are gone through with previous article in which we have explained that how can we add struts2 in Netbeans, if not please follow instruction of previous article first. Check below image for this step.


On finishing you will be done with project creation. Now let’s do some coding part.

Requirement: Below are listed files which are required for this example.

2. index.jsp.
3. HelloWorld.jsp
4. struts.xml
5. web.xml

This is our Action class which respond to a user action, as we know that Action class is the key to Struts2 application and most of the logics are implemented in this class. So Let’s create this class. Below is our action class which is very simple, here we have used only getter and setter method for the “name” property, there is also an execute method which return string message. Check below class.


This is our welcome page from where we will going to perform action. Here on click of submit button action will perform which call specific method of the “HelloWorldAction” class. Check below code.


This page will present final message , and will be called by Struts2 Framework when predefined action will happen. In “struts.xml” file we have done with mapping of this class which will be showing in next step. For this page check below code.


This is our configuration file which is created automatically when you create a project. Here we have done with mapping of our action. Check below code for this. Here we can see that when action will be “welcome” and result will be “success” from “HelloWorldAction” class So “HelloWorld.jsp” will be called.


This is our another configuration file in which we will update our welcome file. Like below code.

below is the structure of this project.


Output :-

We are done with our coding part now simply run your project. You will get output like below Images.


Enter the Name and click on Submit button, after submitting the name above image will be next output.


In this tutorial we have learn about the Struts 2 Example and its application with practical example. I hope you will like this tutorial.