Struts 2 ajax example

This is 7 of 8 part of struts 2 tutorial.In this tutorial we will see how we can use ajax in our webApplication in struts 2 framework.

Tutorial Content:

Ajax Support in struts 2:

Struts 2 provides in-built support to ajax using dojo toolkit library. It comes with dojo ajax APIs which you can use to support ajax.For ajax support you need to add following jar to your classpath struts2-dojo-plugin.jar.
AutoCompleter Example:
Create project named “autoCompleteInStruts2”.For configuring struts 2 in your eclipse ide please refer configuring struts 2 link.
We will create textbox which will autocomplete and suggest input.
Project structure:

Action class :

Create action class named AutoCompleteAction.java under package org.arpit.javaPostsForLearning in src folder. It will get called for our AutoCompleter example.
copy following content to AutoCompleteAction.java

JSPs:

We will create one jsp named “autoComplete.jsp” under WebContent folder.
Copy following code into autoComplete.jsp
Now here <%@taglib uri="/struts-dojo-tags" prefix="sx"%> is a directive used in jsp for including dojo ajax tag files.
tag is used for having textbox or dropdown in jsp. So on writing few characters it will show you relevant options. In this,showDownArrow property indicates whether to show dropdown or not. Here we are setting it to false so that we will get textbox same as we see on google.

struts.xml:

There will be no major changes in struts.xml .It will be same type of url action mapping
Create struts.xml in src folder.
Copy following content in struts.xml

 Web.xml:

It will be also same as in previous posts.
Copy following content in web.xml.

Run Application:

Right click on project->run as->run on server.

copy http://localhost:8080/autoCompleteInStruts2/AutoComplete link to  browser and press enter
we will get following screen.

Enter ‘m’ in textbox and we will get following screen.

Extending above example :

We will extend above example such that when we click submit on autocomplete.jsp,we will be directed to welcome page for submitted state.

Adding another action file:

Create new action file named “welcomeTostate.java”
copy following code in WelcomeToState.java
 so in autoComplete.jsp add action=”welcomeTostate” in tag.

Adding new JSP file:

create new jsp file in Webcontent folder named “WelcomeToState.jsp”
copy following code to WelcomeToState.jsp.
 Now in struts2.xml in tag add following action mapping

Run application again:

Follow above steps(under run application heading) for running application.
Choose Maharastra and click on submit

Source code:

click to begin
6MB .zip

Related Posts

  • nvarchar vs varchar
    29 July

    nvarchar vs varchar in SQL Server

    Table of ContentsIntroductionVARCHARNVARCHARExamples to Demonstrate Difference between VARCHAR and NVARCHARVARCHAR vs NVARCHAR Comparison Chart This article will look at the difference between the data types NVARCHAR and VARCHAR in SQL Server. These data types are commonly used for storing textual data or strings for various design purposes. We will look at the description of each […]

  • Stream2watch
    29 July

    Stream2watch alternatives -13 Best Websites Like Streamtowatch in 2021

    Table of ContentsBest Working Proxies of the Stream2watch in 2021Stream2Watch alternativesSport365SportP2PCricFree12th PlayerVIP BoxLiveTVWiziwigStopStreamSport LemonMamaHDLAOLA1Sport RARTime4TVfuboTVFirstRowSportsVIP LeagueFrequently Asked Questions**Is Stream2watch a safe website?What are the best Streamtowatch Alternatives?Do you need an account for Stream2watch?Final Words Are you after the best Stream2watch alternatives? Good News! this article is all about efficient web-based resources for streaming football, baseball, […]

  • 29 July

    AirPod Case not Charging – Common Issues and Fixes

    Table of ContentsBefore you StartWhy Is My AirPod Case Not Charging?Five Easy Solutions for AirPods Case Not Charging1. Reset Your AirPods Case2. Clean the Charging Port 3. Get a new Charger and Cable4. Update the AirPods Firmware5. Replace the CaseTips for Avoid AirPod not Charging IssueFrequently Asked QuestionsWhy isn’t my AirPod case charging, but my AirPods […]

  • 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 […]

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.