欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springmvc數(shù)據(jù)格式化原理及代碼案例

 更新時間:2020年10月26日 10:00:53   作者:Y_wee  
這篇文章主要介紹了Springmvc數(shù)據(jù)格式化原理及代碼案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1、簡介

  • Converter可以將一種類型轉(zhuǎn)換成另一種類型,是任意Object之間的類型轉(zhuǎn)換。
  • Formatter則只能進(jìn)String與任意Object對象的轉(zhuǎn)換,它提供解析與格式化兩種功能
    • 解析:將String類型字符串轉(zhuǎn)換為任意Objec對象,
    • 格式化:將任意Objec對象轉(zhuǎn)換為字符串進(jìn)行格式化顯示。
  • 使用Formatter
    • 實(shí)現(xiàn)Formatter接口定義一個類,T為要解析得到或進(jìn)行格式化的數(shù)據(jù)類型。
    • 在類中實(shí)現(xiàn)兩個方法
      • String print(T t,Locale locale):把T類型對象解析為字符串形式返回
      • T parse(String sourse,Locale locale):由字符串解析得到T類型對象。

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">
  請輸入日期(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、在配置文件注冊自定義數(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)資源不過濾-->
  <mvc:default-servlet-handler/>
  <!--開啟springmvc注解支持,注冊自定義數(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 +
        '}';
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 中IO流字符流詳解及實(shí)例

    Java 中IO流字符流詳解及實(shí)例

    這篇文章主要介紹了Java 中IO流字符流詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Mybatis-Plus根據(jù)自定義注解實(shí)現(xiàn)自動加解密的示例代碼

    Mybatis-Plus根據(jù)自定義注解實(shí)現(xiàn)自動加解密的示例代碼

    我們把數(shù)據(jù)存到數(shù)據(jù)庫的時候,有些敏感字段是需要加密的,從數(shù)據(jù)庫查出來再進(jìn)行解密,如果我們使用的是Mybatis框架,那就跟著一起探索下如何使用框架的攔截器功能實(shí)現(xiàn)自動加解密吧,需要的朋友可以參考下
    2024-06-06
  • Spring Bean作用域與生命周期深入講解

    Spring Bean作用域與生命周期深入講解

    這篇文章主要介紹了淺談Spring中Bean的作用域,生命周期和注解,從創(chuàng)建到消亡的完整過程,例如人從出生到死亡的整個過程就是一個生命周期。本文將通過示例為大家詳細(xì)講講,感興趣的可以學(xué)習(xí)一下
    2022-07-07
  • Java Web實(shí)現(xiàn)文件下載和亂碼處理方法

    Java Web實(shí)現(xiàn)文件下載和亂碼處理方法

    文件上傳和下載是web開發(fā)中常遇到的問題。今天小編給大家分享下Java Web實(shí)現(xiàn)文件下載和亂碼處理方法的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 微信小程序完整項(xiàng)目實(shí)戰(zhàn)記錄(前端+SpringBoot后端)

    微信小程序完整項(xiàng)目實(shí)戰(zhàn)記錄(前端+SpringBoot后端)

    隨著微信小程序的流行,越來越多的開發(fā)者開始涉足小程序開發(fā),下面這篇文章主要給大家介紹了關(guān)于微信小程序完整項(xiàng)目實(shí)戰(zhàn)的相關(guān)資料,項(xiàng)目包括前端+SpringBoot后端,需要的朋友可以參考下
    2024-09-09
  • Java接口中盡量避免使用數(shù)組

    Java接口中盡量避免使用數(shù)組

    這篇文章主要介紹了Java接口中盡量避免使用數(shù)組的建議,根據(jù)實(shí)際示例展示了接口中使用數(shù)組而造成的影響程序速度的現(xiàn)象,需要的朋友可以參考下
    2015-07-07
  • IDEA創(chuàng)建Maven項(xiàng)目后報錯不出現(xiàn)src文件夾的情況解決

    IDEA創(chuàng)建Maven項(xiàng)目后報錯不出現(xiàn)src文件夾的情況解決

    最近剛開始學(xué)習(xí)maven,正準(zhǔn)備使用idea創(chuàng)建一個maven項(xiàng)目練手,卻發(fā)現(xiàn)自己創(chuàng)建的maven項(xiàng)目始終沒有src目錄,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Maven項(xiàng)目后報錯不出現(xiàn)src文件夾的情況解決,需要的朋友可以參考下
    2023-05-05
  • 基于Java 注解(Annotation)的基本概念詳解

    基于Java 注解(Annotation)的基本概念詳解

    基于Java 注解(Annotation)的基本概念詳解
    2013-04-04
  • java發(fā)送http請求并獲取狀態(tài)碼的簡單實(shí)例

    java發(fā)送http請求并獲取狀態(tài)碼的簡單實(shí)例

    下面小編就為大家?guī)硪黄猨ava發(fā)送http請求并獲取狀態(tài)碼的簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Java中Stream流Map分組方式詳細(xì)匯總

    Java中Stream流Map分組方式詳細(xì)匯總

    Stream將要處理的元素集合看作一種流,在流的過程中借助Stream?API對流中的元素進(jìn)行操作,比如篩選、排序、聚合等,下面這篇文章主要給大家介紹了關(guān)于Java中Stream流Map分組方式的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評論