This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes dependent select boxes with ajax . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "dependent select boxes with ajax ." Watch "dependent select boxes with ajax ." New topic
Author

dependent select boxes with ajax .

sdeep anand
Greenhorn

Joined: Jul 16, 2006
Posts: 7
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
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
sdeep anand
Greenhorn

Joined: Jul 16, 2006
Posts: 7
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Jul 16, 2006
Posts: 7
Hi Merrill,

Thanks for your help. I struggled with this problem for whole day. It solved my problem. Your explaination was very helpful.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dependent select boxes with ajax .
 
Similar Threads
Ajax, Oracle DB and JSP...
Reg : Drop-down box
Dynamic Drop Down Lists in Struts
Dependent drop down boxes with javascript
Drop Down validation