在Controller类中,需要返回JSON的方法上加上注释@ResponseBody,这是必须的。

然后spring-servlet.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 启动扫描所有的controller aokunsang-->
<context:component-scan base-package="com.xxx"/> <!-- 定义注解驱动Controller方法处理适配器 ,注:该适配器必须声明在<mvc:annotation-driven />之前,否则不能正常处理参数类型的转换 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<!-- 设置全局返回JSON到前端时日期格式化 -->
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean> <!-- 会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter
两个bean,是spring MVC为@Controllers分发请求所必须的。 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson) -->
<mvc:annotation-driven /> <!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->
<mvc:resources location="/resources/**" mapping="/resources/**"/> <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

springMVC框架返回JSON到前端日期格式化的更多相关文章

  1. SpringMVC返回JSON数据时日期格式化问题

    https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring   在运用SpringMVC框架开发时,可 ...

  2. js前端日期格式化处理

    js前端日期格式化处理 1.项目中时间返回值,很过时候为毫秒值,我们需要转换成 能够看懂的时间的格式: 例如: ​ yyyy-MM-dd HH:mm:ss 2.处理方法(处理方法有多种,可以传值到前端 ...

  3. SpringMVC中返回JSON时乱码的解决方案

    springMVC中返回JSON会出现乱码,解决如下: produces = "text/html;charset=UTF-8" @ResponseBody @RequestMap ...

  4. springMvc返回Json中自定义日期格式

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  5. SpringMVC+ajax返回JSON串

    一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...

  6. JSON中的日期格式化

    Json字符串中的日期格式化函数 ConvertJsonDate: function (jd) { var d = new Date(parseInt(jd.replace("/Date(& ...

  7. struts框架返回json数据

    设置返回result类型为json格式 <package name="cn.konngo.action" namespace="/" extends=&q ...

  8. SpringMVC解决@ResponseBody返回Json的Date日期类型的转换问题

    在做项目的时候,发现后台把Date类型的属性以json字符串的形式返回,前台拿不到转换后的日期格式,始终响应回去的都是long类型时间戳. 查阅资料之后找到解决方法: 方法一(在springmvc的x ...

  9. Struts2 SSH整合框架返回json时,要注意懒加载问题

    返回的这个json对象,要保证它里面的所有属性都已经取出来了(即不是proxy或者是懒加载),否则当struts框架将该对象转化成json数据时,会报出一个no session的错误. 因此你要将该懒 ...

随机推荐

  1. 『TensorFlow』读书笔记_Word2Vec

    由于暂时不会深入接触NLP相关领域,所以本章的内容就不过多涉及了,以后会进行学习,应该. Word2Vec又称Word Embeddings,中文称为"词向量"."词嵌入 ...

  2. 下载配置nodeJs,cnpm,webpack,vue-cli等,刚装的系统,所有东西重新配置

    最近重新装了系统,所有的环境都要重新配置了,做个笔记. 安装nodeJs: 可以参照教程:https://www.runoob.com/nodejs/nodejs-install-setup.html ...

  3. linux如何让一个程序崩溃后自动重启

    思路:  写一个脚本 监控程序的运行状态  没有运行启动运行 已运行不做操作. 如果在控制台启动脚本 注意必须  nohup sh xxx.sh & while true do ps -ef ...

  4. H5-移动端实现滑屏翻页-原生js/jquery

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

  5. 四种常见的 POST 提交数据方式对应的content-type取值

    application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 app ...

  6. Vue解决同一页面跳转页面不更新

    问题分析:路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新. 方案一:使用 watch 进行监听 watch: { /* = ...

  7. POJ 3254 Corn Fields (状压入门)

    Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M≤ 12; 1 ≤ N ≤ 12) ...

  8. Linux:Ubuntu系统的安装

    好久没更了,今天就更完这一期的Linux系统吧,这次主要安装的是常用Linux系统的之一:Ubuntu(乌班图)系统,这个系统和CentOS 7的安装步骤也是类似的,(我不采取用虚拟机的方法来安装,当 ...

  9. oh_my_zsh

    oh_my_zsh  zsh默认已经安装   cat /etc/shells   查看安装没有   (https://xiaozhou.net/learn-the-command-line-iterm ...

  10. python 前端 css

    CSS(Cascading Style Sheet,层叠样式表) 是一种用来表现HTML或XML等文件样式的计算机语言. 作用:是用来美化HTML标签的,相当于给页面化妆. 每个css 都是有两部分组 ...