SpringMVC 16. Data Formatting

data formatting

1. Formatting the input/output of attribute objects still belongs to the category of "type conversion" in essence.

2. Spring defines an implementation class that implements the ConversionService interface FormattingConversionService in the formatting module. This implementation class extends GenericConversionService, so it has both type conversion and formatting functions.

3.FormattingConversionService has a FormattingConversionServiceFactroyBean factory class, which is used to construct the former in the Spring context

4. FormattingConversionServiceFactroyBean has been registered internally:

  • NumberFormatAnnotationFormatterFactroy: Supports the
    use of
  • JodaDateTimeFormatAnnotationFormatterFactroy: Supports the use of @DateTimeFormat annotations
    on

5. After assembling the FormattingConversionServiceFactroyBean, you can use the annotation driver for Spring MVC input parameter binding and model data output. The ConversionService instance created by default is FormattingConversionServiceFactroyBean

date formatting

  • The @DateTimeFormat annotation can annotate java.util.Date, java.util.Calendar, java.long.Long time types:
  • pattern attribute: the type is string. Specifies the mode for parsing/formatting field data, eg: "yyyy-MM-dd hh:mm:ss"
  • iso property: of type DateTimeFormat.ISO. Specifies the ISO mode for parsing/formatting field data, including four: ISO.NONE (not used) - default, ISO.DATE(yyyy-MM-dd), ISO.TIME(hh:mm:ss.SSSZ), ISO .DATE_TIME(yyyy-MM-dd hh:mm:ss.SSSZ)
  • style attribute: String type. Specify the format of the date and time through the style, which consists of two characters, the first digit represents the date format, and the second digit represents the time format: S: short date/time format, M: medium date/time format, L: long date /time format, F: full date/time format, -: ignore date or time format.

Numeric formatting

@NumberFormat can annotate properties like number types, and it has two mutually exclusive properties:

  • style: The type is NumberFormat.Style. Used to specify the style type, including three types: Style.NUMBER (normal number type), Style.CURRENCY (currency type), Style.PERCENT (percentage type)
  • pattern: Type is String, custom style, such as pattern=”#,###”;

Sample code:


    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birth ;

    @NumberFormat(pattern = "#,###,###.#")
    private Float salary ;

In this way, when we enter the corresponding format file in the input.jsp page, it will be automatically formatted.

  • Note here: springmvc.xml
<mvc:annotation-driven></mvc:annotation-driven>

if:

<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

<!--配置conversionService-->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
      <property name="converters">
            <set>
                <ref bean="employeeConverter"></ref>
            </set>
        </property>
    </bean>

The above formatting does not take effect.

Do both at the same time: replace the bean.

<!--解决上述冲突-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="employeeConverter"></ref>
            </set>
        </property>
    </bean>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325405991&siteId=291194637