Spring MVC Learning record about JSP page passing to controller layer parameter type conversion (formatting)-January 2018

Source: Internet
Author: User
Tags locale

The


Spring MVC jsp passes parameters to a controller that involves date type data and needs to use a type converter:
The author now finds two types of converters that can be used:
type one : Implement CONVERT<SOURCE,TARGET> Interface (source data, Target data type), the implementation function is a data type to another data type: The
Data Transformation class is as follows: When Datetimeformatter.ofpattern ("Yyyy/mm/dd") is not added ( MM must be uppercase, lowercase for time, and the String style "YYYY-MM-DD"
public class Stringtolocaldate implements Converter<string, which is required by default.       Localdate>//string to Localdate
{
@Override
public localdate convert (String s)
{
Return Localdate.parse (S,datetimeformatter.ofpattern ("Yyyy/mm/dd")); The
}
}
Spring-servlet.xml is configured as follows: in MVC: Annoation-driven internal Conversion-service The default assembly of the Bean is Formattingconversionservicefactorybean, So the settings that need to be displayed when you use convert are Conversionservicefactorybean

for the conversion-service corresponding bean.

<mvc:annotation-driven conversion-service= "conversionservice"/>
<!--register a custom data type converter factory and add a custom converter bean-->
<bean class= "Org.springframework.context.support.ConversionServiceFactoryBean" id= "conversionservice ">
<property name= "Converters" >
<set>
<bean class= "Com.vastliu.DataConvert.StringToLocalDate"/>//stringtolocaldate is a custom type converter
</set>
</property>
</bean>
To this borrow convert to implement a custom converter, the interface that needs to be implemented and all the places that need to be configured, you can convert the source type to the target type. Here I use a string to convert to Localdate as an example;


   type two: UsingFormattingconversionservicefactorybeanFormatted transformations, where annotations can be used for simple conversions, or you can use custom class methods to convert
Annotation Conversions:
To use annotations on the attributes of the Pojo entity, the author uses localdate as an example, so I need to add annotations to the variable with the data type localdate:
   Import Org.springframework.format.annotation.DateTimeFormat; Import the package corresponding to the note
   @DateTimeFormat (pattern= "Yyyy-mm-dd")  Pattern is a style type that sets the incoming string, and a string other than this style type cannot be converted to the target type localdate
Private Localdate ReleaseDate; Release date
Since the Mvc:annoation-driven internal default is Formattingconversionservicefactorybean the factory, so there is no need to display the settings Conversion-service,
<mvc:annotation-driven/>
Suggest or display the settings for a bit
<mvc:annotation-driven conversion-service= "ConversionService2"/>
<bean class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean" id= "ConversionService2"/>
At this point the annotation transformation needs to complete the configuration, etc. has been completed;

   Custom class Conversions:
You need to create a custom transformation class and implementformatter<t>interface, as follows:
public classLocaldateformatterfordashImplements Formatter<localdate>
{
/* Declare a DateTime formatted property */
Private DateTimeFormatter DateTimeFormatter;
/* Declare a string as the formatted style */
Private String Datepattern;
/* Create a constructor for initialization to get the string style datepattern that needs to be converted, and generate a DateTimeFormatter object based on the Datepattern style */
Public Localdateformatterfordash (String Datepattern)
{
This.datepattern=datepattern;
Datetimeformatter=datetimeformatter.ofpattern (Datepattern);
}
/* Override the method provided by the interface to convert the source string data source to the target data type localdate,local is localized */
@Override
Public Localdate Parse (String source, locale locale) throws ParseException
{
/* Convert string that conforms to Datepattern style to localdate and return */
Localdate Localdate=localdate.parse (Source,datetimeformatter);
return localdate;
}
/* Convert Localdate to String */
@Override
Public String print (localdate localdate, locale locale)
{
Return Localdate.format (DateTimeFormatter);
}
}

To customize the converter class and declare and edit it, the next step is to let Spring do its work:
There are two ways of doing this:
The first is to add the class directly to the Formattingconversionservicefactorybean as follows:
<mvc:annotation-driven conversion-service= "Conversionservice"/>
<bean class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean" id= "Conversionservice ">
<!--adding custom formatter to the formatter warehouse--
<property name= "Formatters" >
<set>
<bean class= "Com.vastliu.DataConvert. Localdateformatterfordash">
<!--constructor to inject string style yyyy/mm/dd-->
<constructor-arg name= "Datepattern" value= "Yyyy/mm/dd"/>
</bean>
</set>

</property>

  </bean>
To this direct Add method has completed the relevant configuration;
The second is to add the class to the Myformatterregistrar registration class, complete the registration in the class
Myformatterregistrar Registration class Implementation Formatterregistrar the interface
public class Myformatterregistrar implements Formatterregistrar
{
/* Declares the need to convert the style of String */
Private String Datapatternfordash;
/* Constructor */
Public Myformatterregistrar (String Datapatternfordash)
{
This.datapatternfordash=datapatternfordash;
}

@Override
public void Registerformatters (Formatterregistry formatterregistry)
{
/* Register (ADD) The created custom class converter into the registration class, complete the registration (to register the style in the Class) */
Formatterregistry.addformatter (New Localdateformatterfordash (Datapatternfordash));
}
}
XML configuration:
<mvc:annotation-driven conversion-service= "Conversionservice"/>
<bean class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean" id= "Conversionservice">
<!--Adding a registration class to formattingconversionservicefactorybean-->
<property name= "Formatterregistrars" >
<set>
<!--initializes the custom registration class and passes in the string style--
<bean class= "Com.vastliu.DataConvert.MyFormatterRegistrar" >
<constructor-arg name= "Datapatternfordash" value= "Yyyy-mm-dd"/>
</bean>
</set>
</property>
</bean>
To this with the use of the registration class has been completed the relevant configuration, the data type to share the conversion of several ways have been shared, such as a flawed look;
PS: Custom converters for the same target data type (e.g., customizing multiple converters for localdate) spring defaults to using the last converter in the XML configuration to perform the conversion operation.

Spring MVC Learning record about JSP page passing to controller layer parameter type conversion (formatting)-January 2018

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.