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

  • Get current year in java
    13 July

    Get current year in Java

    Table of ContentsUsing Date and SimpleDateFormatUsing CalendarUsing LocalDateUsing LocalDateTimeusing ZonedDateTimeUsing OffsetDateTimeUsing Instant and ZoneIdUsing Instant and ZoneOffsetConclusion In this post, we will see how to get current year in Java. Using Date and SimpleDateFormat The Date class was introduced in Java 1.0 and provided different approaches to work with time. The SimpleDateFormat inherits from DateFormat […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

  • 9Cartoon
    13 July

    13 Best Working 9Cartoon Alternatives in 2021 (Tried and Tested)

    Table of ContentsBest 9cartoon AlternativesCartoonsOnAnimeDaoAnimeToonKissAnimeToonGetWatch Cartoons OnlineCartoonCrazyToonovaKimCartoonWatch SeriesNyaaToonJetKissCartoonFrequently Asked QuestionsWhat is 9cartoon?Is 9cartoon free?What happened to 9cartoon?Which are the best 9cartoon alternatives?Wrapping Up Are you having trouble accessing 9cartoon? Well then, you’re just at the right place. 9cartoon is a well-known web-based platform that bagged a large quality of cartoons, relative TV shows and movies. […]

  • 13 July

    Einthusan Alternatives : 16 Best websites like Enthusan in 2021

    Table of ContentsEinthusan alternativesYouTubeYuppTVSonyLIVJio CinemaHotstarVootZee5HungamaAmazon Prime VideoAirtel XtremeMX PlayerEros NowNetflixHonorable Mentions123 MoviesF MoviesSoap2dayFrequently Asked Questions**Is Einthusan Blocked in India?Is Einthusan Safe?Is Einthusan Free?Einthusan Downloader: Is it legit?Final Words Einthusan is a well-known platform for watching movies online. From streaming in different languages to downloading them for online viewing, the platform offered everything that movie lovers […]

  • CQATest
    13 July

    Everything You Need to Know About CQATest in 2021

    Table of ContentsCQATest: A Brief OverviewHow Can CQATest Help?Problems Associated with CQA TestFixing Problems Associated with CQATestDisabling CQATest AppMethod 1: Force Stopping CQATest AppMethod 2: Resetting Your DevicePrecautions: Things to Keep in Mind Before Disabling CQATest.FAQsWhat is CQATest on Android?What does CQA stand for?Is CQATest app a virus?Is CQA Test Safe?What are the most common […]

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

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.