本篇文章给大家分享的是有关Springmvc实现数据格式化,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1、简介
2、示例
2.1、实体类
package com.yl.bean;
import java.util.Date;
public class User {
private String username;
private Date date;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", date=" + date +
'}';
}
}
2.2、控制器
package com.yl.controller;
import com.yl.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping("/stringToDate")
public ModelAndView jsonToObject(User user){
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("success");
System.out.println(user);
return modelAndView;
}
}
2.3、jsp
<form action="${pageContext.servletContext.contextPath}/stringToDate" method="post">
请输入日期(yyy-mm-dd):<input type="text" name="date"><br>
<button type="submit">提交</button>
</form>
2.4、数据格式化类
package com.yl.utils;
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* 日期格式化
*/
public class DateFormatter implements Formatter<Date> {
/**
* 字符串转Date
* @param text
* @param locale
* @return
* @throws ParseException
*/
@Override
public Date parse(String text, Locale locale) throws ParseException {
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
return sf.parse(text);
}
/**
* Date转字符串
* @param date
* @param locale
* @return
*/
@Override
public String print(Date date, Locale locale) {
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
return sf.format(date);
}
}
2.5、在配置文件注册自定义数据格式化类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定要扫描的包-->
<context:component-scan base-package="com.yl"></context:component-scan>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--数据格式化工厂-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<list>
<!--自定义格式化类-->
<bean class="com.yl.utils.DateFormatter"/>
</list>
</property>
</bean>
<!-- 设置静态资源不过滤-->
<mvc:default-servlet-handler/>
<!--开启springmvc注解支持,注册自定义数据格式化类-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>
3、使用注解实现数据格式化
package com.yl.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class User {
private String username;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", date=" + date +
'}';
}
}
以上就是Springmvc实现数据格式化,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。