Struts 2 reading notes -- custom type converter for struts 2

Source: Internet
Author: User

Most of the time, we use struts 2The built-in type converter can meet most of the type conversion requirements. However, in some special cases, if you want to convert a string into a composite object, you need to use a custom type converter.

To implement a custom type converter, you must follow these two steps:

1, Write your own type converter

2, Registered type converter

 

1. Custom type converter

Generally, a class is required for writing a custom type converter:Defatypetypeconverter. To implement a custom converter, you must override this classConvertvalueMethod.

convertvalue the method converts the type. However, this type of conversion is bidirectional, that is, to convert a string to User This method is used for instances; User converting an instance to a string is also implemented through this method.

to enable bidirectional conversion, you can determine totype to determine the conversion direction. totype type is the target type to be converted. When totype type: User type, convert the string to User Instance; When totype type: string indicates that User the instance is converted to the string type.

2. Register a type converter

Register a type converter.WebApplication, tellStruts 2How to use these types of converters.

Struts 2Three types of converters are provided:

Register a local converter: This converter only applies toActionAttribute

Register a global converter: This converter applies to allActionWill take effect for specific types of attributes.

UseJDKTo register a type converter: to register a type converter by commenting.

 

1), Local type converter

Register the local type converter and use the local type conversion file to specify. Add a line to the conversion file:

<Propname >=< converterclass>

Set<Propname>Replace it with the attribute to be converted. Set<Converterclass>Replace it with the Implementation class of the type converter.

Generally, local Type converters have great limitations. Therefore, the type converter is generally registered as a global type converter.

 

2) Global Converter

The global type converter is not for the specifiedActionThe specified attribute takes effect, but takes effect on the specified type.

To register a global converter, you must provideXwork-conversion.propertiesFile.

To register a global type converter, you only need to add a line to the global type conversion file:

<Proptype >=< convertclass>

Set<Proptype>Replace it with the type to be converted.<Convertclass>Replace it with the Implementation class of the type converter.

Example:

Type converter:

 1  Public class userconverter extends defaulttypeconverter {  2   3   // Rewrite the convertvalue method. This method requires bidirectional conversion.  4   Public object convertvalue (MAP context, object value, class totype ){  5   // When the string type needs to be converted to the user type  6   If (totype = userbean. Class ){  7   // The system request parameter is a String Array  8  String [] Params = (string []) value;  9 For (INT I = 0; I <  Params  . Length; I ++ ){  10   System. Out. println (Params [I]);  11   }  12   // Create a user instance  13   Userbean user  = New Userbean ();  14   // Only process the first array element of the request parameter array and separate the string with commas (,) into two strings.  15   String [] uservalue  = Params [0]. Split (",");  16               // Assign a value to the user instance  17   User. setname (uservalue [0]);  18   User. setpassword (uservalue [1]);  19               20  Return user;  21   }  22   If (totype  =  String. Class ){  23   // Forcibly convert the value to the user type  24   Userbean user  = (Userbean)  Value;  25   Return "<" + User. getname () + "," + User. GetPassword () +" >  ";  26   }  27   Return NULL;  28   }  29       30 }

 

Registration type converter:

 
1Com. App. entity. userbean = com. App. converter. userconverter

 

JSP file:

 1   <  S: Form  Action  = "Registerlogin"  >  2         <  S: textfield  Name  = "User. Name"  Label  = "User Name"  > </ S: textfield  >  3          <  S: Submit  Value  = "Login"  > </  S: Submit  >  4      </  S: Form  > 

In the text box on the JSP page, enter chenssy and chentmt to log on successfully.

In fact, struts 2 provides a strutstypeconverter abstract class, which can simplify the implementation of the type converter. This class implements defaulttypeconverter convertvalue method, when this method is implemented, it replaces two different conversion methods with different methods-when you need to convert a string to a composite type, call convertfromstring abstract method ; when you need to convert the composite type to a string, call converttostring abstract method.

As shown above, we only need to inherit strutstypeconmverter abstract class, and rewrite convertfromstring methods and converttostring you can simply implement your own type converter.

  Public   class  userconverter  extends   strutstypeconverter { ///   Method for converting string type to composite type   Public   Object convertformstring (MAP context, string [] values, class toclass ){......}   //   methods for converting composite types to string types   Public String converttostring (MAP context, object O) {.........}}  

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.