Spring3中的Formatter结合Velocity格式化输出实例

来源:互联网 发布:百度文库免费下载软件 编辑:程序博客网 时间:2024/05/18 16:35

看本文之前,希望你对Spring3的Formatter机制有所了解,还不了解的可以猛戳这里。

Spring3中对于格式化有两种级别,一种是针对类型级别的格式化,另一种是针对字段的格式化。

首先,针对类型级别的格式化也就是说,比如对于Date类型,我都采用一种格式化方案,那么可以用如下方式注册:

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;}});

而针对字段级别的格式化,它的灵活度就相对较高,是对于某个类的某个字段而进行格式化的一种方案(一般采用注解形式):

public class TestEntity {//@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date date;@NumberFormat(pattern = "人民币:##.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;}}


而对于对字段的解析来说也是一样的,若是字段级别的解析,那么在定义方法参数的时候,需要带上注解:

@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";} 

鉴于Date类型在格式化中的出场频率较高,那么我们就用Date来举例吧:我们先来实现类型级别的格式化

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);}}}

这里也提现了try...catch的一个妙用,我们使用它来帮助我们处理两种不同格式的Date类型

写好Formatter之后,我们将它注册到FormattingConversionService上去

conversionService.addFormatter(new DateFormatter());

这样一来,我们在使用conversionService.converter(...)方法转换Date和String类型的数据时,就会调用DateFormatter中的parse和print方法了

但是每次都手动来转换似乎太麻烦了,Spring貌似提供了<spring:eval expression="model.date">来实现自动格式化,可是这个没怎么接触过

还是自己来手动实现一个吧,这里是结合了Velocity的 Directive来实现的。

public class FormatterDirective extends Directive {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,ParseErrorException, MethodInvocationException {if (conversionService == null) {conversionService = new DefaultFormattingConversionService();conversionService.addFormatter(new DateFormatter());}String param = node.jjtGetChild(0).value(context).toString();String[] params = param.split("\\.");BeanWrapper beanWrapper = new BeanWrapperImpl(context.get(params[0]));// 获取类型信息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;}}

前台页面写法:

#formatter("person.salary")<br /> #formatter("person.date")<br />

看起来是不是和spring提供的那种写法类似呢?



2 0