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<anji...@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-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org




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






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

Reply via email to