簡體   English   中英

如何在JavaScript中傳遞/使用jstl arrayList?

[英]How to pass/use jstl arrayList in javascript?

我有一個帶有表單的網頁,一旦輸入了緯度和經度(以及其他數據)並提交了這些數據,這些數據將保存在mongo數據庫中,同時在Google地圖上放置標記。 因此,當我提交新的經/緯度數據時,我在地圖上添加了一個新標記,依此類推。 現在的問題是,當我切換到網站的另一頁並返回上一頁(帶有地圖)時,我仍然具有該地圖,但是沒有標記。 因此,每次我轉到帶有地圖的頁面時,我都希望顯示其緯度/經度在數據庫中的標記。 換句話說,我想從數據庫中檢索數據(如果有的話),並且每次我進入/切換到該頁面時都在地圖上顯示標記。 我在為此實現代碼時遇到一些問題,如果有人可以看一下下面的代碼並告訴我我做錯了,我將不勝感激。

在此先感謝您的幫助。

這是代碼:

AdminWebControler.java

    /**
 *
 * @output ModelAndView
 * @description redirect to dashboard
 */

@RequestMapping(value = "/adminDashboard", method = RequestMethod.GET)
public ModelAndView adminPage() {
    userDao.updateLastLogin(getCurrentUser());
    final ModelAndView model = new ModelAndView();
    model.addObject("iLiteList", getAllILites());
    model.setViewName("adminDashboard");
    return model;
}

WebcontrollerUtilities.java`

protected List<ILites> getAllILites(){
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    List<ILites> ilites = mongoOperation.findAll(ILites.class, "ILites");

    System.out.println("4. Number of Ilites in the database = " + ilites.size());
    return ilites;
}

ILites.java

package mongodb.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

 @Document(collection = "ILiteDB")
 public class ILites {

private Double latitude;
private Double longitude;

/**
*

* @param longitude
* @param latitude
*/

public ILites( final Double latitude,final Double longitude) {
    super();

    this.longitude = longitude;
    this.latitude = latitude;
}

public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude) {
    this.longitude = longitude;
}
}

adminDashboard.jsp(jsp代碼的一部分(顯示地圖的行))

     <div class="row">
                <h4>
                    <strong>I-Lite Network View</strong>
                </h4>
                <div>
                    <hr class="Horizontal_divider_main" />
                </div>
                    <c:choose>
                        <c:when test="${not empty iLiteList}">                      
                            <div class="row" id="googleMap" style="width: 100%; height: 50%; margin-left: 3px;"> </div>
                        </c:when>
                        <c:otherwise>
                            <script type="text/javascript">
                                var lat  = new array();
                                <c:forEach items="${iLiteList}" var="ilites" > 
                                    //lat.push('${ilites.latitude}');
                                    lat.push('<c:out value='${ilites.latitude}' />');
                                </c:forEach> 

                                var lng  = new array();
                                <c:forEach items="${iLiteList}" var="ilites" > 
                                    //lng.push('${ilites.longitude}');
                                    lng.push('<c:out value='${ilites.longitude}' />');
                                </c:forEach> 


                                for (i = 0; i < lat.length; i++) { 
                                    var latLng = new google.maps.LatLng(lat[i], lng[i]);
                                    var marker = new google.maps.Marker({
                                    position: latLng,
                                    map: map,
                                    });

                                    marker.setMap(map); 

                                    var infowindow = new google.maps.InfoWindow({

                                    content: 'Latitude: ' + location.latitude + '<br>Longitude: ' + location.longitude 
                                     });

                                    google.maps.event.addListener(marker, 'click', function() {
                                     coordinates(map, marker, infowindow)});

                                    function coordinates(map, marker,infowindow){
                                        infowindow.open(map,marker);
                                    }
                                }


                            </script>
                        </c:otherwise>
                    </c:choose>

            </div>

map.js

var myCenter=new google.maps.LatLng(48.2188,11.6248);
var map;
function initialize()
{
var mapProp = {
  center:myCenter,
  zoom:18,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

    map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize
var jsList = new Array();
<c:forEach items="${javaList}" var="javaLvar" varStatus="status"> 
   objJs = new Object();
   objJs.someProperty = ${javaLvar}; 
   jsList.push(objJs);
</c:forEach> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM