-1

I'm very new to Ajax. Currently I'm working on a new project. For that project one of the requirements is, populating a second dropdown based on the input from the first dropdown. I'm using Struts to do that. I don't want the page to be refreshed, so I need to use Ajax for calling the second dropdown content in the backend and populate in the second dropdown. I don't know how to write code for that.

What should be included (jars,tags) in my struts project? What entries should come in my JSP (I am using <html:select>)? What will come in JavaScript? What will come in action class (in action class I can fetch the list values from the DB based on the selection from the first dropdown)?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • What did you try? Did you ever start to write a code? – Roman C Feb 23 '15 at 07:59
  • I tried initially with sumitting form. but its resetting all the previous values. i have around 45 fields. Its very difficult to keep all values in request. So I'm looking for Ajax. I have seen couple of examples. but I wasn't able to corelate my code with those examples. – srikanthpirate Feb 23 '15 at 20:31

2 Answers2

1

Instead of ajax, I used struts only to acheive this. I have assigned all variables to form to keep values when submitting form.

0

The answer to this question would probably a thesis ;) I will just guide you in here.

1]Use a javascript framework like jquery. It will help you make ajax calls to your controller URL Check an easy tutorial on http://www.tutorialspoint.com/jquery/jquery-ajax.htm

2] For the controllers lets have two urls mapped:

  • urapp/poplateDropdown1 to controller which return values/list target at ur first drop down
  • urapp/poplateDropdown2 to a another controller(or method within same controller) which responds to a GET request and receives a parameter say name SEL_VALUE for the selected value.

3] onchange events. Have an onchange or similar best suited even on your first dropdoww. In your event call a js function; like <select onchange=callDropDown2Controller(this.value) > in your callDropDown2Controller() method:

//pseudo implementation
callDropDown2Controller(var selectedValue){

// now generate an AJAX get request using jquery with the following url 
urlToCall =  '/urapp/poplateDropdown2?SEL_VALUE=' + selectedValue

}

The rest buddy u need to do some homework and research.

Milind J
  • 517
  • 5
  • 10