在Spring MVC中使用Ajax [英] Using ajax with Spring MVC
问题描述
我目前正在使用Spring MVC,并且尝试使用ajax做一些事情.基本上,我现在要做的是在网页上动态显示控制器的结果.
I am currently using the Spring MVC, and I am trying to do some stuff with ajax. Basically what I want to do now is display the result from a controller dynamically on a webpage.
I.E.用户按下按钮,它会转到"whatever.do"控制器,并获得列表并显示该列表,而无需重新加载该页面.
I.E. A user pushes a button it goes to the "whatever.do" controller and gets a list and displays that list without having to reload that page.
任何人都知道任何好的教程或示例项目吗?
Anyway does anyone know any good tutorials or example projects?
推荐答案
这很简单,我什至不认为不需要专门的教程(除了通用的spring-mvc教程).
It is very simple, I don't even think a special tutorial is needed (apart from a generic spring-mvc one).
- 制作一个
@RequestMapping("/foo")
方法,该方法返回一个List<Foo>
-
dispatcher-servlet.xml
中具有<mvc:annotation-driven />
以激活处理程序映射和转换器 - 将Jackson(json序列化器)放在类路径上
- 使用
$.getJSON("/foo", function(data) {..});
(jquery)-您将获得Foo
对象的JSON编码列表
- Make a
@RequestMapping("/foo")
method that returns aList<Foo>
- Have
<mvc:annotation-driven />
in yourdispatcher-servlet.xml
to activate handler mappings and convertors - Put Jackson (json serializer) on your classpath
- Use
$.getJSON("/foo", function(data) {..});
(jquery) - you will get a JSON-encoded list of yourFoo
objects
Spring将检测到浏览器请求json响应,并使用Jackson转换您的对象.
Spring will detect that the browser requests a json response, and converts your objects using Jackson.
这篇关于在Spring MVC中使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!