JAVA springmvc 转换器
一、有时候springmvc给咱们提供的数据转换并不能转换所有类型比如说由字符串类型转换Date类型,这时候需要我们自定义转换器,帮助springmvc转换我们需要的类型。
二、1)定义我们的转换器:
package jd.com.contronller.jd.com.convert; import org.springframework.core.convert.converter.Converter; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class CustomStringToDate implements Converter<String,Date> {
@Override
public Date convert(String s) {
try {
Date date= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2)在springmvc配置文件添加我们的转换器:
<!--自定义转换器 注意在注解驱动里指明引用-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="jd.com.contronller.jd.com.convert.CustomStringToDate"/>
</set>
</property>
</bean>
3)需要注意:在注解驱动的添加我们的转换器id
<mvc:annotation-driven conversion-service="conversionService" />
因为转换器是处理器适配器进行处理的,如果我们不采用注解的方式,传统的引用如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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:dubbo="http://code.alibabatech.com/schema/dubbo"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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描带Controller注解的类 -->
<context:component-scanbase-package="cn.itcast.springmvc.controller"/> <!-- 转换器配置 -->
<beanid="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<propertyname="converters">
<set>
<beanclass="jd.com.contronller.jd.com.convert.CustomStringToDate"/>
</set>
</property>
</bean>
<!-- 自定义webBinder -->
<beanid="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<propertyname="conversionService"ref="conversionService"/>
</bean>
<!--注解适配器 -->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<propertyname="webBindingInitializer"ref="customBinder"></property>
</bean>
<!-- 注解处理器映射器 -->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
注意的:
前端页面想显示日期格式需要定义下面标签:
<td><fmt:formatDate value="${n.create_time}" pattern="yyyy-MM-dd HH:mm:ss" /> </td>
需要注意格式需要和后面转换器类的格式一致,否则报错!!!!
JAVA springmvc 转换器的更多相关文章
- [Java] SpringMVC工作原理之一:DispatcherServlet
一.DispatcherServlet 处理流程 在整个 Spring MVC 框架中,DispatcherServlet 处于核心位置,它负责协调和组织不同组件完成请求处理并返回响应工作.在看 Di ...
- JEECG(二) JEECG框架下调用webservice java springmvc maven 调用 webservice
JEECG系列教程二 如何在JEECG框架下使用webservice 本文所使用的webservice是c#开发的 其实无论是什么语言开发的webservice用法都一样 java springmvc ...
- java springMVC 报400错误问题
java springMVC 中如果报400错误 很有可能是因为时间转换的问题. 我在项目中就遇到了这个问题,是因为我少引用了一个库,如果是因为时间问题的话添加以下依赖就可以解决. <depen ...
- JAVA springmvc+spring+mybatis整合
一.springmvc---controller spring----service mybatiss---dao pring(包括springmvc).mybatis.mybatis-sprin ...
- java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis
A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单; 技术:313596790freemaker模版技术 ,0个代码不用写 ...
- Java SpringMVC实现国际化整合案例分析(i18n)
所谓国际化就是支持多种语言,web应用在不同的浏览环境中可以显示出不同的语言,比如说汉语.英语等.下面我将以具体的实例来举例说明: (1)新建动态Javaweb项目,并导入几个SpringMVC必需的 ...
- java springmvc Log4j filter等(稍微完善一下项目)
仅供参考-接上文 springmvc 1.设置Log4jConfigListener日志监听(可以为开发调试.发布后运行的意外调试.等) 在src/main/resources目录下新建log4j. ...
- BootStrap-validator 使用记录(JAVA SpringMVC实现)
BootStrap 是一个强大的前面框架,它用优雅的方式解决了网页问题.最近正在使用其开发网站的表单验证,一点体会记录如下: 注:本文中借鉴了博客Franson 的文章<使用bootstrapv ...
- [java]SpringMVC+Swagger实现自动接口
项目使用SpringMVC+Maven 1.在站点项目的POM文件中引入Swagger的jar包 <properties> <project.build.sourceEncoding ...
随机推荐
- Java - "JUC线程池" 线程状态与拒绝策略源码分析
Java多线程系列--“JUC线程池”04之 线程池原理(三) 本章介绍线程池的生命周期.在"Java多线程系列--“基础篇”01之 基本概念"中,我们介绍过,线程有5种状态:新建 ...
- git 报错:error: failed to push some refs to 'https://github.com/Anderson-An/******.git'(已解决)
提交push 报错: $ git push origin masterTo https://github.com/Anderson-An/******.git ! [rejected] master ...
- 08:Vigenère密码
08:Vigenère密码 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 16世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法— ...
- 数据库查询字段为null 时,返回0
oracle select nvl(字段名,0) from 表名; sqlserver select isnull(字段名,0) from 表名; mysql select ifnull(字段名,0) ...
- jQuery复选框全选和全选取消
jQuery(".salaryIds").each(function(){ if(jQuery("#salaryIds").attr("checked ...
- 关于flex布局兼容
(做个记录) 一.W3C各个版本的flex 2009 version 标志:display: box; or a property that is box-{*} (eg. box-pack) 201 ...
- php用smarty来做简易留言系统,明细步骤简单操作
留言信息是之前用php做过的一个例子,现在把它用smarty模板来做 大概是这样子 点击发布信息 然后填写内容,发送后会返回表格,写的内容都会出现在表格里 数据库的数据是这样的: 先建两个文件.php ...
- 机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes)
机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes) 关键字:朴素贝叶斯.python.源码解析作者:米仓山下时间:2018-10-2 ...
- .NET Dispose模式的实现
以下是代码: /// <summary> /// Dispose Pattern /// </summary> /// <remarks> /// 由逻辑可知: / ...
- 无法将数据库从SINGLE_USER模式切换回MULTI_USER模式(Error 5064),及查找SQL Server数据库中用户spid(非SQL Server系统spid)的方法
今天公司SQL Server数据库无意间变为SINGLE_USER模式了,而且使用如下语句切换回MULTI_USER失败: ALTER DATABASE [MyDB] SET MULTI_USER W ...