前台向后台传递参数:

  @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,先进行配置,用的比较多的应该是下面两种:

    <bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>

以及:

    <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

代码示例:

    @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<String, Object> map = new HashedMap();
map.put("total", "1");
map.put("value", person);
return map;
}

附带mybatis的分页功能

maven包配置:

        <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.3</version>
</dependency>

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<Person> result=personDao.findByParams(person);
Map<String,Object> resMap = new HashMap<String,Object>();
resMap.put("rows",result);
resMap.put("total",page.getTotal());
ResultBean resultBean = new ResultBean();
resultBean.setResultObj(resMap);
return resultBean;
}

  

Spring MVC中前后台数据传输小结的更多相关文章

  1. 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

    后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...

  2. spring mvc 项目 相关配置文件小结

    web.xml文件主要配置如下: 需要加载的配置文件: 类路径下,可以使用通配符配置  类似:classpath:conf/spring/*/*.xml, <context-param> ...

  3. Spring mvc前台后台传值

    前台向后台传值: ①同名参数传递:form表单中提交input,Controller方法入参中,直接以同名参数获取 ②不同名参数传递:from表单提交input,Controller方法入参中需要使用 ...

  4. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  5. spring mvc从后台往前台传参数的三种方式

    第一种:使用Model对象(常用) 第一步:使用model对象往前台传递数据 第二步:在jsp中接收从后台传递过来的参数 第二种:使用HttpServletRequest对象 第一步:使用HttpSe ...

  6. spring mvc 解决后台传递值乱码问题

    在Web-xml 配置添加过滤器 <!-- 配置过滤器 解决乱码问题 --> <filter> <filter-name>CharacterEncodingFilt ...

  7. 【spring mvc】后台的API,测试中,总提示接口实体的某一个字段不能为null,但是明明给值了还提示不能为空

    实体是这三个字段 接口的实现类Controller 前台测试给值 依旧报错 解决方法: 需要添加@RequestBody注解

  8. IntelliJ idea创建Spring MVC的Maven项目

    参考:http://my.oschina.net/gaussik/blog/385697?fromerr=Pie9IlFV 创建Maven Web项目 菜单File->New Project可进 ...

  9. Spring MVC 以.html为后缀名访问获取数据,报406 Not Acceptable错误。

    如题,最近以spring mvc作为后台框架,前端异步获取数据时(.html为后缀名的访问方式),报406 Not Acceptable错误.当初都不知道啥原因,前后台都没报错就是返回不了数据,于是查 ...

随机推荐

  1. apache 安装[转]

    Apache简介         Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广 ...

  2. Unity5系列资源管理AssetBundle——打包

    资源管理是游戏开发的重要环节,Unity中使用AssetBundle可以非常方便地帮我们打包和更新游戏内容,在5系列中,AssetBundle更是方便好用,现在让我们先进行打包吧. 刚说了,5系列打包 ...

  3. Lua入门基础

    什么是Lua Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Rober ...

  4. luci-bwc(记录流量功能)

    使用方法如下: root@openwrt:/# luci-bwcUsage:        luci-bwc [-t timeout] -i ifname             // ifname为 ...

  5. 高精度运算专题2-减法运算(The subtraction operation)

    这个专题呢,我就来讲讲高精度的减法,下面是三个计算减法的函数,第一个函数是char类型的,要对字符串进行数字转换,而第二个是两个int类型的数组,不用转换成数字,第三个则更为优化,用a数组-b数组放回 ...

  6. ural 1355. Bald Spot Revisited(数的素因子划分)

    1355. Bald Spot Revisited Time limit: 1.0 secondMemory limit: 64 MB A student dreamt that he walked ...

  7. 当用反射获取一个model,这个model里面字段有nullable的时候,获取字段真实类型

    Using Reflection to Determine whether an Type is Nullable And Get the underlying Type /// <summar ...

  8. vs打开项目,创建虚拟目录,提示权限不足无法写入配置文件

    如题,从源代码管理器上获取下来程序后,自己打开始,提示如题,尝试过以管理员启动vs,给目录权限提升,修改csproj项目配置文件(修改userIIS节点)and so on,无意间在用别的文档编辑器编 ...

  9. Linux通过XAMPP集成软件包搭建LAMPP环境

    前面介绍过一篇“Linux手动搭建LAMP环境”,今天再来整理一篇“Linux通过XAMPP集成软件包搭建LAMPP环境”. 其实当初整理通过XAMPP集成软件包搭建LAMPP环境的原因是这样的: 自 ...

  10. Hibernate框架--关联映射,一对多,多对多 inverse cascade

    回顾Hibernate: 1. hibernate开发环境搭建 ----> 引入jar: hibernate.jar + required + jpa + 驱动包 ---> hiberna ...