Formatter in Spring3 is combined with Velocity to format the output instance, spring3formatter
Before reading this article, I hope you have some knowledge about Spring3's Formatter mechanism. If you do not know about it, you can click here.
In Spring3, there are two levels for formatting. One is for type-level formatting and the other is for field formatting.
First, for formatting at the type level, that is to say, for example, if I use a formatting scheme for the Date type, you can use the following method for registration:
conversionService.addFormatter(new Formatter<Date>() {@Overridepublic String print(Date object, Locale locale) {return null;}@Overridepublic Date parse(String text, Locale locale)throws ParseException {return null;}});
For field-level formatting, the flexibility is relatively high. It is a method for formatting a field of a class (usually in the form of annotations ):
Public class TestEntity {// @ DateTimeFormat (pattern = "yyyy-MM-dd HH: mm: ss") private Date date; @ NumberFormat (pattern = "RMB :##. 00 ") private Double salary; public Date getDate () {return date;} public void setDate (Date date) {this. date = date;} public Double getSalary () {return salary;} public void setSalary (Double salary) {this. salary = salary ;}}
The field resolution is the same. If it is a field-level resolution, annotations must be added when defining method parameters:
@RequestMapping(value = "/format2")public String test2(@NumberFormat @RequestParam("salary") Double salary, @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @RequestParam("date") Date date) { System.out.println(phoneNumber); System.out.println(date); return "format/success2";}
Given the frequent appearance of the Date type in formatting, let's use Date as an example: We will first implement type-level formatting.
public class DateFormatter implements Formatter<Date> {@Overridepublic String print(Date object, Locale locale) {try {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale).format(object);} catch (Exception e) {return new SimpleDateFormat("yyyy-MM-dd", locale).format(object);}}@Overridepublic Date parse(String text, Locale locale) throws ParseException {try {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale).parse(text);} catch (Exception e) {return new SimpleDateFormat("yyyy-MM-dd", locale).parse(text);}}}
Here we have also introduced a clever use of try... catch. We use it to help us deal with the Date types in two different formats.
After Formatter is written, register it with FormattingConversionService.
conversionService.addFormatter(new DateFormatter());
In this way, when we use the conversionService. converter (...) method to convert data of the Date and String types, we will call the parse and print methods in DateFormatter.
However, it seems too difficult to manually convert each time. Spring seems to provide <spring: eval expression = "model. date"> for automatic formatting, but I have never touched on this.
Let's implement it manually. Here we use the Velocity direve ve to implement it.
Public class formatterdireve ve extends direve ve {private FormattingConversionService conversionService; @ Overridepublic String getName () {return "formatter" ;}@ Overridepublic int getType () {return LINE ;} @ Overridepublic boolean render (InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, expiration, MethodInvocationException {if (conversionService = null) {conversionService = new partition (); conversionService. addFormatter (new DateFormatter ();} String param = node. jjtGetChild (0 ). value (context ). toString (); String [] params = param. split ("\\. "); BeanWrapper beanWrapper = new BeanWrapperImpl (context. get (params [0]); // obtain the type information TypeDescriptor descriptor; descriptor = beanWrapper. getPropertyTypeDescriptor (params [1]); TypeDescriptor stringDescriptor = TypeDescriptor. valueOf (String. class); Object value = conversionService. convert (beanWrapper. getPropertyValue (params [1]), descriptor, stringDescriptor); writer. write (value. toString (); return true ;}}
Front-end page writing:
#formatter("person.salary")<br /> #formatter("person.date")<br />
Does it look similar to the method provided by spring?