如何从Struts2的行动数据与jQuery? [英] How to get data from struts2 action with jQuery?

查看:75
本文介绍了如何从Struts2的行动数据与jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Struts2的Web上的GoogleMaps。我想加载它们保存在SQL DDBB标记列表。要做到这一点,我试图用jQuery和Ajax。在这里,您是code:

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

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;
}

我想使用markerList数组在Javascript中,创造了标记。

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

这是在struts.xml文件:

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>

在这里,你是使用Javascript(jQuery的)code:

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);        
                }
            }
        }
    }
});

}

成功:功能(数据){到最后,是JavaScript的code创建德标记,这没关系。问题是蒙山阿贾克斯,因为我没有得到获得markerList阵列通过JSON数据返回......我觉得这个问题是从$就url属性...我想loadMarkers.action和loadMarkers,但没有发生。当我执行网页,只打印警报警报(迪登特鲁),警报警报(数据)从不已被打印

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.

我忘了加code,我调用JavaScript函数(loadMarkersJ)。在这里,您是:

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?

推荐答案

你有没有考虑使用的 Struts2的-jQuery插件它真的简化Ajax开发Struts2的,用它由我自己

Have you consider using the Struts2-jQuery plugin It REALLY simplifies ajax development with Struts2, used it by myself

这篇关于如何从Struts2的行动数据与jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆