在JFinal的Controller中接收json数据

简介:

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

1
2
3
4
5
6
7
8
9
10
11
12
{
     "type" :1,
     "key" : "keyword" ,
     "paging" :{
         "size" :50,
         "index" :0
     },
     "sort" :{
         "field" : "time" ,
         "type" : "desc"
     }
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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两个类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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解析提交的数据了,:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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( "测试" );
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
  * 取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);
}


使用的时候一句就行了:

1
QueryRequest requst = getRequestObject(QueryRequest. class );



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

1
2
3
4
5
6
7
8
9
10
11
12
13
$.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时会有问题。




     本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/1698113,如需转载请自行联系原作者




相关文章
|
3月前
|
JSON PHP 数据格式
|
1月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
2月前
|
XML 机器学习/深度学习 JSON
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
29 0
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
|
28天前
|
JSON 数据格式
糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
19 1
|
29天前
|
JSON 前端开发 数据格式
Ajax传递json数据
Ajax传递json数据
11 0
|
29天前
|
JSON 并行计算 API
使用CJSON/Nlohmann:快速简便地在C/C++中处理JSON数据
使用CJSON/Nlohmann:快速简便地在C/C++中处理JSON数据
74 0
|
1月前
|
JSON 数据格式 Python
Python生成JSON数据
Python生成JSON数据
22 0
|
1月前
|
JSON 数据可视化 Linux
数据可视化工具JSON Crack结合内网穿透实现公网访问
数据可视化工具JSON Crack结合内网穿透实现公网访问
数据可视化工具JSON Crack结合内网穿透实现公网访问
|
2月前
|
JSON fastjson Java
FastJSON操作各种格式的JSON数据
FastJSON处理各种格式的JSON数据
|
3月前
|
JSON PHP 数据格式
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)