Struts2 uses 9-Converter

Source: Internet
Author: User
Tags dateformat

Struts2.0 built-in Converter

Struts 2.0 has implemented a frequently used converter, such as a date, integer, or floating point number. The following lists the implemented converters.

L predefined types, such as int, Boolean, and double;

L date type: use the short format conversion of the current region (locale), that is, dateformat. getinstance (dateformat. Short );

L collection type, which converts the string data returned by request. getparametervalues (string Arg) to Java. util. collection;

L set type, similar to list conversion, with the same value removed;

L array type: converts each element of the string array to a specific type and forms an array.

For existing converters, you do not have to re-invent the wheel. Struts will automatically call the corresponding converter when encountering these types.

 

Custom Converter

To convert a custom type, you need to implement the strutstypeconverter class. There are two methods to be implemented. One is to convert strings into new classes, and the other is to convert new types into strings. For example:

Public class mytypeconverter extends strutstypeconverter {

Public object convertfromstring (MAP context, string [] values, class toclass ){

...

}

Public String converttostring (MAP context, object O ){

...

}

}

Before using the new converter in action, you need to configure it. A separate "*-conversion. properties" file can be used here. If the action that is using the conversion function is called "myaction", you need to create a file named "MyAction-conversion.properties" under the same package. File Content:

Typeval = mytypeconverter

The "typeval" on the left of the equation is the request name or the form value to be converted, and the right of the equation is the fully qualified name of the conversion class.

If you use the Transform Feature across multiple actions, you can use a global profile "xwork-conversion.properties ". This file is stored in the classpath root directory of the application. File Content:

Mytype = mytypeconverter

The equation is the fully qualified name of the class corresponding to the type to be converted (the full name of the type to be converted (including the package path and class name) and the converter. Note that the class name of the type to be converted is used here, instead of the Request Name or form value. Therefore, the setter of the action is "settypevalue (mytype type )".

 

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.