You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Anjib Mulepati <an...@hotmail.com> on 2011/09/28 16:34:42 UTC

Returning response object only from Action

Hi All,

I want to know how I can send the repose object with JSON content to the 
view from the Action class.

I am trying to add AJAX feature in Struts 1.3.8 application as shown in 
http://www.zammetti.com/articles/xhrstruts/index.htm.

Instead of doing

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 = "";
         }
         ArrayList characters = getCharacters(tvShow);

         // creating HTML in an Action
         String html = "<select name=\"CharactersSelect\">";
         int i = 0;
         for (Iterator it = characters.iterator(); it.hasNext();) {
             String name = (String) it.next();
             i++;
             html += "<option value=\"" + i + "\">" + name + "</option>";
         }
         html += "</select>";

         // Write the HTML to response
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         out.println(html);
         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?

Thanks,
Anjib



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


Re: Returning response object only from Action

Posted by Christian Grobmeier <gr...@gmail.com>.
On Wed, Sep 28, 2011 at 6:27 PM, Anjib Mulepati <an...@hotmail.com> wrote:
> Thanks for your help.
> I get the part to convert to/from the JSON object . But my problem was to
> make such result fit within Struts. i.e. how can i populate the "data" into
> select box using struts tag?

Oh ok... now this is a little time travel for me. Have not used Struts
1 in the past 5 years (thanks heaven).

Do you know this tag?
http://struts.apache.org/1.x/struts-taglib/tagreference.html#html:options

You'll need that and make a bean accessible for this tag.

here is a short example which might help you:
http://www.vaannila.com/struts/struts-example/struts-html-select-tag-example-1.html

Cheers
Christian

>
>
> On 9/28/2011 12:14 PM, Christian Grobmeier wrote:
>>
>> Ajib,
>>
>> I am using jQuery, which makes an json object out of the json string
>> you would get. This is very straightforward. Since you are using plain
>> javascript this is not possible so easily.
>>
>> Basically you get a string and need to turn it into a jsonobject.
>> You could do it like that:
>> http://www.json.org/js.html
>>
>> You probably need to make something like that:
>> var myObject = JSON.parse(data);
>>
>> and then you can iterate over myObject like an array and create the
>> html you need.
>>
>> I am afraid, you'll need to dig that out on your own now, but I guess
>> you have a good direction ;-)
>>
>> Cheers
>> Christian
>>
>>
>> On Wed, Sep 28, 2011 at 6:08 PM, Anjib Mulepati<an...@hotmail.com>
>>  wrote:
>>>
>>> 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
>>>
>>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
http://www.grobmeier.de

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


Re: Returning response object only from Action

Posted by Anjib Mulepati <an...@hotmail.com>.
Thanks for your help.
I get the part to convert to/from the JSON object . But my problem was 
to make such result fit within Struts. i.e. how can i populate the 
"data" into select box using struts tag?


On 9/28/2011 12:14 PM, Christian Grobmeier wrote:
> Ajib,
>
> I am using jQuery, which makes an json object out of the json string
> you would get. This is very straightforward. Since you are using plain
> javascript this is not possible so easily.
>
> Basically you get a string and need to turn it into a jsonobject.
> You could do it like that:
> http://www.json.org/js.html
>
> You probably need to make something like that:
> var myObject = JSON.parse(data);
>
> and then you can iterate over myObject like an array and create the
> html you need.
>
> I am afraid, you'll need to dig that out on your own now, but I guess
> you have a good direction ;-)
>
> Cheers
> Christian
>
>
> On Wed, Sep 28, 2011 at 6:08 PM, Anjib Mulepati<an...@hotmail.com>  wrote:
>> 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
>>
>>
>
>


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


Re: Returning response object only from Action

Posted by Christian Grobmeier <gr...@gmail.com>.
Ajib,

I am using jQuery, which makes an json object out of the json string
you would get. This is very straightforward. Since you are using plain
javascript this is not possible so easily.

Basically you get a string and need to turn it into a jsonobject.
You could do it like that:
http://www.json.org/js.html

You probably need to make something like that:
var myObject = JSON.parse(data);

and then you can iterate over myObject like an array and create the
html you need.

I am afraid, you'll need to dig that out on your own now, but I guess
you have a good direction ;-)

Cheers
Christian


On Wed, Sep 28, 2011 at 6:08 PM, Anjib Mulepati <an...@hotmail.com> wrote:
> 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
>
>



-- 
http://www.grobmeier.de

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


Re: Returning response object only from Action

Posted by Anjib Mulepati <an...@hotmail.com>.
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


Re: Returning response object only from Action

Posted by Christian Grobmeier <gr...@gmail.com>.
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