Posted By:
Nina_Anderson
Posted On:
Monday, August 13, 2007 05:32 AM
Hi, I have a struts 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've never used Ajax before. My jsp page is able to send the request to my Struts Action process. However, the data that gets forwarded back to the JSP page is NOT 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. Here's my code: /// JSP //// if (req.status == 200) { // OK response alert(req.respon
More>>
Hi,
I have a struts 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've never used Ajax before.
My jsp page is able to send the request to my Struts Action process. However, the data that gets forwarded back to the JSP page is NOT 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.
Here's my code:
/// JSP ////
if (req.status == 200) { // OK response
alert(req.responseText);
textToSplit = document.createTextNode(req.responseText);
if(textToSplit == '803'){
alert("No select option available on the server")
}
//Split the document
returnElements=textToSplit.split("||")
//Process each of the elements
for ( var i=0; i
valueLabelPair = returnElements[i].split(";")
alert("label - " + valueLabelPair[0]);
document.forms['SearchResultForm'].selectedState.options.length = returnElements.length;
document.forms['SearchResultForm'].selectedState.options[i] = new Option(valueLabelPair[0], valueLabelPair[1]);
}
}
}
}
]]>
///// STRUTS Action ////
public class MainSearchDisplayAction extends Action {
/*
* Generated Methods
*/
ServletContext context = null;
public void setServlet(ActionServlet servlet)
{
context = servlet.getServletContext();
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping_, ActionForm form_,
HttpServletRequest request_, HttpServletResponse response_) {
DynaValidatorForm form = (DynaValidatorForm) form_;
if( request_.getParameter("selectedCountry") != null && request_.getParameter("selectedCountry").length() > 0)
{
PopulateAjaxLabelValue populateAjaxValues = new PopulateAjaxLabelValue();
SearchBS searchBS = new SearchBS();
String selectedCountry = null;
try {
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);
} catch (Exception e) {
System.out.println("Exception occurred - " + e);
}
}
return mapping_.findForward("mainSearch");
}
///// STRUTS CONFIG /////
Thanks in Advance