前台向后台传递参数:

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

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

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

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

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

  1. @ResponseBody
  2. @RequestMapping(value = "/findById/{id}", method = { RequestMethod.POST,
  3. RequestMethod.GET })
  4. public Person findById(@PathVariable("id") Long id) {
  5. Person person=new Person();
  6. person.setId(id);
  7. return person;
  8. }

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

  1. <bean id="fastJsonHttpMessageConverter"
  2. class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  3. <property name="supportedMediaTypes">
  4. <list>
  5. <value>application/json;charset=UTF-8</value>
  6. <value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
  7. </list>
  8. </property>
  9. <property name="features">
  10. <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
  11. <value>WriteMapNullValue</value>
  12. <value>QuoteFieldNames</value>
  13. <value>DisableCircularReferenceDetect</value>
  14. </array>
  15. </property>
  16. </bean>

以及:

  1. <bean
  2. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  3. <property name="messageConverters">
  4. <list>
  5. <ref bean="mappingJacksonHttpMessageConverter" />
  6. </list>
  7. </property>
  8. </bean>
  9. <bean id="mappingJacksonHttpMessageConverter"
  10. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  11. <property name="supportedMediaTypes">
  12. <list>
  13. <value>text/html;charset=UTF-8</value>
  14. </list>
  15. </property>
  16. </bean>

代码示例:

  1. @ResponseBody
  2. @RequestMapping(value = "/findById/{id}", method = { RequestMethod.POST,
  3. RequestMethod.GET })
  4. public Map findById(@PathVariable("id") Long id) {
  5. Person person=new Person();
  6. person.setId(id);
  7. Map<String, Object> map = new HashedMap();
  8. map.put("total", "1");
  9. map.put("value", person);
  10. return map;
  11. }

附带mybatis的分页功能

maven包配置:

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper</artifactId>
  4. <version>3.7.3</version>
  5. </dependency>

service层分页代码示例:

  1. @Override
  2. public ResultBean findByParams(Person person,
  3. HttpServletRequest request) {
  4. int currentPage = Integer.parseInt(request.getParameter("page") == null ?"1":request.getParameter("page"));//当前页
  5. int pageSize = Integer.parseInt(request.getParameter("rows")== null?"10":request.getParameter("rows"));//每页条数
  6. Page<?> page = PageHelper.startPage(currentPage, pageSize);
  7. List<Person> result=personDao.findByParams(person);
  8. Map<String,Object> resMap = new HashMap<String,Object>();
  9. resMap.put("rows",result);
  10. resMap.put("total",page.getTotal());
  11. ResultBean resultBean = new ResultBean();
  12. resultBean.setResultObj(resMap);
  13. return resultBean;
  14. }

  

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. terminal color

    自己喜欢的前背景颜色1: foreground:    ab8d0f        yellow        c4a000        default background:    23292b  ...

  2. input编辑框编辑状态切换

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. php基础(六)Include

    本文内容来自http://www.w3school.com.cn/php/php_file.asp 服务器端包含 (SSI) 用于创建可在多个页面重复使用的函数.页眉.页脚或元素. include ( ...

  4. jquery的跳转.禁止全url跳转.只需控制器+方法

    success:function(){ window.location.href="/enterprise/show"; } success:function(){ window. ...

  5. luci-bwc

    文件位于:   ../feeds/luci/modules/admin-full/src/luci-bwc.c 功能: Very simple bandwidth collector cache fo ...

  6. openwrt启动过程(脚本)

    来源:  http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...

  7. centos 7上搭建HDP2.3集群

    centos 7上安装 cat /etc/redhat-release

  8. ubuntu下的ssh工具gstm

    (转自:http://www.nenew.net/ubuntu-ssh-gstm.html) 首先安装: sudo apt-get install gstm 就可以安装,当然你也可以到http://s ...

  9. 设n是奇数,证明:16|(n4+4n2+11)(整除原理1.1.1)

    设n是奇数,证明:16|(n4+4n2+11) 解: 令n=2k+1,k∈z n4+4n2+11 =(2k+1)4+4(2k+1)2+11 =(4k2+4k+1)2+(2k+1)2+11 =16k4+ ...

  10. php随意笔记

    local(局部) global(全局)global 关键词用于访问函数内的全局变量.$GLOBALS[index] 的数组中存储了所有的全局变量.这个数组在函数内也可以访问,并能够用于直接更新全局变 ...