</body>
1. First, we will directly use Formatter for demonstration. Add the FormatModel. java content to the com. demo. web. models package as follows:
package com.demo.web.models;public class FormatModel{ private String money; private String date; public String getMoney(){ return money; } public String getDate(){ return date; } public void setMoney(String money){ this.money=money; } public void setDate(String date){ this.date=date; } }
Add FormatController. java to the com. demo. web. controllers package as follows:
Package com. demo. web. controllers; import java. math. roundingMode; import java. util. date; import java. util. locale; import org. springframework. context. i18n. localeContextHolder; import org. springframework. format. datetime. dateFormatter; import org. springframework. format. number. currencyFormatter; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. Bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import com. demo. web. models. formatModel; @ Controller @ RequestMapping (value = "/format") public class FormatController {@ RequestMapping (value = "/test", method = {RequestMethod. GET}) public String test (Model model) throws NoSuchFieldException, SecurityException {if (! Model. containsAttribute ("contentModel") {FormatModel formatModel = new FormatModel (); CurrencyFormatter currencyFormatter = new CurrencyFormatter (); currencyFormatter. setFractionDigits (2); // retain 2 decimal places currencyFormatter. setRoundingMode (RoundingMode. HALF_UP); // round to the nearest side. If the distance between the two sides is equal, round up (rounding) DateFormatter dateFormatter = new DateFormatter (); dateFormatter. setPattern ("yyyy-MM-dd HH: mm: ss"); Locale locale = LocaleContextHolder. getLocale (); formatModel. setMoney (currencyFormatter. print (12345.678, locale); formatModel. setDate (dateFormatter. print (new Date (), locale); model. addAttribute ("contentModel", formatModel) ;}return "formattest ";}}
Run the test:
Change the preferred browser language:
Refresh page:
2. In this demonstration, use defaformatformattingconversionservice to change FormatController. java to the following content:
Package com. demo. web. controllers; import java. math. roundingMode; import java. util. date; import org. springframework. format. datetime. dateFormatter; import org. springframework. format. number. currencyFormatter; import org. springframework. format. support. defaultFormattingConversionService; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. bind. an Notation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import com. demo. web. models. formatModel; @ Controller @ RequestMapping (value = "/format") public class FormatController {@ RequestMapping (value = "/test", method = {RequestMethod. GET}) public String test (Model model) throws NoSuchFieldException, SecurityException {if (! Model. containsAttribute ("contentModel") {FormatModel formatModel = new FormatModel (); CurrencyFormatter currencyFormatter = new CurrencyFormatter (); currencyFormatter. setFractionDigits (2); // retain 2 decimal places currencyFormatter. setRoundingMode (RoundingMode. HALF_UP); // round to the nearest side. If the distance between the two sides is equal, round up (rounding) DateFormatter dateFormatter = new DateFormatter (); dateFormatter. setPattern ("yyyy-MM-dd HH: mm: ss"); DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService (); conversionService. addFormatter (currencyFormatter); conversionService. addFormatter (dateFormatter); formatModel. setMoney (conversionService. convert (12345.678, String. class); formatModel. setDate (conversionService. convert (new Date (), String. class); model. addAttribute ("contentModel", formatModel) ;}return "formattest ";}}
Locale locale = LocaleContextHolder is missing this time. getLocale (); run the test again, change the language, and refresh it. You can see the same effect as the first method. It means that defaformatformattingconversionservice will automatically return the corresponding format based on the browser request information.
3. Some people may think, ah... I just want to format the display, but it is still so troublesome to write code to convert a field and a field ??? Don't worry. The above is just a demonstration of the built-in formatting converter, which is certainly not used in the actual project. The following describes the annotation-based formatting. First, change FormatModel. java to the following content:
package com.demo.web.models;import java.util.Date;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.format.annotation.NumberFormat;import org.springframework.format.annotation.NumberFormat.Style;public class FormatModel{ @NumberFormat(style=Style.CURRENCY) private double money; @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date date; public double getMoney(){ return money; } public Date getDate(){ return date; } public void setMoney(double money){ this.money=money; } public void setDate(Date date){ this.date=date; } }
Note: Here, money and date are no longer of the String type, but their own types.
Change FormatController. java to the following content:
package com.demo.web.controllers;import java.util.Date;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.demo.web.models.FormatModel;@Controller@RequestMapping(value = "/format")public class FormatController { @RequestMapping(value="/test", method = {RequestMethod.GET}) public String test(Model model) throws NoSuchFieldException, SecurityException{ if(!model.containsAttribute("contentModel")){ FormatModel formatModel=new FormatModel(); formatModel.setMoney(12345.678); formatModel.setDate(new Date()); model.addAttribute("contentModel", formatModel); } return "formattest"; } }
Note: Only the value assignment in the Code is not formatted.
Modify the formattest. jsp view as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
Note: here you need to add reference <% @ taglib prefix = "spring" uri = "http://www.springframework.org/tags" %> and use spring: eval to bind the value to be displayed.
Run the test to change the browser language, refresh the page, and you can still see the same effect as the first method, proving that the annotation is valid.