博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在JFinal的Controller中接收json数据
阅读量:7041 次
发布时间:2019-06-28

本文共 3431 字,大约阅读时间需要 11 分钟。

JFinal中接收URL中的参数或者model中的参数是很方便的,但是对于web2.0的网站来说,经常会以json方式提交比较复杂的数据,比如一个查询,包含了各种过滤条件和排序分页,前端脚本可能提交的数据是这样的:

{    "type":1,    "key":"keyword",    "paging":{        "size":50,        "index":0    },    "sort":{        "field":"time",        "type":"desc"    }}

像SpringMVC就提供了@RequestBody将数据绑定到json对象上,但是jFinal不支持,需要自己从POST中读取并解析这个json数据,先定义一个与请求同结构的Java对象,比如起名叫QueryRequest:

packagecom.demo;import com.demo.Paging;import com.demo.Sort;public class QueryRequest {    private int type;    private String key;    private Paging paging;    private Sort sort;    public int getType() {        return type;    }    public void setType(int type) {        this.type = type;    }    public String getKey() {        return key;    }    public void setKey(String key) {        this.key = key;    }    public Paging getPaging() {        return paging;    }    public void setPaging(Paging paging) {        this.paging = paging;    }    public Sort getSort(){        return sort;    }    public void setSort(Sort sort){        this.sort = sort;    }}

其中用到了Paging和Sort两个类:

package com.demo;public class Paging {    private int size;    private int index;        public int getSize() {        return size;    }    public void setSize(int size) {        this.size = size;    }    public int getIndex() {        return index;    }    public void setIndex(int index) {        this.index = index;    }}
package com.demo;public class Sort {    private String field;    private String type;        public String getField() {        return field;    }    public void setField(String field) {        this.field = field;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }}

然后在Controller里就从request中读取json字符串,然后调用fastjson解析提交的数据了,:

@Before(POST.class)public void getData(){    try{        //从requst中读取json字符串        StringBuilder json = new StringBuilder();         BufferedReader reader = this.getRequest().getReader();        String line = null;        while((line = reader.readLine()) != null){            json.append(line);        }        reader.close();        //调用fastjson解析出对象        QueryRequest request = JSONObject.parseObject(json.toString(), QueryRequest.class);                //然后就可以使用request得到请求的所有数据了        //下略        //.......    }    catch(Exception ex){        //异常处理,略    }        renderText("测试");}

转换部分会经常使用,可以提出来:

/** * 取Request中的数据对象 * @param valueType * @return * @throws Exception  */protected 
 T getRequestObject(Class
 valueType) throws Exception {    StringBuilder json = new StringBuilder();    BufferedReader reader = this.getRequest().getReader();    String line = null;    while((line = reader.readLine()) != null){        json.append(line);    }    reader.close();        return JSONObject.parseObject(json.toString(), valueType);}

使用的时候一句就行了:

QueryRequest requst = getRequestObject(QueryRequest.class);

另外附上前端ajax调用的脚本:

$.ajax({    "url": "/home/getDate",      //路径    "cache": false,              //不缓存    "async": true,               //异步    "type": "POST",              //POST方式提交    "dataType": "json",          //json格式,重要    "contentType": "application/json",      //json格式    "data": {},                  //要提交的数据对象    success: function (json) { //成功处理    },    error: function (x, e) {  //异常处理    }});

PS:很喜欢jFinal,相比于SpringMVC庞大的体积,jFinal真是的很小巧。

PPS:使用的是jFinal-2.0,配合fastjson-1.2.3,之前用fastjson-1.2.4时会有问题。

转载地址:http://vbqal.baihongyu.com/

你可能感兴趣的文章
windows下apache+php+mysql 环境配置方法
查看>>
solais 10中执行crontab -e报unkown terminal type
查看>>
jar包下载网址
查看>>
virtualbox使用总结
查看>>
CentOS7上搭建VNC服务
查看>>
Apache select与Nginx epoll模型区别
查看>>
Vue组件入门
查看>>
广告条
查看>>
『中级篇』Docker-cloud介绍(54)
查看>>
到期的Navicat需要注册码--解决方案
查看>>
【Unity3D基础教程】给初学者看的Unity教程(二):所有脚本组件的基类 -- MonoBehaviour的前世今生...
查看>>
×××小程序接口调试
查看>>
OpenSSL安装
查看>>
我是如何沉迷于linux系统的?
查看>>
微软正式释出基于 Chromium 的全新版本 Edge
查看>>
SHELL菜单select练习
查看>>
aaa
查看>>
wwballizer以及awstat下的apache日志监控
查看>>
NO3.Shell脚本学习——编写Shell脚本
查看>>
Linux 磁盘管理及基础命令使用
查看>>