• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dependent select boxes with ajax .

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i have 2 drop down boxes, the second drop down dependent on first.
Whenever the user selects an option from the first drop down I have to make an ajax call to the struts action class to get the list of options for the second drop down. I am using jstl tags in the jsp.


Can you point me to similar examples done with ajax?

Thanks
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've done this in my applications. Here is the process:

1-Populate the first select using server-side code (JSTL, Struts tags, etc.).

2-In your server-side code, create the second select without options or with only a "please select" null option.

3-Use the onchange event of the first select to initiate an AJAX call.

4-Use the data returned from the AJAX call to clear the second select of any existing options and add the new ones based on the data returned.

I would highly recommend that you look into the DWR framework. It handles all the low-level mechanics of making an AJAX call. All you have to do is write a method in a Java class that will find the correct options and register the method with DWR. DWR then provides you with a JavaScript function that will handle making the AJAX call to the server and calling the Java method you've written.
 
sdeep anand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried passing the request through ajax to action class.

I have this javascript method

function getGrouping() {
alert("inside grouping");
var parameters= new Array();
var i=0;
parameters[i++]='methodName';
parameters[i++]='getValuesA';
parameters[i++]='primaryValue';
parameters[i++]=document.getElementById('primary').value;


var response = XMLHttpRequestSender('somePage.do',
parameters,
'true',
'GET',
'1000000',
'3','secondary', displayResult);



}

function displayResult(req, idToChange)
{
alert('Inside displayResulte into '+idToChange);
//I am able to come into this method.
var result = req.responseText;
alert("inside result");
alert(result);

//here I have code to populate the second drop down
}
public ActionForward getValuesA(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
String primaryValue = request.getParameter("primaryValue");
List secondaryvalues= new ArrayList();
secondaryValues.add("abc");
secondaryValues.add("cde");
//I construct a string "optionsString" with the values and set it in request.
request.setAttribute("result", optionsString);


return null;// as I am returning to the same jsp i am returning null.is this right.

}

So should I use another jsp and use request.getAttribute("result") to get the options from action class or how do I get the options set in the action class into this jsp.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you're using AJAX, you have to remember that everything is done in JavaScript and HTML except for the retrieval of the data. You're not refreshing the page, so there is no new JSP. Only the current HTML document.

Let me tell you up front that I don't use an Action class to retrieve my data. I use a regular POJO with some JDBC code to retrieve the data. I then register my class and method with DWR and have DWR make the AJAX call. I can give you a limited amount of help with the method you're using, but it's not the one I use or recommend.

Having said that, here are my comments on your code:

An Action class responding to an AJAX call must output an HTML or XML response. It doesn't put anything in the HttpServletRequest object. It simply sends back its data through the HttpServletResponse object. Therefore, remove the line:

request.setAttribute("result", optionsString);

and add these lines:

PrintWriter out = response.getWriter();
out.print(optionsString);
out.close();
return null;

Then in your displayResult function, you should be able to view the string that you sent back. I suspect you'll want to use the innerHTML property of some JavaScript object to change your options. I know others have done it this way, but I've always used DWR, so I couldn't advise you on how to do it.

Good luck.
[ August 08, 2006: Message edited by: Merrill Higginson ]
 
sdeep anand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Thanks for your help. I struggled with this problem for whole day. It solved my problem. Your explaination was very helpful.
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic