Custom type converters-local and global

Source: Internet
Author: User
Tags abstract

Java EE Development of various resources download list, the history of the most IT resources, personal collection summary.


Using a custom type converter in the STRUTS2 framework requires registration so that the system knows which type of converter to use when processing user requests.

1. Registering a local type converter

A local type converter is scoped to the action range. Registering a local type converter requires establishing a property file named Actionname-conversion.properties, which is stored in the same directory as the action class file. ActionName is the class name of the class using the action implementation of the type converter, and the subsequent "-conversion.properties" is a fixed format. The file is a standard property file, and the content is in the standard key-value format, and the key value pair is defined as follows:

Propertyname= type converter

For example: The contents of the Registproductaction-conversion.properties property file are as follows:

Userage=edu.converter.integertypeconverter
Product.productid=edu.converter.strutstypeconverter
Product.createdate=edu.converter.datetypeconverter

2. Registering a global type converter

To register a global type converter, you need to create a Xwork-conversion.properties property file that needs to be stored in the root directory of the class path, such as web-inf/classes. The file is also composed of several standard Key-value key-value pairs whose content is defined as "composite type-corresponding type converter", where the composite type is the Type property of the action that requires type conversion, and the corresponding type converter is the corresponding converter that transforms the composite type.

Example: The contents of the Xwork-conversion.properties file are as follows:

Java.util.date=edu.converter.datetypeconverter

3. OGNL-based type converters

The Struts2 type converter needs to implement a TypeConverter interface, which is located within the Ognl.jar package, and also uses OGNL technology for the Struts2 frame converter. The interface defines a Convertvalue () method, and the type converter implementation class that implements the interface needs to override the method for type conversion. OGNL also provides a class defaulttypeconverter that implements the TypeConverter interface, and developers can develop implementation classes for type converters as long as they inherit the class.

Subclasses of the Defaulttypeconverter class need to override the Convertvalue () method to implement a bidirectional conversion between a string type and a composite type. Convertvalue () method has three parameters: Map context: This parameter is the context content of the type conversion environment; Object Value: This parameter is a parameter that needs to be converted; Class ToType: This parameter refers to the type of the conversion target.

public Object Convertvalue (map<string, object> context, Object value,class totype); Note that value here is an array type, Because STRUTS2 is compatible with the parameter of the checkbox type.

Note : As you can see, the Convertvalue () method is used for bidirectional conversions through the ToType type judgment.

4. STRUTS2-based type converters

The STRUTS2 framework provides a type converter's Strutstypeconverter abstract class that developers can inherit to develop custom type converter implementation classes. The abstract class actually inherits the Defaulttypeconverter class, which is simplified on the basis of this class. The Strutstypeconverter abstract class is the basis of a type converter in the STRUTS2 framework, which provides two abstract methods that implement a two-way conversion between a form string parameter-struts compound type, respectively. Public Object convertfromstring (Map context, string[] values, Class toclass);

Public String converttostring (Map context, Object obj);


5. Example:

Registproductaction.java


Product.java

Properties file directory structure:


edu/action/ Registproductaction-conversion.properties


edu/action/product-conversion.properties


As you can see from the above, there are two ways to type conversions for a composite type to receive request parameters:

(1) Registproductaction-conversion.properties in product.productid= ....

(2) Product-conversion.properties in productid= ...


Datetypeconverter.java

Integertypeconverter.java

Strutstypeconverter.java

Global type converter: xwork-conversion.properties

Note: It is a good idea not to use a global type converter, because after such a definition, it means that all properties of the Java.util.Date type in the action will be converted according to the type defined in Datetypeconverter.java.

On the browser, enter:


Click the reigst button:

Console output:

The console does not output a return type conversion statement, does not know why, and does not call the callback method ....

If there is no Datetypeconverter.java, then, createdate= "20121208", then the console will output:


Browser:


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.