在JFinal的Controller中接收json数据

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:
java

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两个类:web

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解析提交的数据了,:ajax

@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("测试");
}


转换部分会常用,能够提出来:
json

/**
 * 取Request中的数据对象
 * @param valueType
 * @return
 * @throws Exception 
 */
protected <T> T getRequestObject(Class<T> 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调用的脚本:app

$.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时会有问题。async

相关文章
相关标签/搜索