如何使用自动完成从 JSTL 中的列表中获取数据 spring mvc

How to get data from List in JSTL with autocomplete spring mvc

我正在使用 JSTL 和 Jquery 将数据导入自动完成,数据列表将从 Spring mvc

中的列表中获取

我的操作:

List<Map> mapList = googleMapLocationService.getAllGoogleMapLocations();
model.put("mapList", mapList);

jquery/jsp:

<script>
  $( function() {
  var availableTags =
      <c:forEach items="${mapList}" var="map">
      [         
          "<c:out value="${map.address}"/>"         
      ];
      </c:forEach>
  $( "#tags" ).autocomplete({
    source: availableTags
  });
 } );

  <div style="width:320px ;margin-left: -38px; margin-top: -24px">
    <input id="tags" path="tags" />
  </div>

已更新:(尝试将 'address' 替换为 'id')

<script>
$( function() {
var availableTags = []
    <c:forEach items="${mapList}" var="map">
        availableTags.push("<c:out value="'${map.id}', "/>");
    </c:forEach>
 $( "#tags" ).autocomplete({
   source: availableTags
 });
} );

结果:

当我输入 'keyword' any 时,似乎无法在自动完成列表中列出

我该如何解决这个问题?非常感谢!

此代码生成 javascript 数组结构。尝试检查一下。

 <script>
  $( function() {
  var availableTags =
      <c:forEach items="${mapList}" var="map" varStatus="totalCount">
      [         
          <c:out value="'${map.address}'">
          </c:out>
          <c:if test="${totalCount.count lt fn:length(mapList)}">
          <c:out value=",">
          </c:out>
          </c:if>      
      ];
      </c:forEach>
  $( "#tags" ).autocomplete({
    source: availableTags
  });
 } );