springMVC + fastjson

来源:互联网 发布:电解质溶液的电导数据 编辑:程序博客网 时间:2024/06/08 10:38

在项目中一直有用到springMVC传参和用fastjson转换json数据,但老是记不住api,所以写一篇文章记录一下在实际项目中经常会使用到的方法。

springMVC简介:

Front controller - 前端控制器
在springMVC中,前端控制器就是DispatcherServlet
request --> DispatcherServelet --> Controller --> Model --> DispatcherServlet --> View

一.传参的方式

传参的方式有两种:

1.在连接后跟参数:url?param=value,param=value...  

2.ajax :ajax({  data:{param:value,param:value}})

二.controller接收参数

1.传普通数据格式:

url?param1=value,param2=value

ajax({  data:{param1:value,param2:value}})

public String view(@RequestParam("param1") Integer id , String param2)){ } 或者

public String view(Integer param1 ,  String param2)){ }

2.传json对象:

//前端传参var pageCondition = {   pageNo : page,   pageSize : 10,   filters : filter}
$.ajax({   url :  baseUrl,   type : 'get',   dataType : 'json', //必须加上json数据格式   data : pageCondition})//后台直接以对象接收
public void getAll(PageCondition pageCondition , HttpServletResponse response){}

3.传json字符串

//前端传参var pageCondition = {   pageNo : page,   pageSize : 10,   filters : filter}
$.ajax({   url :  baseUrl,   type : 'get',   dataType : 'json', //必须加上json数据格式   data : {
pageCondition :JSON.stringify(pageCondition; //将对象转化为json字符串
    } })//后台直接以对象接收
public void getAll(String pageCondition , HttpServletResponse response){
        1.若是对象 
        
        PageCondition object = JSON.parseObject(json_str, PageCondition.class);
        2.若是数组
List<PageCondition> list = JSON.parseArray(json_str, PageCondition.class);
}


原创粉丝点击