我必须使用fatbase的纬度和经度值显示GoogleMap,但使用c:foreach标记无法正常工作,但我不知道c:foreach标记有什么问题.jsp页面和托管bean的代码如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
</head>
<body>
<!--<div id="load_me">Current Time :<%= new java.util.Date() %></div>-->
<input type="button" onclick="mapRefresh();" value="Refresh" class="button" />
<div><br/></div>
<div id="map" style="width: 1300px; height: 600px;"></div>
<!-- <script type="text/javascript">
//alert("my alert");
var auto_refresh = setInterval(
function()
{
// alert("alert");
$('#load_me').load('mapview.jsp').fadeIn("slow");
}, 5000); // autorefresh the content of the div after
//every 10000 milliseconds(10sec)
</script>-->
<script type="text/javascript">
//alert("hello");
var locations = [
<c:forEach items="${mapBean.googleMapLocations}" var="googleLocation" varStatus ="status">
['${googleLocation.latitude}', '${googleLocation.longitude}']
<c:if test="${!status.last}">
,
</c:if>
</c:forEach>
];
alert(locations);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng("${mapBean.centerLatitude}", "${mapBean.centerLongitude}"),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
<table>
<c:forEach var="googleLocation" items="${mapBean.googleMapLocations}">
<tr><td>${googleLocation.latitude}</td><td>${googleLocation.longitude}</td></tr>
</c:forEach>
CENTER<tr><td>${mapBean.centerLatitude}</td><td>${mapBean.centerLongitude}</td></tr>
</table>
</body>
</html>
Managedbean Code:
public class MapBean implements java.io.Serializable {
private double centerLatitude;
private double centerLongitude;
public java.util.List<Components> getGoogleMapLocations() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session sess = sf.openSession();
Query q = sess.createQuery("from Components");
java.util.List l = q.list();
sess.close();
return l;
}
}
最佳答案
我想这就是您需要在c:foreach标记中包含的内容。
<c:out value="${googleLocation.latitude}', '${googleLocation.longitude}"></c:out>