在Spring MVC中使用Ajax [英] Using ajax with Spring MVC

查看:76
本文介绍了在Spring MVC中使用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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).

  1. 制作一个@RequestMapping("/foo")方法,该方法返回一个List<Foo>
  2. dispatcher-servlet.xml中具有<mvc:annotation-driven />以激活处理程序映射和转换器
  3. 将Jackson(json序列化器)放在类路径上
  4. 使用$.getJSON("/foo", function(data) {..});(jquery)-您将获得Foo对象的JSON编码列表
  1. Make a @RequestMapping("/foo") method that returns a List<Foo>
  2. Have <mvc:annotation-driven /> in your dispatcher-servlet.xml to activate handler mappings and convertors
  3. Put Jackson (json serializer) on your classpath
  4. Use $.getJSON("/foo", function(data) {..}); (jquery) - you will get a JSON-encoded list of your Foo objects

Spring将检测到浏览器请求json响应,并使用Jackson转换您的对象.

Spring will detect that the browser requests a json response, and converts your objects using Jackson.

这篇关于在Spring MVC中使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆