struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anjib Mulepati <anji...@hotmail.com>
Subject Re: Returning response object only from Action
Date Wed, 28 Sep 2011 16:08:47 GMT
This is great I am able to get the String of all shows. But my ultimate 
goal was to get the result and populate in the select box.
I am using AJAX since second box (charater) content depends upon first 
select box (tvshow). SO I need to parse String or is there any easy way 
to get values so that I can use that to populate the select  box.
Also when I was looking at the script on the JSP I was wondering how can 
I use that string to populate the select box.
<script>

     var req;
     var which;

     function retrieveURL(url) {
         if (window.XMLHttpRequest) { // Non-IE browsers
             req = new XMLHttpRequest();
             req.onreadystatechange = processStateChange;
             try {
                 req.open("GET", url, true);
             } catch (e) {
                 alert(e);
             }
             req.send(null);
         } else if (window.ActiveXObject) { // IE
             req = new ActiveXObject("Microsoft.XMLHTTP");
             if (req) {
                 req.onreadystatechange = processStateChange;
                 req.open("GET", url, true);
                 req.send();
             }
         }
     }

     function processStateChange() {
         if (req.readyState == 4) { // Complete
             if (req.status == 200) { // OK response
                 var data = req.responseText;
                 alert(data);
                 document.getElementById("characters").innerHTML = data;
             } else {
                 alert("Problem: " + req.statusText);
             }
         }
     }

</script>

<html:form action="doSome">
<html:select property="show" onchange="retrieveURL('GetChars.do?tvID=' + 
this.value);">
<html:options collection="showList" property="showId" 
labelProperty="showName" />
</html:select>
<br/>
             Characters: <span id="characters"></span>
<html:submit />
</html:form>


On 9/28/2011 10:41 AM, Christian Grobmeier wrote:
> Hi Ajib,
>
>> public ActionForward execute(ActionMapping mapping, ActionForm inForm,
>> HttpServletRequest request, HttpServletResponse response) throws Exception {
>>
>>         // Get a list of characters associated with the select TV show
>>         String tvShow = (String) request.getParameter("tvShow");
>>         if (tvShow == null) {
>>             tvShow = "";
>>         }
>>
> // use a json lib, like: http://code.google.com/p/jjson/
> // to generate json from an arrayList
>
> List<String>  character = getCharacters(tvShow);
>      		JSONArray arr = new JSONArray();
>      		for (String string : character) {
>      			arr.add(new JSONString(string));
> 			}
>      		
>      		String responseString = arr.toJSON();
>
> // use text/plain:
>>         response.setContentType("text/plain");
>>         PrintWriter out = response.getWriter();
> // use response String
>>         out.println(responseString);
>>         out.flush();
>>
>>         return null; // Not forwarding to anywhere, response is fully-cooked
>>
>>     } // End execute()
>>
>> How can I avoid creating HTML in action class and send the Arraylist /JSON
>> to view?
> Similar to that. :-)
>
> Cheers
> Christian
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message