解决:springmvc中接收date数据问题

这里提供三种解决方案。

一.局部转换 :只是对当前Controller类有效

springMVC.xml中添加:

  <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <!-- String类型解析器,允许直接返回String类型的消息 -->
    <bean id="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter" />
    <!-- 日期转换 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="com.rw.tools.ConvertDate"/>
        </property>
    </bean>
Controller 类文件中添加:
@Controller
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder binder){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        binder.registerCustomEditor(Date.class,dateEditor);
    }

二.全局转换

1.创建convertDate类实现WebBindingInitializer接口

public class convertDate implements WebBindingInitializer{

    @Override
    public void initBinder(WebDataBinder binder, WebRequest request) {
        // TODO Auto-generated method stub
        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}

2.在Spring-MVC.xml中配置日期转换

<!-- 日期转换 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="com.wx.web.convertDate"/>
        </property>
    </bean>

三.实体类属性方法配置

 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")//接受前台的时间格式 传到后台的格式
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")//作用:后台的时间 格式化 发送到前台
    private Date date;

@JsonFormat 默认是标准时区的时间, 北京时间 东八区 timezone=”GMT+8” 
作用:后台的时间 格式化 发送到前台

@DateTimeFormat 接受前台的时间格式 传到后台的格式

原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/9379231.html

时间: 2024-08-29 11:31:15

解决:springmvc中接收date数据问题的相关文章

在JFinal的Controller中接收json数据

JFinal中接收URL中的参数或者model中的参数是很方便的,但是对于web2.0的网站来说,经常会以json方式提交比较复杂的数据,比如一个查询,包含了各种过滤条件和排序分页,前端脚本可能提交的数据是这样的: {     "type":1,     "key":"keyword",     "paging":{         "size":50,         "index":

【Spring】SpringMVC中浅析Date类型数据的传递

在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置自己的date格式 CustomDateEditor cust = new CustomDateEditor(dsf,true); bin.registerCustomEditor(Date

SpringMVC中接收不同类型的数据

1.直接将参数写在controller相应的方法形参中,适用于get方法,不适用于post方法 /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @param username * @param password * @return */ @RequestMapping("/addUser1") public String addUser1(String username,String password) { System.out.println(&quo

解决Ubuntu中phpmyadmin对数据上传上限2M

本文部分参考自:http://www.myhack58.com/Article/sort099/sort0102/2011/29396.htm 原文有少量错误或者过时的(相对于ubuntu15来说)内容,已做修改. Ubuntu中php.ini的路径和其他linux不一样,路径为:/etc/php5/apache2/php.ini sudo gedit /etc/php5/apache2/php.ini 1.查找 post_max_size,指通过表单POST给PHP的所能接收的最大值,包括表单

解决spark中遇到的数据倾斜问题

一. 数据倾斜的现象 多数task执行速度较快,少数task执行时间非常长,或者等待很长时间后提示你内存不足,执行失败. 二. 数据倾斜的原因 常见于各种shuffle操作,例如reduceByKey,groupByKey,join等操作. 数据问题 key本身分布不均匀(包括大量的key为空) key的设置不合理 spark使用问题 shuffle时的并发度不够 计算方式有误 三. 数据倾斜的后果 spark中一个stage的执行时间受限于最后那个执行完的task,因此运行缓慢的任务会拖累整个

解决js中post提交数据并且跳转到指定页面的问题总结

今天在开发中过程中遇到了这个问题,js中利用JQuery中的 $.post("url", id, function(){}); 这个方法是数据提交正常,但是后台处理完成之后跳转无法成功.经过分析,后台只是将要跳转的页面的html发回了前台,也就是说前台与后台利用JQuery的post方法只能发生数据的交互,并不能实现页面的跳转.所以,这就导致无法正常跳转. 解决方案: 1.将我们要提交的数据加入到指定的form表单中,在form表单中设置需要跳转的url和方法类型 post或get等

java中,Date数据类型和JSONObject数据类型之间的转换

import java.text.SimpleDateFormat;import java.util.Date;import net.sf.json.JSONObject; public class DateTest { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"

解决springmvc中使用redirect跳转后https变为http

方法一:配置文件修改ViewResolver的 redirectHttp10Compatible 属性,这个属性是为了兼容 http1.0协议. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.s

SpringMVC中数据转换

SpringMVC中接收到的数据都是String形式,然后再根据反射机制将String转换成对应的类型.如果此时想接收一个Date类型,那么我们可以定义一个转换器来完成. 例如,我们有下面的Emp类: package org.lyk.vo; import java.util.Date; public class Emp { private Integer empno; private String ename; private Double sal; private Date hiredate;