如何使用 jQuery 从 struts2 操作获取数据?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用 Struts2-jQuery 插件 它确实简化了 Struts2 的 ajax 开发,使用就我自己而言
Have you consider using the Struts2-jQuery plugin It REALLY simplifies ajax development with Struts2, used it by myself
为什么我们不能只在 JSP 中提供内容类型信息。这对我有用。
我们不需要下载任何插件来实现对 struts actions 的 JSON 支持。
我们可以下载任何 json 转换器,例如 GSON。
使用以下命令将对象转换为 json 格式。
在您成功的 JSP 中
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.
In your success JSP
在 javascript 函数调用后添加
return false
。看看这个:
https://stackoverflow.com/a/20217387/1360694
Add
return false
after the javascript function call.Check this out:
https://stackoverflow.com/a/20217387/1360694