Springmvc数据格式化原理及代码案例

网友投稿 213 2023-02-28

Springmvc数据格式化原理及代码案例

1、简介

Converter可以将一种类型转换成另一种类型,是任意Object之间的类型转换。

Formatter则只能进String与任意Object对象的转换,它提供解析与格式化两种功能

解析:将String类型字符串转换为任意Objec对象,

格式化:将任意Objec对象转换为字符串进行格式化显示。

使用Formatter

实现Formatter接口定义一个类,T为要解析得到或进行格式化的数据类型。

在类中实现两个方法

String print(T t,Locale locale):把T类型对象解析为字符串形式返回

T parhttp://se(String sourse,Locale locale):由字符串解析得到T类型对象。

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;

}

@OverridrYRSMyUmEbe

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

请输入日期(yyy-mm-dd):

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

* @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、在配置文件注册自定义数据格式化类

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=" http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=" http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

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 +

'}';

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 注解学习笔记
下一篇:电视节目预告(电视节目预告电视猫)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~