How to get data from struts2 action with jQuery?

Posted by Rafa Romero on Stack Overflow See other posts from Stack Overflow or by Rafa Romero
Published on 2011-02-02T12:13:02Z Indexed on 2011/02/02 15:25 UTC
Read the original article Hit count: 338

Filed under:
|
|
|
|

I have an Struts2 Web with GoogleMaps. I want to load a list of markers which are saved in a SQL DDBB. To do this, I tried with jQuery and Ajax. Here you are the code:

loadMarkers.java

  public class loadMarkers extends ActionSupport implements ServletRequestAware,ServletResponseAware{

//Variables de sesion/cookie
FunctionClass ses;
protected HttpServletResponse servletResponse;
protected HttpServletRequest servletRequest;
private String userID="";

//Variables del marker
private List<marker> markersList = new ArrayList<marker>();

public String execute() throws Exception{
    FunctionClass friends = new FunctionClass();

    //Leemos de la cookie
    for(Cookie c : servletRequest.getCookies()) {
        if (c.getName().equals("userID"))
            userID = (c.getValue());
    } 
    System.out.println("en el loadMarkers");
    connectionBBDD con = new connectionBBDD();
    markersList = con.loadMarkers(userID);
    return SUCCESS;
}

I want to use the markerList array in Javascript, to create the markers.

This is the struts.xml file:

<package name="jsonActions" namespace="/test" extends="json-default">
    <action name="LoadMarkers" class="web.localizadroid.maps.loadMarkers">
    <interceptor-ref name="basicStack"/>
        <result type="json">
        <param name="root">markersList</param>
        </result>
    </action>
</package>

And here you are the Javascript (jQuery) code:

function loadMarkersJ(){
alert("dentro");
$.ajax({
    type : "post",
    url : "LoadMarkers",
    dataType: "json",       
    success : function(data) {
        alert(data);
        var image = new google.maps.MarkerImage ('http://i53.tinypic.com/ettquh.png');
        var jSon_Object = eval("(" + data + ")");
        //For para analizar los datos (Json) obtenidos de la BBDD
        for (x = 0; x < jSon_Object.length; x++) {

            var markersArray = [];

            var myLatlng = new google.maps.LatLng(jSon_Object[x].lat, jSon_Object[x].lon);

            markerLoaded = new google.maps.Marker( {
                position : myLatlng,
                map : map,
                icon: image,
                title: "NOMBRE: " + jSon_Object[x].tarjetName + "\n" + "ANOTACIONES: " + jSon_Object[x].anotaciones + "\n" + "TIME: " + jSon_Object[x].time
            });
            markersArray.push(markerLoaded);

            if (markersArray) {
                for (i in markersArray) {
                    alert("entro en forColocaMarkers");     
                    if (markersArray[i].getAnimation() != null) {
                        markersArray[i].setAnimation(null);
                    } else {
                            markersArray[i].setAnimation(google.maps.Animation.BOUNCE);
                    }

                    markersArray[i].setMap(map);        
                }
            }
        }
    }
});

}

From success : function(data) { to the end, is JavaScript code to create de markers, and this it's OK. The problem is whith ajax, because I don't get to obtain markerList Array by jSon data return...I think that the problem is in the url attribute from $.ajax...I tried loadMarkers.action and loadMarkers, but nothing happens. When I execute the web, only prints the alert alert("dentro"), the alert alert(data) never has been printed.

I have forgotten to add the code where I call the Javascript function (loadMarkersJ). Here you are:

<p><s:a  action="LoadMarkers.action" namespace="/test" onclick="loadMarkersJ(this)">Cargar Marcadores S</s:a></p>

Somebody can help me please?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX