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

  • Mobdro alternatives
    29 July

    Mobdro Alternatives – 14 Best apps like Mobdro in 2021

    Table of ContentsBest Mobdro Alternatives Working in 2021Cyberflix TVMorph TVTerrarium TVTea TVVisit Thop TVTubi Tv123MoviesMovie BoxVuduOreo TVMegaBox HDFreeFlixYuppTVFrequently Asked QuestionsWhat is Mobdro?Why do you need Mobdro alternatives?Are there free Mobdro alternatives?Wrapping Up Can’t access Mobdro anymore and are worried about watching your favorite movies? Take a breath; you still have a long list of working […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in Pandas A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from external files and […]

  • 27 July

    OBJ Emoji Error – Everything You Need to Know

    Table of ContentsWhat Does OBJ Mean?What is OBJ Emoji?What does OBJ mean on Facebook?How Can I Fix OBJ Emoji Error?Fix obj emoji in Android and iOSFix obj emoji in Windows and MacsOBJ Emoji FAQsWhat does OBJ in a dotted box mean, Tiktok?What does the OBJ file contain?Why do I see a box while texting?Why is […]

  • 27 July

    How to fix the Google Play Services Keeps Stopping Error on Android

    Table of ContentsHow to Fix the Google Play Services Keeps Stopping Error on AndroidSolution No. 1: Getting Your Android Device RebootedSolution No. 2: Updating the Google Play services AppSolution No. 3: Get the Cache of Google Play Services app ClearedSolution No. 4: Disabling the Google Play services on a wholeSolution No. 5: Getting the Google […]

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.