博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC中前后台数据传输小结
阅读量:6902 次
发布时间:2019-06-27

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

前台向后台传递参数:

  @ResponseBody    @RequestMapping(value = "/findById/{id}", method = { RequestMethod.POST,            RequestMethod.GET })    public void findById(@PathVariable("id") Long id) {        Person person=new Person();        person.setId(id);    }

  访问地址为:项目地址+/findById/1.do

如果参数是一个对象bean:(@RequestBody注解帮助自动封装成bean,前台只需要传递格式正确的json)

@ResponseBody    @RequestMapping(value = "/findById", method = { RequestMethod.POST,            RequestMethod.GET })    public void findById(@RequestBody Person person) {        person.setId(100);    }

如果需要有返回值到前台:(普通bean或者list)

@ResponseBody    @RequestMapping(value = "/findById/{id}", method = { RequestMethod.POST,            RequestMethod.GET })    public Person findById(@PathVariable("id") Long id) {        Person person=new Person();        person.setId(id);        return person;    }

如果需要返回json,先进行配置,用的比较多的应该是下面两种:

application/json;charset=UTF-8
text/html;charset=UTF-8
WriteMapNullValue
QuoteFieldNames
DisableCircularReferenceDetect

以及:

text/html;charset=UTF-8

代码示例:

@ResponseBody    @RequestMapping(value = "/findById/{id}", method = { RequestMethod.POST,            RequestMethod.GET })    public Map findById(@PathVariable("id") Long id) {        Person person=new Person();        person.setId(id);        Map
map = new HashedMap(); map.put("total", "1"); map.put("value", person); return map; }

附带mybatis的分页功能

maven包配置:

com.github.pagehelper
pagehelper
3.7.3

service层分页代码示例:

@Override    public ResultBean findByParams(Person person,            HttpServletRequest request) {        int currentPage = Integer.parseInt(request.getParameter("page") == null ?"1":request.getParameter("page"));//当前页        int pageSize    = Integer.parseInt(request.getParameter("rows")== null?"10":request.getParameter("rows"));//每页条数        Page
page = PageHelper.startPage(currentPage, pageSize); List
result=personDao.findByParams(person); Map
resMap = new HashMap
(); resMap.put("rows",result); resMap.put("total",page.getTotal()); ResultBean resultBean = new ResultBean(); resultBean.setResultObj(resMap); return resultBean; }

 

  

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

你可能感兴趣的文章
3月30日作业
查看>>
公司电话突然不能打外线故障处理过程
查看>>
Windows Server 2008流媒体服务器---创建播放列表
查看>>
centos添加批量添加ip提示无效参数
查看>>
PHP mkdir函数
查看>>
Linux基础命令---检查密码文件pwck
查看>>
python这+=和=的拓展知识
查看>>
oracle集群件
查看>>
linux shell 中"2>&1"含义
查看>>
oracle 11g RAC grid安装前准备
查看>>
01背包 暴力搜索
查看>>
RIP区域和OSPF区域通信
查看>>
MySQL
查看>>
网络安全系列之四十 在Linux中设置SET位权限
查看>>
SCCM OSD部署排错
查看>>
十道非常好的shell脚本试题
查看>>
app项目案例一手机浏览器
查看>>
java 中 isEmpty和isBlank区别
查看>>
申请SSL证书怎样验证域名所有权
查看>>
麒麟开源堡垒机集中管控平台软件简介
查看>>