Springmvc數(shù)據(jù)格式化原理及代碼案例
更新時(shí)間:2020年10月26日 10:00:53 作者:Y_wee
這篇文章主要介紹了Springmvc數(shù)據(jù)格式化原理及代碼案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、簡(jiǎn)介
- Converter可以將一種類型轉(zhuǎn)換成另一種類型,是任意Object之間的類型轉(zhuǎn)換。
- Formatter則只能進(jìn)String與任意Object對(duì)象的轉(zhuǎn)換,它提供解析與格式化兩種功能
- 解析:將String類型字符串轉(zhuǎn)換為任意Objec對(duì)象,
- 格式化:將任意Objec對(duì)象轉(zhuǎn)換為字符串進(jìn)行格式化顯示。
- 使用Formatter
- 實(shí)現(xiàn)Formatter接口定義一個(gè)類,T為要解析得到或進(jìn)行格式化的數(shù)據(jù)類型。
- 在類中實(shí)現(xiàn)兩個(gè)方法
- String print(T t,Locale locale):把T類型對(duì)象解析為字符串形式返回
- T parse(String sourse,Locale locale):由字符串解析得到T類型對(duì)象。
2、示例
2.1、實(shí)體類
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">
請(qǐng)輸入日期(yyy-mm-dd):<input type="text" name="date"><br>
<button type="submit">提交</button>
</form>
2.4、數(shù)據(jù)格式化類
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> {
/**
* 字符串轉(zhuǎn)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轉(zhuǎn)字符串
* @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、在配置文件注冊(cè)自定義數(shù)據(jù)格式化類
<?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>
<!--數(shù)據(jù)格式化工廠-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<list>
<!--自定義格式化類-->
<bean class="com.yl.utils.DateFormatter"/>
</list>
</property>
</bean>
<!-- 設(shè)置靜態(tài)資源不過(guò)濾-->
<mvc:default-servlet-handler/>
<!--開(kāi)啟springmvc注解支持,注冊(cè)自定義數(shù)據(jù)格式化類-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>
3、使用注解實(shí)現(xiàn)數(shù)據(jù)格式化
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 +
'}';
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Spring mvc JSON數(shù)據(jù)交換格式原理解析
- Java SpringMVC框架開(kāi)發(fā)之?dāng)?shù)據(jù)導(dǎo)出Excel文件格式實(shí)例詳解
- springMVC返回復(fù)雜的json格式數(shù)據(jù)方法
- Spring MVC通過(guò)添加自定義注解格式化數(shù)據(jù)的方法
- Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解
- Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解
- SpringMVC中Json數(shù)據(jù)格式轉(zhuǎn)換
- 解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問(wèn)題處理
- SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
相關(guān)文章
通過(guò)Spring Boot整合Mybatis分析自動(dòng)配置詳解
這篇文章主要給大家介紹了關(guān)于如何通過(guò)Spring Boot整合Mybatis分析自動(dòng)配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
淺談System.getenv()和System.getProperty()的區(qū)別
這篇文章主要介紹了System.getenv()和System.getProperty()的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot整合log4j2日志的實(shí)現(xiàn)
在項(xiàng)目推進(jìn)中,如果說(shuō)第一件事是搭Spring框架的話,那么第二件事情就是在Sring基礎(chǔ)上搭建日志框架,大家都知道日志對(duì)于一個(gè)項(xiàng)目的重要性,尤其是線上Web項(xiàng)目,因?yàn)槿罩究赡苁俏覀兞私鈶?yīng)用如何執(zhí)行的唯一方式。此篇文章是博主在實(shí)踐中用Springboot整合log4j2日志的總結(jié)2021-06-06

