This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I have a JSP page that has a Country drop-down list and a State drop-down list. I want to be able to retrieve a list of States when a Country is selected. I used Struts and JSP in our web applications. I was told that Ajax will be able to handle this more elegantly. I've never I'm new to Ajax.
So, I'm wondering how I can incorporate Struts and Ajax in my web application.
Here's my JSP code:
<html> <script language='javascript'>
function setState(selectedState) { document.forms['SearchResultForm'].submit(); }
function setCountry(selectedCountry) { document.forms['SearchResultForm'].submit(); }
This is generally referred to as a "dependent select" or "dependent dropdown". If you google "ajax dependent select" you'll see a lot of different methods for doing this. You might find the link below helpful, as it is specific to Struts:
I followed the struts-AJax's examples, and my jsp page is able to send the request to my Struts Action process. However, the data the gets forwarded back to the JSP page is making it into the responseText. I'm writing the data into the PrintWriter object but that data is not making it back.
Can someone Please let me know what I'm doing wrong. This is my first time using AJAX and I'm pulled all my hair trying to debug this
Here's my code:
/// JSP ////
<script language='javascript'>
var req; /* * Get the second options by calling a Struts action */ function getStates(selectedCountry, countrySelectBox) { document.forms['SearchResultForm'].submit(); url="http://localhost:9080/PBWebApp/displayHomePage.do?selectedCountry="+selectedCountry;
//Do the Ajax call if (window.XMLHttpRequest){ // Non-IE browsers req = new XMLHttpRequest(); //A call-back function is define so the browser knows which function to call after the server gives a reponse back req.onreadystatechange = populateSecondBox;
req.open("GET", url, true); //was get req.send(null); }
//Do the Ajax call if (window.XMLHttpRequest){ // Non-IE browsers req = new XMLHttpRequest(); //A call-back function is define so the browser knows which function to call after the server gives a reponse back req.onreadystatechange = populateSecondBox;
req.open("GET", url, true); //was get req.send(null); }
else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = populateSecondBox; req.open("GET", url, true); req.send(null); } } }
//Callback function function populateSecondBox(){ document.forms['SearchResultForm'].selectedState.options.length = 0;
response_.setContentType("text/xml"); PrintWriter out = response_.getWriter();
List states = searchBS.getStatesByCountry( new Integer(selectedCountry) ); //Make a String representation in format label;value||label;value String outLine = populateAjaxValues.makeOutputString(states); System.out.println("Response String - " + outLine); out.print(outLine);