如何将数组列表从DOM获取为javascript



我不知道如何获得来自Spring Controller的Objects arrayList。

这是我的控制器,它正在返回对象的数组列表。

控制器

@RequestMapping(value = "hello", method = RequestMethod.GET) public ModelAndView showMessage(Principal p,HttpServletResponse response) { ModelAndView mv = new ModelAndView("mapa"); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); mv.addObject("postossaude", Lists.newArrayList(PostosSaude.findAll())); return mv; }

这是Postosaude.java对象,它包含了我需要救援的所有信息。

@Entity
public class Postosaude {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String nome_posto_saude;
private double latitude;
private double longitude;
private String endereco;
public Postosaude()
{}
//gets and sets...
}

`

这是我的mapa.html,它正在接收列表(我使用Thymelaf获取列表)。

mapa.html

  <!DOCTYPE html>
    <html>
      <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
       var NodeListPostosSaude = document.getElementsByName('postossaude'); // ?? I want to obtain the "postossaude" list
            alert(NodeListPostosSaude.length);
        </script>
      </head>
      <body>
        <p th:text="${#vars.get('postossaude')}"> </p> //I check that the list is arriving OK (it prints the three objects that are in my list:
                                                      //"[palmaslab.mapas.repository.Postosaude@e7228c7a, palmaslab.mapas.repository.Postosaude@478808dc, palmaslab.mapas.repository.Postosaude@f35bea7d]"
       </body>
    </html>

所以,我想把这个List获取到我的脚本中,但我不知道怎么做。你知道有什么javascript方法或jQuery方法可以获取吗?

我的目标是,使用这个数组对象列表(里面是不同的带有地理坐标的对象)来定制谷歌地图,就像一样

根据评论中的讨论,这是我的解决方案-JSBin

HTML

<div id="container">
  <!-- P tags generated -->
  <p>Data Value 1</p>
  <p>Data Value 2</p>
  <p>Data Value 3</p>
  <p>Data Value 4</p>
</div>


JS

var container = document.getElementById('container');
var p = container.getElementsByTagName('p');
var array = [];
for (i = 0; i < p.length; i++) {
    array.push(p[i].innerHTML)
}
console.log(array)

最新更新