我不知道如何获取来自Spring Controller的Objects arrayList。
这是我的Controller,它返回一个数组对象列表。
控制器
@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,它正在接收列表(我使用Thymeleaf获取列表)。
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方法来获得它吗?
我的目标是,使用此数组对象列表(在其内部是与地理坐标不同的对象)来制作像that
这样的googlemaps答案 0 :(得分:0)
根据评论中的讨论,这是我的解决方案 - 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)