如何使用 jQuery 从 struts2 操作获取数据?

发布于 2024-10-15 05:02:31 字数 3395 浏览 2 评论 0原文

我有一个带有 GoogleMaps 的 Struts2 Web。我想加载保存在 SQL DDBB 中的标记列表。为此,我尝试使用 jQuery 和 Ajax。这里是代码:

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

我想在Javascript中使用markerList数组来创建标记。

这是 struts.xml 文件:

<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) 代码:

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

}

success : function(data) { 到最后,是创建 de 标记的 JavaScript 代码,这就是好的。问题出在ajax上,因为我无法通过jSon数据返回来获取markerList数组...我认为问题出在$.ajax的url属性中...我尝试了loadMarkers.action和loadMarkers,但没有任何结果发生。当我执行网络时,仅打印警报 alert("dentro"),警报 alert(data) 从未被打印过。

我忘记添加调用 Javascript 函数 (loadMarkersJ) 的代码。在这里:

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

有人可以帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白衬杉格子梦 2024-10-22 05:02:31

您是否考虑过使用 Struts2-jQuery 插件 它确实简化了 Struts2 的 ajax 开发,使用就我自己而言

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

对风讲故事 2024-10-22 05:02:31

为什么我们不能只在 JSP 中提供内容类型信息。这对我有用。

我们不需要下载任何插件来实现对 struts actions 的 JSON 支持。

我们可以下载任何 json 转换器,例如 GSON
使用以下命令将对象转换为 json 格式。

Gson gson = new Gson();
json  = gson.toJson(events);
return SUCCESS;

在您成功的 JSP 中

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="application/json" %>
<s:property value="json" escape="false"/>

Why can't we just provide content-type information in JSP. It works for me.

We don't need to download any plugin to implement JSON support for struts actions.

We can just download any json convertor like GSON.
Use following to convert your object into json format.

Gson gson = new Gson();
json  = gson.toJson(events);
return SUCCESS;

In your success JSP

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="application/json" %>
<s:property value="json" escape="false"/>
弄潮 2024-10-22 05:02:31

在 javascript 函数调用后添加 return false

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

看看这个:

https://stackoverflow.com/a/20217387/1360694

Add return false after the javascript function call.

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

Check this out:

https://stackoverflow.com/a/20217387/1360694

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文