2014-12-03 41 views
0

我必須使用來自fatbase的經度和緯度值顯示GoogleMap,但不能使用c:foreach標記,但我不知道c:對於JSP頁面和managedbean的foreach tag.Code給出如下:c:foreach標記不適用於JavaScript中的Google地圖jsp

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

回答

1

我想這就是你需要在你C到包括:forEach標記。

<c:out value="${googleLocation.latitude}', '${googleLocation.longitude}"></c:out>