struts 2 hello world example

This is 3 of 8 part of struts 2 tutorial.It provides struts 2 hello world example.

Tutorial Content:

lets start with first struts2 application.

We will implement modules described in above diagram in our project.
Create dynamic web project named “Struts2FirstProject”.For configuring struts 2 in your eclipse ide please refer configuring struts 2 link.

The web.xml file:

The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.
As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder WebContent/WEB-INF.

copy following content in web.xml

Action Class:

We will have action class which is simple POJO class having attributes and method.
By default execute() method is called when client request goes to action class.

Create TutorialAction.java under src.Here we have TutorialAction.java as action class in our project.

copy following content into TutorialAction.java

JSP:

Create two jsp files named “TutorialView.jsp” and “Error.jsp” under WebContent.

copy following content into TutorialView.jsp

copy following content into Error.jsp

The struts.xml file:

strut2.xml provides mapping between url to action mapping.
So if client enters “http://mywebapp/getTutorial” then Tutorial action will be called.
Create strut2.xml under src folder.
copy following content into struts.xml


This diagram describes the request workflow :
  1. Request(http://localhost:8080/Strut2FirstProject/getTutorial) is generated by client and sent to Servlet container.
  2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.In this project,getTutorial action goes to TutorialAction class.
  3. In tutorialAction,execute() method is executed and returns “success”
  4. As per mapping in struts2.xml, name is matched with returned string “success”.
  5. Accordingly,TutorialView.jsp is rendered and returned to user.

Now finally we will run our project.

right click on project->run as->run on server

so when you paste resultant url to your browser,you will get some thing like this.

  We are getting this error because we have declared welcome file as “default.jsp” which do not exist.
  so if you paste “http://localhost:8080/Strut2FirstProject/getTutorial” you will get your output as
as we included action(getTutorial) in url,It rendered “tutorialView.jsp” as output.

Source code:

click to begin

6MB .zip

Now in next post,we will write another example of login page with validation

Related Posts

  • Convert datetime to date in Python
    20 June

    Convert datetime to date in Python

    Table of ContentsUsing the date() function to convert datetime to date in Python.Using the dt.date function in pandas to convert datetime to date in Python. Date and time in Python are not a pre-defined data type itself. The datetime module can be imported to the Python code and it provides classes used that manipulate and […]

  • Convert String to Boolean in Python
    20 June

    Convert String to Boolean in Python

    Table of ContentsUsing the bool() method to convert string to Boolean in Python.Using the distutils.util.strtobool() method to convert a string to Boolean in Python.Using list comprehension to convert a string to Boolean in Python.Using the map() function along with Lambda function to convert a string to Boolean in Python.Using the JSON parser to convert a […]

  • Calculator program in Python
    19 June

    Calculator program in Python

    Table of ContentsUsing the while loop along with the if...else conditional statement.Define functions for Addition, Subtraction, Multiplication and DivisionTake user input using input functionComplete calculator program in Python A simple Calculator can be utilized to carry out the four basic arithmetic operations namely addition, division, multiplication, and subtraction depending on the input of the user. […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Maximum number of vowels in a Substring of given length
    18 June

    Maximum Number of Vowels in a Substring of Given Length

    Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.