相信正在使用Play Framework 1.2的朋友一定会碰到使用Ajax在Play中做异步请求是碰到的困惑。
Play Framework 1.2版本是不支持原生的Javascript的Ajax请求的,如果你使用JQuery.Ajax去发送JSON数据,在Play的Controller内是无法获得JSON对象字符串的,今天通过搜索,终于找到了一个非常好的解决方案,可以让你的Play更好的支持Ajax请求。
在加入下面代码之前,需要你的Play中包含有GSON(Google的json库)。
首先在Play项目中添加一个类:
@Global
public class JsonObjectBinder implements TypeBinder<JsonObject> {
@Override
public Object bind(String name, Annotation[] annotations, String value,
Class actualClass, Type genericType) throws Exception {
return new JsonParser().parse(value);
}
}
Controller中接收Ajax请求的方法:
public static void auditPass(JsonObject body) {
String msgId = body.get("id").toString();
String content = body.get("content").toString();
HashMap<String, String> result = new HashMap<String, String>();
result.put("result", "true");
renderJSON(result);
}
Ajax请求发送代码:
var params = {
id : "12",
content: "test"
};
var jsonString= $.toJSON(params);
$.post('/Controller/auditPass', jsonString, function(data) {
alert(data.result);
});
怎么样?是不是有限像Spring MVC中的Restful风格呢?
分享到:
相关推荐
play framework api,play! framework api,play api
本文通过一个例子来演示play1.2.x在windows,和linux的部署方法.
Play Framework最新教程(12年),play框架教程
playframework,play! 书中范例代码下载地址: https://github.com/spinscale/play-cookbook 292 pages Publisher: Packt Publishing (September 4, 2011) Language: English ISBN-10: 1849515522 ISBN-13: 978-...
Litmus 是一个 playframework (1.x) 模块,它使 TDD 玩起来更容易。 它的主要特点是: 更具表现力的功能测试 轻松测试您的模型验证,包括内置或自定义验证 自定义测试运行器,允许您对测试进行分类并分别运行类别,...
1. Play Framework 介绍 2. 创建和发布 Play 应用 2.1 创建 Play 的工程 2.2 Play 常用指令 2.3 Play 应用的 JVM 调优 3. 如何读取静态资源 4. Play框架的配置文件 5. 使用 Play 框架开发 Java 应用 5.1 HTTP...
Play Framework Cookbook.pdf
Playframework 1.2.7 sdk zip包
DSCommon 一个在 Play Framework 1.2+ 上加速开发过程的库,为数据建模、文档流实现、数据安全、CRUD 屏幕表单带来了扩展功能。
playframework javaweb playframework javaweb
Mastering Play Framework for Scala
The Learning Play! Framework 2 has been created for web developers that are building web applications. The core idea is to focus on the ...
play framework2.01上半部分。
PlayFramework框架验证.pdf
Play framework框架
11.在play框架里使用Ajax - 94 - 通过jsAction标签使用jQuery - 95 - 12. Internationalization国际化支持 - 96 - 仅使用 UTF-8! - 96 - 国际化你的信息 - 96 - 通过应用程序定义支持的语言 - 96 - 依照你的区域定义...
Leverage the awesome features of Play Framework to build scalable, resilient, and responsive applications First published: May 2015 274page
通过简单的代码和思路写了play framework的简单的小例子,包括添加页面进行的routes配置,页面跳转,页面之间的数据传递等方面。暂时未使用h2内置数据库及ArrayList、HashMap在页面之间的数据传递。
Play Framework Cookbook
play2-war-plugin, 用于 Play Framework 2.x的WAR插件 用于 Play Framework 2.x的 WAR插件Current versions: Play 2.2.x : 1.2.1 Play 2.3.0 -> 2.3.1 : 1.3-beta1 (Scala 2.1